diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2020-02-23 14:30:52 -0700 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2020-02-23 14:31:05 -0700 |
commit | 7cca291d62c910c0544f0c0169a8f0c81627e5d3 (patch) | |
tree | 1ca9cd582ec1d5ac28e71e13936ef6a30d7cc8b2 /modules/caddyhttp/reverseproxy/healthchecks.go | |
parent | e3591009dc8cf386704f8671c54b0f97c3f75b8c (diff) |
reverse_proxy: Health checks: Don't cross the streams
Fixes https://caddy.community/t/v2-health-checks-are-going-to-the-wrong-upstream/7084?u=matt
... I think
Diffstat (limited to 'modules/caddyhttp/reverseproxy/healthchecks.go')
-rw-r--r-- | modules/caddyhttp/reverseproxy/healthchecks.go | 31 |
1 files changed, 14 insertions, 17 deletions
diff --git a/modules/caddyhttp/reverseproxy/healthchecks.go b/modules/caddyhttp/reverseproxy/healthchecks.go index a3b57e1..85274c6 100644 --- a/modules/caddyhttp/reverseproxy/healthchecks.go +++ b/modules/caddyhttp/reverseproxy/healthchecks.go @@ -124,26 +124,25 @@ type CircuitBreaker interface { // h.HealthChecks.Active.stopChan is closed. func (h *Handler) activeHealthChecker() { ticker := time.NewTicker(time.Duration(h.HealthChecks.Active.Interval)) - h.doActiveHealthChecksForAllHosts() + h.doActiveHealthCheckForAllHosts() for { select { case <-ticker.C: - h.doActiveHealthChecksForAllHosts() + h.doActiveHealthCheckForAllHosts() case <-h.HealthChecks.Active.stopChan: + // TODO: consider using a Context for cancellation instead ticker.Stop() return } } } -// doActiveHealthChecksForAllHosts immediately performs a -// health checks for all hosts in the global repository. -func (h *Handler) doActiveHealthChecksForAllHosts() { - hosts.Range(func(key, value interface{}) bool { - networkAddr := key.(string) - host := value.(Host) - - go func(networkAddr string, host Host) { +// doActiveHealthCheckForAllHosts immediately performs a +// health checks for all upstream hosts configured by h. +func (h *Handler) doActiveHealthCheckForAllHosts() { + for _, upstream := range h.Upstreams { + go func(upstream *Upstream) { + networkAddr := upstream.Dial addr, err := caddy.ParseNetworkAddress(networkAddr) if err != nil { h.HealthChecks.Active.logger.Error("bad network address", @@ -165,18 +164,15 @@ func (h *Handler) doActiveHealthChecksForAllHosts() { // so use a fake Host value instead; unix sockets are usually local hostAddr = "localhost" } - err = h.doActiveHealthCheck(DialInfo{Network: addr.Network, Address: hostAddr}, hostAddr, host) + err = h.doActiveHealthCheck(DialInfo{Network: addr.Network, Address: hostAddr}, hostAddr, upstream.Host) if err != nil { h.HealthChecks.Active.logger.Error("active health check failed", zap.String("address", networkAddr), zap.Error(err), ) } - }(networkAddr, host) - - // continue to iterate all hosts - return true - }) + }(upstream) + } } // doActiveHealthCheck performs a health check to host which @@ -209,7 +205,8 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, host H u.Host = net.JoinHostPort(host, portStr) } - // attach dialing information to this request + // attach dialing information to this request - TODO: use caddy.Context's context + // so it can be canceled on config reload ctx := context.Background() ctx = context.WithValue(ctx, caddy.ReplacerCtxKey, caddy.NewReplacer()) ctx = context.WithValue(ctx, caddyhttp.VarsCtxKey, map[string]interface{}{ |