fixed idlewatcher panics and incorrect behavior, update screenshot

This commit is contained in:
yusing
2024-10-06 16:17:52 +08:00
parent 03cad9f315
commit de7805f281
6 changed files with 17 additions and 11 deletions

View File

@@ -17,12 +17,6 @@ func (rt roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
}
func (w *watcher) roundTrip(origRoundTrip roundTripFunc, req *http.Request) (*http.Response, error) {
// wake the container
select {
case w.wakeCh <- struct{}{}:
default:
}
// target site is ready, passthrough
if w.ready.Load() {
return origRoundTrip(req)
@@ -53,6 +47,11 @@ func (w *watcher) roundTrip(origRoundTrip roundTripFunc, req *http.Request) (*ht
case <-w.ctx.Done():
return
default:
// wake the container and reset idle timer
select {
case w.wakeCh <- struct{}{}:
default:
}
resp, err := origRoundTrip(req)
if err == nil {
w.ready.Store(true)
@@ -75,9 +74,9 @@ func (w *watcher) roundTrip(origRoundTrip roundTripFunc, req *http.Request) (*ht
if ctx.Err() == context.DeadlineExceeded {
return w.makeErrResp("Timed out waiting for %s to fully wake", w.ContainerName)
}
return w.makeErrResp("idlewatcher has stopped\n%s", w.ctx.Err().Error())
return w.makeErrResp("idlewatcher has stopped\n%s", w.ctx.Err())
case <-w.ctx.Done():
return w.makeErrResp("idlewatcher has stopped\n%s", w.ctx.Err().Error())
return w.makeErrResp("idlewatcher has stopped\n%s", w.ctx.Err())
}
}
}

View File

@@ -170,6 +170,10 @@ func (w *watcher) containerStatus() (string, E.NestedError) {
}
func (w *watcher) wakeIfStopped() E.NestedError {
if w.ready.Load() || w.ContainerRunning {
return nil
}
status, err := w.containerStatus()
if err.HasError() {
@@ -249,9 +253,11 @@ 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)