improved idlewatcher support for API-like services, fixed idlewaker proxying to zero port

This commit is contained in:
yusing
2024-10-07 18:50:51 +08:00
parent ef83ed0596
commit d1c9e18c97
3 changed files with 38 additions and 52 deletions

View File

@@ -12,6 +12,7 @@ import (
E "github.com/yusing/go-proxy/internal/error"
P "github.com/yusing/go-proxy/internal/proxy"
PT "github.com/yusing/go-proxy/internal/proxy/fields"
F "github.com/yusing/go-proxy/internal/utils/functional"
W "github.com/yusing/go-proxy/internal/watcher"
)
@@ -45,9 +46,11 @@ var (
mainLoopCancel context.CancelFunc
mainLoopWg sync.WaitGroup
watcherMap = make(map[string]*watcher)
watcherMap = F.NewMapOf[string, *watcher]()
watcherMapMu sync.Mutex
portHistoryMap = F.NewMapOf[PT.Alias, string]()
newWatcherCh = make(chan *watcher)
logger = logrus.WithField("module", "idle_watcher")
@@ -65,7 +68,11 @@ func Register(entry *P.ReverseProxyEntry) (*watcher, E.NestedError) {
key := entry.ContainerID
if w, ok := watcherMap[key]; ok {
if entry.URL.Port() != "0" {
portHistoryMap.Store(entry.Alias, entry.URL.Port())
}
if w, ok := watcherMap.Load(key); ok {
w.refCount.Add(1)
w.ReverseProxyEntry = entry
return w, nil
@@ -88,7 +95,7 @@ func Register(entry *P.ReverseProxyEntry) (*watcher, E.NestedError) {
w.refCount.Add(1)
w.stopByMethod = w.getStopCallback()
watcherMap[key] = w
watcherMap.Store(key, w)
go func() {
newWatcherCh <- w
@@ -118,7 +125,7 @@ func Start() {
w.watchUntilCancel()
w.refCount.Wait() // wait for 0 ref count
delete(watcherMap, w.ContainerID)
watcherMap.Delete(w.ContainerID)
w.l.Debug("unregistered")
mainLoopWg.Done()
}()