fix after review
This commit is contained in:
parent
99ffc26d40
commit
755822bf14
3 changed files with 28 additions and 23 deletions
|
@ -2,12 +2,14 @@ package healthcheck
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/containous/traefik/log"
|
|
||||||
"github.com/vulcand/oxy/roundrobin"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/containous/traefik/log"
|
||||||
|
"github.com/containous/traefik/safe"
|
||||||
|
"github.com/vulcand/oxy/roundrobin"
|
||||||
)
|
)
|
||||||
|
|
||||||
var singleton *HealthCheck
|
var singleton *HealthCheck
|
||||||
|
@ -52,54 +54,56 @@ 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, parentCtx context.Context) {
|
||||||
hc.Backends = backends
|
hc.Backends = backends
|
||||||
if hc.cancel != nil {
|
if hc.cancel != nil {
|
||||||
hc.cancel()
|
hc.cancel()
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(parentCtx)
|
||||||
hc.cancel = cancel
|
hc.cancel = cancel
|
||||||
hc.execute(ctx)
|
hc.execute(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HealthCheck) execute(ctx context.Context) {
|
func (hc *HealthCheck) execute(ctx context.Context) {
|
||||||
for backendID, backend := range hc.Backends {
|
for backendID, backend := range hc.Backends {
|
||||||
go func(backendID string, backend *BackendHealthCheck) {
|
currentBackend := backend
|
||||||
|
currentBackendID := backendID
|
||||||
|
safe.Go(func() {
|
||||||
for {
|
for {
|
||||||
ticker := time.NewTicker(time.Second * 30)
|
ticker := time.NewTicker(time.Second * 30)
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
log.Debugf("Refreshing All Healthcheck goroutines")
|
log.Debugf("Stopping all current Healthcheck goroutines")
|
||||||
return
|
return
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
log.Debugf("Refreshing Healthcheck for backend %s ", backendID)
|
log.Debugf("Refreshing Healthcheck for currentBackend %s ", currentBackendID)
|
||||||
enabledURLs := backend.lb.Servers()
|
enabledURLs := currentBackend.lb.Servers()
|
||||||
var newDisabledURLs []*url.URL
|
var newDisabledURLs []*url.URL
|
||||||
for _, url := range backend.DisabledURLs {
|
for _, url := range currentBackend.DisabledURLs {
|
||||||
if testHealth(url, backend.URL) {
|
if checkHealth(url, currentBackend.URL) {
|
||||||
log.Debugf("Upsert %s", url.String())
|
log.Debugf("HealthCheck is up [%s]: Upsert in server list", url.String())
|
||||||
backend.lb.UpsertServer(url, roundrobin.Weight(1))
|
currentBackend.lb.UpsertServer(url, roundrobin.Weight(1))
|
||||||
} else {
|
} else {
|
||||||
newDisabledURLs = append(newDisabledURLs, url)
|
newDisabledURLs = append(newDisabledURLs, url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
backend.DisabledURLs = newDisabledURLs
|
currentBackend.DisabledURLs = newDisabledURLs
|
||||||
|
|
||||||
for _, url := range enabledURLs {
|
for _, url := range enabledURLs {
|
||||||
if !testHealth(url, backend.URL) {
|
if !checkHealth(url, currentBackend.URL) {
|
||||||
log.Debugf("Remove %s", url.String())
|
log.Debugf("HealthCheck has failed [%s]: Remove from server list", url.String())
|
||||||
backend.lb.RemoveServer(url)
|
currentBackend.lb.RemoveServer(url)
|
||||||
backend.DisabledURLs = append(backend.DisabledURLs, url)
|
currentBackend.DisabledURLs = append(currentBackend.DisabledURLs, url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}(backendID, backend)
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testHealth(serverURL *url.URL, checkURL string) bool {
|
func checkHealth(serverURL *url.URL, checkURL string) bool {
|
||||||
timeout := time.Duration(5 * time.Second)
|
timeout := time.Duration(5 * time.Second)
|
||||||
client := http.Client{
|
client := http.Client{
|
||||||
Timeout: timeout,
|
Timeout: timeout,
|
||||||
|
|
|
@ -745,7 +745,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
healthcheck.GetHealthCheck().SetBackendsConfiguration(backendsHealthcheck)
|
healthcheck.GetHealthCheck().SetBackendsConfiguration(backendsHealthcheck, server.routinesPool.Ctx())
|
||||||
middlewares.SetBackend2FrontendMap(&backend2FrontendMap)
|
middlewares.SetBackend2FrontendMap(&backend2FrontendMap)
|
||||||
//sort routes
|
//sort routes
|
||||||
for _, serverEntryPoint := range serverEntryPoints {
|
for _, serverEntryPoint := range serverEntryPoints {
|
||||||
|
|
|
@ -4,10 +4,11 @@ import (
|
||||||
"encoding"
|
"encoding"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/docker/libkv/store"
|
|
||||||
"github.com/ryanuber/go-glob"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/docker/libkv/store"
|
||||||
|
"github.com/ryanuber/go-glob"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Backend holds backend configuration.
|
// Backend holds backend configuration.
|
||||||
|
@ -36,7 +37,7 @@ type CircuitBreaker struct {
|
||||||
Expression string `json:"expression,omitempty"`
|
Expression string `json:"expression,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// HealthCheck holds healthchk configuration
|
// HealthCheck holds HealthCheck configuration
|
||||||
type HealthCheck struct {
|
type HealthCheck struct {
|
||||||
URL string `json:"url,omitempty"`
|
URL string `json:"url,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue