One goroutine for each backend + fix typo
This commit is contained in:
parent
4a8f032304
commit
99ffc26d40
2 changed files with 43 additions and 29 deletions
|
@ -1,6 +1,7 @@
|
|||
package healthcheck
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/containous/traefik/log"
|
||||
"github.com/vulcand/oxy/roundrobin"
|
||||
"net/http"
|
||||
|
@ -16,7 +17,6 @@ var once sync.Once
|
|||
func GetHealthCheck() *HealthCheck {
|
||||
once.Do(func() {
|
||||
singleton = newHealthCheck()
|
||||
singleton.execute()
|
||||
})
|
||||
return singleton
|
||||
}
|
||||
|
@ -33,6 +33,7 @@ var launch = false
|
|||
//HealthCheck struct
|
||||
type HealthCheck struct {
|
||||
Backends map[string]*BackendHealthCheck
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
type loadBalancer interface {
|
||||
|
@ -42,7 +43,7 @@ type loadBalancer interface {
|
|||
}
|
||||
|
||||
func newHealthCheck() *HealthCheck {
|
||||
return &HealthCheck{make(map[string]*BackendHealthCheck)}
|
||||
return &HealthCheck{make(map[string]*BackendHealthCheck), nil}
|
||||
}
|
||||
|
||||
// NewBackendHealthCheck Instantiate a new BackendHealthCheck
|
||||
|
@ -53,14 +54,25 @@ func NewBackendHealthCheck(URL string, lb loadBalancer) *BackendHealthCheck {
|
|||
//SetBackendsConfiguration set backends configuration
|
||||
func (hc *HealthCheck) SetBackendsConfiguration(backends map[string]*BackendHealthCheck) {
|
||||
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() {
|
||||
ticker := time.NewTicker(time.Second * 30)
|
||||
go func() {
|
||||
for t := range ticker.C {
|
||||
func (hc *HealthCheck) execute(ctx context.Context) {
|
||||
for backendID, backend := range hc.Backends {
|
||||
log.Debugf("Refreshing Healthcheck %s for backend %s ", t.String(), backendID)
|
||||
go func(backendID string, backend *BackendHealthCheck) {
|
||||
for {
|
||||
ticker := time.NewTicker(time.Second * 30)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Debugf("Refreshing All Healthcheck goroutines")
|
||||
return
|
||||
case <-ticker.C:
|
||||
log.Debugf("Refreshing Healthcheck for backend %s ", backendID)
|
||||
enabledURLs := backend.lb.Servers()
|
||||
var newDisabledURLs []*url.URL
|
||||
for _, url := range backend.DisabledURLs {
|
||||
|
@ -80,9 +92,11 @@ func (hc *HealthCheck) execute() {
|
|||
backend.DisabledURLs = append(backend.DisabledURLs, url)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}()
|
||||
}(backendID, backend)
|
||||
}
|
||||
}
|
||||
|
||||
func testHealth(serverURL *url.URL, checkURL string) bool {
|
||||
|
|
|
@ -553,7 +553,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
|
|||
|
||||
backends := map[string]http.Handler{}
|
||||
|
||||
backendsHealcheck := map[string]*healthcheck.BackendHealthCheck{}
|
||||
backendsHealthcheck := map[string]*healthcheck.BackendHealthCheck{}
|
||||
|
||||
backend2FrontendMap := map[string]string{}
|
||||
for _, configuration := range configurations {
|
||||
|
@ -655,7 +655,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
|
|||
continue frontend
|
||||
}
|
||||
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:
|
||||
|
@ -681,7 +681,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
|
|||
}
|
||||
}
|
||||
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
|
||||
|
@ -745,7 +745,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
|
|||
}
|
||||
}
|
||||
}
|
||||
healthcheck.GetHealthCheck().SetBackendsConfiguration(backendsHealcheck)
|
||||
healthcheck.GetHealthCheck().SetBackendsConfiguration(backendsHealthcheck)
|
||||
middlewares.SetBackend2FrontendMap(&backend2FrontendMap)
|
||||
//sort routes
|
||||
for _, serverEntryPoint := range serverEntryPoints {
|
||||
|
|
Loading…
Reference in a new issue