mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 08:48:32 +02:00
Improved healthcheck, idlewatcher support for loadbalanced routes, bug fixes
This commit is contained in:
@@ -2,7 +2,6 @@ package idlewatcher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -73,15 +72,15 @@ func (w *Waker) Uptime() time.Duration {
|
||||
}
|
||||
|
||||
func (w *Waker) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(map[string]any{
|
||||
"name": w.Name(),
|
||||
"url": w.URL,
|
||||
"status": w.Status(),
|
||||
"config": health.HealthCheckConfig{
|
||||
return (&health.JSONRepresentation{
|
||||
Name: w.Name(),
|
||||
Status: w.Status(),
|
||||
Config: &health.HealthCheckConfig{
|
||||
Interval: w.IdleTimeout,
|
||||
Timeout: w.WakeTimeout,
|
||||
},
|
||||
})
|
||||
URL: w.URL,
|
||||
}).MarshalJSON()
|
||||
}
|
||||
|
||||
/* End of HealthMonitor interface */
|
||||
@@ -89,6 +88,10 @@ func (w *Waker) MarshalJSON() ([]byte, error) {
|
||||
func (w *Waker) wake(rw http.ResponseWriter, r *http.Request) (shouldNext bool) {
|
||||
w.resetIdleTimer()
|
||||
|
||||
if r.Body != nil {
|
||||
defer r.Body.Close()
|
||||
}
|
||||
|
||||
// pass through if container is ready
|
||||
if w.ready.Load() {
|
||||
return true
|
||||
@@ -115,6 +118,16 @@ func (w *Waker) wake(rw http.ResponseWriter, r *http.Request) (shouldNext bool)
|
||||
return
|
||||
}
|
||||
|
||||
select {
|
||||
case <-w.task.Context().Done():
|
||||
http.Error(rw, "Waking timed out", http.StatusGatewayTimeout)
|
||||
return
|
||||
case <-ctx.Done():
|
||||
http.Error(rw, "Waking timed out", http.StatusGatewayTimeout)
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
// wake the container and reset idle timer
|
||||
// also wait for another wake request
|
||||
w.wakeCh <- struct{}{}
|
||||
@@ -169,3 +182,8 @@ func (w *Waker) wake(rw http.ResponseWriter, r *http.Request) (shouldNext bool)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
// static HealthMonitor interface check
|
||||
func (w *Waker) _() health.HealthMonitor {
|
||||
return w
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user