diff options
author | 2017-04-24 20:37:43 +0100 | |
---|---|---|
committer | 2017-04-24 20:37:43 +0100 | |
commit | 003b1bf678f6fc1d551fed5184adccde2137e86f (patch) | |
tree | f8d3af6f48ef9d73c841095cf3c04649b11a1fac /middleware/proxy/upstream.go | |
parent | bfa18470e53c5058237e02b56c97c5d0a2cb47be (diff) | |
download | coredns-003b1bf678f6fc1d551fed5184adccde2137e86f.tar.gz coredns-003b1bf678f6fc1d551fed5184adccde2137e86f.tar.zst coredns-003b1bf678f6fc1d551fed5184adccde2137e86f.zip |
Fix health race (#645)
* Revert "middleware/proxy: Make Unhealthy a pointer (#615)"
This reverts commit acbf522cebdcd53c26d153c1d9267d709ba75f64.
* middleware/proxy: add proper locking
This add the proper locking around `Unhealthy`.
Diffstat (limited to '')
-rw-r--r-- | middleware/proxy/upstream.go | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/middleware/proxy/upstream.go b/middleware/proxy/upstream.go index a69ebe275..278b08d0f 100644 --- a/middleware/proxy/upstream.go +++ b/middleware/proxy/upstream.go @@ -84,11 +84,13 @@ func NewStaticUpstreams(c *caddyfile.Dispenser) ([]Upstream, error) { Conns: 0, Fails: 0, FailTimeout: upstream.FailTimeout, - Unhealthy: newBool(), + Unhealthy: false, CheckDown: func(upstream *staticUpstream) UpstreamHostDownFunc { return func(uh *UpstreamHost) bool { - if *uh.Unhealthy { + uh.checkMu.Lock() + defer uh.checkMu.Unlock() + if uh.Unhealthy { return true } @@ -251,19 +253,22 @@ func (u *staticUpstream) healthCheck() { hostURL := "http://" + net.JoinHostPort(checkHostName, checkPort) + u.HealthCheck.Path + host.checkMu.Lock() + defer host.checkMu.Unlock() + if r, err := http.Get(hostURL); err == nil { io.Copy(ioutil.Discard, r.Body) r.Body.Close() if r.StatusCode < 200 || r.StatusCode >= 400 { log.Printf("[WARNING] Health check URL %s returned HTTP code %d\n", hostURL, r.StatusCode) - *host.Unhealthy = true + host.Unhealthy = true } else { - *host.Unhealthy = false + host.Unhealthy = false } } else { log.Printf("[WARNING] Health check probe failed: %v\n", err) - *host.Unhealthy = true + host.Unhealthy = true } } } @@ -338,9 +343,3 @@ func (u *staticUpstream) IsAllowedDomain(name string) bool { } func (u *staticUpstream) Exchanger() Exchanger { return u.ex } - -func newBool() *bool { - b := new(bool) - *b = false - return b -} |