mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-23 17:41:05 +01:00
- These changes makes the API incombatible with previous versions - Added new types for error handling, success responses, and health checks. - Updated health check logic to utilize the new types for better clarity and structure. - Refactored existing handlers to improve response consistency and error handling. - Updated Makefile to include a new target for generating API types from Swagger. - Updated "new agent" API to respond an encrypted cert pair
129 lines
2.5 KiB
Go
129 lines
2.5 KiB
Go
package idlewatcher
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/yusing/go-proxy/internal/gperr"
|
|
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
|
|
"github.com/yusing/go-proxy/internal/task"
|
|
"github.com/yusing/go-proxy/internal/types"
|
|
)
|
|
|
|
// Start implements health.HealthMonitor.
|
|
func (w *Watcher) Start(parent task.Parent) gperr.Error {
|
|
w.task.OnCancel("route_cleanup", func() {
|
|
parent.Finish(w.task.FinishCause())
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// Task implements health.HealthMonitor.
|
|
func (w *Watcher) Task() *task.Task {
|
|
return w.task
|
|
}
|
|
|
|
// Finish implements health.HealthMonitor.
|
|
func (w *Watcher) Finish(reason any) {
|
|
if w.stream != nil {
|
|
w.stream.Close()
|
|
}
|
|
}
|
|
|
|
// Name implements health.HealthMonitor.
|
|
func (w *Watcher) Name() string {
|
|
return w.cfg.ContainerName()
|
|
}
|
|
|
|
// String implements health.HealthMonitor.
|
|
func (w *Watcher) String() string {
|
|
return w.Name()
|
|
}
|
|
|
|
// Uptime implements health.HealthMonitor.
|
|
func (w *Watcher) Uptime() time.Duration {
|
|
return 0
|
|
}
|
|
|
|
// Latency implements health.HealthMonitor.
|
|
func (w *Watcher) Latency() time.Duration {
|
|
return 0
|
|
}
|
|
|
|
// Status implements health.HealthMonitor.
|
|
func (w *Watcher) Status() types.HealthStatus {
|
|
state := w.state.Load()
|
|
if state.err != nil {
|
|
return types.StatusError
|
|
}
|
|
if state.ready {
|
|
return types.StatusHealthy
|
|
}
|
|
if state.status == idlewatcher.ContainerStatusRunning {
|
|
return types.StatusStarting
|
|
}
|
|
return types.StatusNapping
|
|
}
|
|
|
|
// Detail implements health.HealthMonitor.
|
|
func (w *Watcher) Detail() string {
|
|
state := w.state.Load()
|
|
if state.err != nil {
|
|
return state.err.Error()
|
|
}
|
|
if !state.ready {
|
|
return "not ready"
|
|
}
|
|
if state.status == idlewatcher.ContainerStatusRunning {
|
|
return "starting"
|
|
}
|
|
return "napping"
|
|
}
|
|
|
|
// MarshalJSON implements health.HealthMonitor.
|
|
func (w *Watcher) MarshalJSON() ([]byte, error) {
|
|
url := w.hc.URL()
|
|
if url.Port() == "0" {
|
|
url = nil
|
|
}
|
|
var detail string
|
|
if err := w.error(); err != nil {
|
|
detail = err.Error()
|
|
}
|
|
return (&types.HealthJSONRepr{
|
|
Name: w.Name(),
|
|
Status: w.Status(),
|
|
Config: dummyHealthCheckConfig,
|
|
URL: url,
|
|
Detail: detail,
|
|
}).MarshalJSON()
|
|
}
|
|
|
|
func (w *Watcher) checkUpdateState() (ready bool, err error) {
|
|
// already ready
|
|
if w.ready() {
|
|
return true, nil
|
|
}
|
|
|
|
if !w.running() {
|
|
return false, nil
|
|
}
|
|
|
|
// the new container info not yet updated
|
|
if w.hc.URL().Host == "" {
|
|
return false, nil
|
|
}
|
|
|
|
res, err := w.hc.CheckHealth()
|
|
if err != nil {
|
|
w.setError(err)
|
|
return false, err
|
|
}
|
|
|
|
if res.Healthy {
|
|
w.setReady()
|
|
return true, nil
|
|
}
|
|
w.setStarting()
|
|
return false, nil
|
|
}
|