mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 00:38:33 +02:00
routes in loadbalance pool no longer listed in ls-route and its API, the loadbalancer is listed instead. improved context handling and grateful shutdown
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
"github.com/yusing/go-proxy/internal/net/types"
|
||||
)
|
||||
|
||||
@@ -15,9 +15,9 @@ type HTTPHealthMonitor struct {
|
||||
pinger *http.Client
|
||||
}
|
||||
|
||||
func NewHTTPHealthMonitor(ctx context.Context, name string, url types.URL, config HealthCheckConfig) HealthMonitor {
|
||||
func NewHTTPHealthMonitor(task common.Task, url types.URL, config HealthCheckConfig) HealthMonitor {
|
||||
mon := new(HTTPHealthMonitor)
|
||||
mon.monitor = newMonitor(ctx, name, url, &config, mon.checkHealth)
|
||||
mon.monitor = newMonitor(task, url, &config, mon.checkHealth)
|
||||
mon.pinger = &http.Client{Timeout: config.Timeout}
|
||||
if config.UseGet {
|
||||
mon.method = http.MethodGet
|
||||
@@ -29,7 +29,7 @@ func NewHTTPHealthMonitor(ctx context.Context, name string, url types.URL, confi
|
||||
|
||||
func (mon *HTTPHealthMonitor) checkHealth() (healthy bool, detail string, err error) {
|
||||
req, reqErr := http.NewRequestWithContext(
|
||||
mon.ctx,
|
||||
mon.task.Context(),
|
||||
mon.method,
|
||||
mon.URL.String(),
|
||||
nil,
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
"github.com/yusing/go-proxy/internal/net/types"
|
||||
F "github.com/yusing/go-proxy/internal/utils/functional"
|
||||
)
|
||||
@@ -27,7 +28,7 @@ type (
|
||||
healthy atomic.Bool
|
||||
checkHealth HealthCheckFunc
|
||||
|
||||
ctx context.Context
|
||||
task common.Task
|
||||
cancel context.CancelFunc
|
||||
done chan struct{}
|
||||
|
||||
@@ -37,22 +38,18 @@ type (
|
||||
|
||||
var monMap = F.NewMapOf[string, HealthMonitor]()
|
||||
|
||||
func newMonitor(parentCtx context.Context, name string, url types.URL, config *HealthCheckConfig, healthCheckFunc HealthCheckFunc) *monitor {
|
||||
if parentCtx == nil {
|
||||
parentCtx = context.Background()
|
||||
}
|
||||
ctx, cancel := context.WithCancel(parentCtx)
|
||||
func newMonitor(task common.Task, url types.URL, config *HealthCheckConfig, healthCheckFunc HealthCheckFunc) *monitor {
|
||||
task, cancel := task.SubtaskWithCancel("Health monitor for %s", task.Name())
|
||||
mon := &monitor{
|
||||
Name: name,
|
||||
Name: task.Name(),
|
||||
URL: url.JoinPath(config.Path),
|
||||
Interval: config.Interval,
|
||||
checkHealth: healthCheckFunc,
|
||||
ctx: ctx,
|
||||
task: task,
|
||||
cancel: cancel,
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
mon.healthy.Store(true)
|
||||
monMap.Store(name, mon)
|
||||
return mon
|
||||
}
|
||||
|
||||
@@ -65,8 +62,12 @@ func IsHealthy(name string) (healthy bool, ok bool) {
|
||||
}
|
||||
|
||||
func (mon *monitor) Start() {
|
||||
defer monMap.Store(mon.Name, mon)
|
||||
defer logger.Debugf("%s health monitor started", mon)
|
||||
|
||||
go func() {
|
||||
defer close(mon.done)
|
||||
defer mon.task.Finished()
|
||||
|
||||
ok := mon.checkUpdateHealth()
|
||||
if !ok {
|
||||
@@ -78,7 +79,7 @@ func (mon *monitor) Start() {
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-mon.ctx.Done():
|
||||
case <-mon.task.Context().Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
ok = mon.checkUpdateHealth()
|
||||
@@ -92,7 +93,7 @@ func (mon *monitor) Start() {
|
||||
}
|
||||
|
||||
func (mon *monitor) Stop() {
|
||||
defer logger.Debugf("health monitor %q stopped", mon)
|
||||
defer logger.Debugf("%s health monitor stopped", mon)
|
||||
|
||||
monMap.Delete(mon.Name)
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
"github.com/yusing/go-proxy/internal/net/types"
|
||||
)
|
||||
|
||||
@@ -14,9 +14,9 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func NewRawHealthMonitor(ctx context.Context, name string, url types.URL, config HealthCheckConfig) HealthMonitor {
|
||||
func NewRawHealthMonitor(task common.Task, url types.URL, config HealthCheckConfig) HealthMonitor {
|
||||
mon := new(RawHealthMonitor)
|
||||
mon.monitor = newMonitor(ctx, name, url, &config, mon.checkAvail)
|
||||
mon.monitor = newMonitor(task, url, &config, mon.checkAvail)
|
||||
mon.dialer = &net.Dialer{
|
||||
Timeout: config.Timeout,
|
||||
FallbackDelay: -1,
|
||||
@@ -25,7 +25,7 @@ func NewRawHealthMonitor(ctx context.Context, name string, url types.URL, config
|
||||
}
|
||||
|
||||
func (mon *RawHealthMonitor) checkAvail() (avail bool, detail string, err error) {
|
||||
conn, dialErr := mon.dialer.DialContext(mon.ctx, mon.URL.Scheme, mon.URL.Host)
|
||||
conn, dialErr := mon.dialer.DialContext(mon.task.Context(), mon.URL.Scheme, mon.URL.Host)
|
||||
if dialErr != nil {
|
||||
detail = dialErr.Error()
|
||||
/* trunk-ignore(golangci-lint/nilerr) */
|
||||
|
||||
Reference in New Issue
Block a user