aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Michael S. Fischer <mfischer-zd@users.noreply.github.com> 2017-03-17 00:20:55 -0700
committerGravatar Miek Gieben <miek@miek.nl> 2017-03-17 07:20:55 +0000
commit7dc431ada3909aba9dfeaf64003718e0ea4d43b9 (patch)
tree04792744ba4202d44a0aa5fb8fa820805235ed56
parentdfc71df07d7a19cc0227083ce6b04eea60ecd5a0 (diff)
downloadcoredns-7dc431ada3909aba9dfeaf64003718e0ea4d43b9.tar.gz
coredns-7dc431ada3909aba9dfeaf64003718e0ea4d43b9.tar.zst
coredns-7dc431ada3909aba9dfeaf64003718e0ea4d43b9.zip
middleware/proxy: fix race; add Go 1.7 backward compatibility (#603)
* Fix race on backend health status update * Ensure test case is compatible on Go 1.7
-rw-r--r--middleware/proxy/proxy.go2
-rw-r--r--middleware/proxy/upstream.go6
-rw-r--r--test/proxy_http_health_test.go7
3 files changed, 13 insertions, 2 deletions
diff --git a/middleware/proxy/proxy.go b/middleware/proxy/proxy.go
index d21dee732..ce8b99d83 100644
--- a/middleware/proxy/proxy.go
+++ b/middleware/proxy/proxy.go
@@ -3,6 +3,7 @@ package proxy
import (
"errors"
+ "sync"
"sync/atomic"
"time"
@@ -59,6 +60,7 @@ type UpstreamHost struct {
Unhealthy bool
CheckDown UpstreamHostDownFunc
WithoutPathPrefix string
+ checkMu sync.Mutex
}
// Down checks whether the upstream host is down or not.
diff --git a/middleware/proxy/upstream.go b/middleware/proxy/upstream.go
index cabcd9ba8..c595976bc 100644
--- a/middleware/proxy/upstream.go
+++ b/middleware/proxy/upstream.go
@@ -250,7 +250,9 @@ func (u *staticUpstream) healthCheck() {
}
hostURL := "http://" + net.JoinHostPort(checkHostName, checkPort) + u.HealthCheck.Path
- host.Unhealthy = false
+
+ host.checkMu.Lock()
+ defer host.checkMu.Unlock()
if r, err := http.Get(hostURL); err == nil {
io.Copy(ioutil.Discard, r.Body)
@@ -259,6 +261,8 @@ func (u *staticUpstream) healthCheck() {
log.Printf("[WARNING] Health check URL %s returned HTTP code %d\n",
hostURL, r.StatusCode)
host.Unhealthy = true
+ } else {
+ host.Unhealthy = false
}
} else {
log.Printf("[WARNING] Health check probe failed: %v\n", err)
diff --git a/test/proxy_http_health_test.go b/test/proxy_http_health_test.go
index 9bfc3b209..b190f2b45 100644
--- a/test/proxy_http_health_test.go
+++ b/test/proxy_http_health_test.go
@@ -4,6 +4,7 @@ import (
"io"
"io/ioutil"
"log"
+ "net"
"net/http"
"net/http/httptest"
"net/url"
@@ -29,7 +30,11 @@ func TestProxyWithHTTPCheckOK(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- healthCheckPort := healthCheckURL.Port()
+ // TODO: use URL.Port() (Go 1.8+) once we've deprecated Go 1.7 support
+ var healthCheckPort string
+ if _, healthCheckPort, err = net.SplitHostPort(healthCheckURL.Host); err != nil {
+ healthCheckPort = "80"
+ }
name, rm, err := test.TempFile(".", exampleOrg)
if err != nil {