One goroutine for each backend + fix typo

This commit is contained in:
Julien Salleyron 2016-11-30 22:49:57 +01:00
parent 4a8f032304
commit 99ffc26d40
2 changed files with 43 additions and 29 deletions

View file

@ -1,6 +1,7 @@
package healthcheck package healthcheck
import ( import (
"context"
"github.com/containous/traefik/log" "github.com/containous/traefik/log"
"github.com/vulcand/oxy/roundrobin" "github.com/vulcand/oxy/roundrobin"
"net/http" "net/http"
@ -16,7 +17,6 @@ var once sync.Once
func GetHealthCheck() *HealthCheck { func GetHealthCheck() *HealthCheck {
once.Do(func() { once.Do(func() {
singleton = newHealthCheck() singleton = newHealthCheck()
singleton.execute()
}) })
return singleton return singleton
} }
@ -33,6 +33,7 @@ var launch = false
//HealthCheck struct //HealthCheck struct
type HealthCheck struct { type HealthCheck struct {
Backends map[string]*BackendHealthCheck Backends map[string]*BackendHealthCheck
cancel context.CancelFunc
} }
type loadBalancer interface { type loadBalancer interface {
@ -42,7 +43,7 @@ type loadBalancer interface {
} }
func newHealthCheck() *HealthCheck { func newHealthCheck() *HealthCheck {
return &HealthCheck{make(map[string]*BackendHealthCheck)} return &HealthCheck{make(map[string]*BackendHealthCheck), nil}
} }
// NewBackendHealthCheck Instantiate a new BackendHealthCheck // NewBackendHealthCheck Instantiate a new BackendHealthCheck
@ -53,36 +54,49 @@ func NewBackendHealthCheck(URL string, lb loadBalancer) *BackendHealthCheck {
//SetBackendsConfiguration set backends configuration //SetBackendsConfiguration set backends configuration
func (hc *HealthCheck) SetBackendsConfiguration(backends map[string]*BackendHealthCheck) { func (hc *HealthCheck) SetBackendsConfiguration(backends map[string]*BackendHealthCheck) {
hc.Backends = backends hc.Backends = backends
if hc.cancel != nil {
hc.cancel()
}
ctx, cancel := context.WithCancel(context.Background())
hc.cancel = cancel
hc.execute(ctx)
} }
func (hc *HealthCheck) execute() { func (hc *HealthCheck) execute(ctx context.Context) {
ticker := time.NewTicker(time.Second * 30) for backendID, backend := range hc.Backends {
go func() { go func(backendID string, backend *BackendHealthCheck) {
for t := range ticker.C { for {
for backendID, backend := range hc.Backends { ticker := time.NewTicker(time.Second * 30)
log.Debugf("Refreshing Healthcheck %s for backend %s ", t.String(), backendID) select {
enabledURLs := backend.lb.Servers() case <-ctx.Done():
var newDisabledURLs []*url.URL log.Debugf("Refreshing All Healthcheck goroutines")
for _, url := range backend.DisabledURLs { return
if testHealth(url, backend.URL) { case <-ticker.C:
log.Debugf("Upsert %s", url.String()) log.Debugf("Refreshing Healthcheck for backend %s ", backendID)
backend.lb.UpsertServer(url, roundrobin.Weight(1)) enabledURLs := backend.lb.Servers()
} else { var newDisabledURLs []*url.URL
newDisabledURLs = append(newDisabledURLs, url) for _, url := range backend.DisabledURLs {
if testHealth(url, backend.URL) {
log.Debugf("Upsert %s", url.String())
backend.lb.UpsertServer(url, roundrobin.Weight(1))
} else {
newDisabledURLs = append(newDisabledURLs, url)
}
} }
} backend.DisabledURLs = newDisabledURLs
backend.DisabledURLs = newDisabledURLs
for _, url := range enabledURLs { for _, url := range enabledURLs {
if !testHealth(url, backend.URL) { if !testHealth(url, backend.URL) {
log.Debugf("Remove %s", url.String()) log.Debugf("Remove %s", url.String())
backend.lb.RemoveServer(url) backend.lb.RemoveServer(url)
backend.DisabledURLs = append(backend.DisabledURLs, url) backend.DisabledURLs = append(backend.DisabledURLs, url)
}
} }
} }
} }
} }(backendID, backend)
}() }
} }
func testHealth(serverURL *url.URL, checkURL string) bool { func testHealth(serverURL *url.URL, checkURL string) bool {

View file

@ -553,7 +553,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
backends := map[string]http.Handler{} backends := map[string]http.Handler{}
backendsHealcheck := map[string]*healthcheck.BackendHealthCheck{} backendsHealthcheck := map[string]*healthcheck.BackendHealthCheck{}
backend2FrontendMap := map[string]string{} backend2FrontendMap := map[string]string{}
for _, configuration := range configurations { for _, configuration := range configurations {
@ -655,7 +655,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
continue frontend continue frontend
} }
if configuration.Backends[frontend.Backend].HealthCheck != nil { if configuration.Backends[frontend.Backend].HealthCheck != nil {
backendsHealcheck[frontend.Backend] = healthcheck.NewBackendHealthCheck(configuration.Backends[frontend.Backend].HealthCheck.URL, rebalancer) backendsHealthcheck[frontend.Backend] = healthcheck.NewBackendHealthCheck(configuration.Backends[frontend.Backend].HealthCheck.URL, rebalancer)
} }
} }
case types.Wrr: case types.Wrr:
@ -681,7 +681,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
} }
} }
if configuration.Backends[frontend.Backend].HealthCheck != nil { if configuration.Backends[frontend.Backend].HealthCheck != nil {
backendsHealcheck[frontend.Backend] = healthcheck.NewBackendHealthCheck(configuration.Backends[frontend.Backend].HealthCheck.URL, rr) backendsHealthcheck[frontend.Backend] = healthcheck.NewBackendHealthCheck(configuration.Backends[frontend.Backend].HealthCheck.URL, rr)
} }
} }
maxConns := configuration.Backends[frontend.Backend].MaxConn maxConns := configuration.Backends[frontend.Backend].MaxConn
@ -745,7 +745,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
} }
} }
} }
healthcheck.GetHealthCheck().SetBackendsConfiguration(backendsHealcheck) healthcheck.GetHealthCheck().SetBackendsConfiguration(backendsHealthcheck)
middlewares.SetBackend2FrontendMap(&backend2FrontendMap) middlewares.SetBackend2FrontendMap(&backend2FrontendMap)
//sort routes //sort routes
for _, serverEntryPoint := range serverEntryPoints { for _, serverEntryPoint := range serverEntryPoints {