|
|
@@ -22,24 +22,16 @@ import (
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
-const (
|
|
|
- // amount of time an endpoint will be held in a failed
|
|
|
- // state before being reconsidered for proxied requests
|
|
|
- endpointFailureWait = 5 * time.Second
|
|
|
-
|
|
|
- // how often the proxy will attempt to refresh its set of endpoints
|
|
|
- refreshEndpoints = 30 * time.Second
|
|
|
-)
|
|
|
-
|
|
|
-func newDirector(urlsFunc GetProxyURLs) *director {
|
|
|
+func newDirector(urlsFunc GetProxyURLs, failureWait time.Duration, refreshInterval time.Duration) *director {
|
|
|
d := &director{
|
|
|
- uf: urlsFunc,
|
|
|
+ uf: urlsFunc,
|
|
|
+ failureWait: failureWait,
|
|
|
}
|
|
|
d.refresh()
|
|
|
go func() {
|
|
|
for {
|
|
|
select {
|
|
|
- case <-time.After(refreshEndpoints):
|
|
|
+ case <-time.After(refreshInterval):
|
|
|
d.refresh()
|
|
|
}
|
|
|
}
|
|
|
@@ -49,8 +41,10 @@ func newDirector(urlsFunc GetProxyURLs) *director {
|
|
|
|
|
|
type director struct {
|
|
|
sync.Mutex
|
|
|
- ep []*endpoint
|
|
|
- uf GetProxyURLs
|
|
|
+ ep []*endpoint
|
|
|
+ uf GetProxyURLs
|
|
|
+ failureWait time.Duration
|
|
|
+ refreshInterval time.Duration
|
|
|
}
|
|
|
|
|
|
func (d *director) refresh() {
|
|
|
@@ -64,7 +58,7 @@ func (d *director) refresh() {
|
|
|
log.Printf("proxy: upstream URL invalid: %v", err)
|
|
|
continue
|
|
|
}
|
|
|
- endpoints = append(endpoints, newEndpoint(*uu))
|
|
|
+ endpoints = append(endpoints, newEndpoint(*uu, d.failureWait))
|
|
|
}
|
|
|
|
|
|
// shuffle array to avoid connections being "stuck" to a single endpoint
|
|
|
@@ -89,11 +83,11 @@ func (d *director) endpoints() []*endpoint {
|
|
|
return filtered
|
|
|
}
|
|
|
|
|
|
-func newEndpoint(u url.URL) *endpoint {
|
|
|
+func newEndpoint(u url.URL, failureWait time.Duration) *endpoint {
|
|
|
ep := endpoint{
|
|
|
URL: u,
|
|
|
Available: true,
|
|
|
- failFunc: timedUnavailabilityFunc(endpointFailureWait),
|
|
|
+ failFunc: timedUnavailabilityFunc(failureWait),
|
|
|
}
|
|
|
|
|
|
return &ep
|