refactored http import name, fixed and simplified idlewatcher/idlewaker implementation, dependencies update

This commit is contained in:
yusing
2024-10-07 12:45:07 +08:00
parent 929b7f7059
commit 921ce23dde
19 changed files with 194 additions and 261 deletions

View File

@@ -2,7 +2,6 @@ package idlewatcher
import (
"context"
"net/http"
"sync"
"sync/atomic"
"time"
@@ -96,10 +95,8 @@ func Register(entry *P.ReverseProxyEntry) (*watcher, E.NestedError) {
return w, nil
}
func Unregister(entry *P.ReverseProxyEntry) {
if w, ok := watcherMap[entry.ContainerID]; ok {
w.refCount.Add(-1)
}
func (w *watcher) Unregister() {
w.refCount.Add(-1)
}
func Start() {
@@ -133,12 +130,6 @@ func Stop() {
mainLoopWg.Wait()
}
func (w *watcher) PatchRoundTripper(rtp http.RoundTripper) roundTripper {
return roundTripper{patched: func(r *http.Request) (*http.Response, error) {
return w.roundTrip(rtp.RoundTrip, r)
}}
}
func (w *watcher) containerStop() error {
return w.client.ContainerStop(w.ctx, w.ContainerID, container.StopOptions{
Signal: string(w.StopSignal),
@@ -253,11 +244,9 @@ func (w *watcher) watchUntilCancel() {
switch {
// create / start / unpause
case e.Action.IsContainerWake():
w.ContainerRunning = true
ticker.Reset(w.IdleTimeout)
w.l.Info(e)
default: // stop / pause / kill
w.ContainerRunning = false
ticker.Stop()
w.ready.Store(false)
w.l.Info(e)
@@ -272,13 +261,10 @@ func (w *watcher) watchUntilCancel() {
w.l.Debug("wake signal received")
ticker.Reset(w.IdleTimeout)
err := w.wakeIfStopped()
if err != nil && err.IsNot(context.Canceled) {
if err != nil {
w.l.Error(E.FailWith("wake", err))
}
select {
case w.wakeDone <- err: // this is passed to roundtrip
default:
}
w.wakeDone <- err
}
}
}