mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 02:03:20 -04:00 
			
		
		
		
	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
This commit is contained in:
		
				
					committed by
					
						 Miek Gieben
						Miek Gieben
					
				
			
			
				
	
			
			
			
						parent
						
							dfc71df07d
						
					
				
				
					commit
					7dc431ada3
				
			| @@ -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. | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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 { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user