2016-11-26 18:48:49 +00:00
|
|
|
package healthcheck
|
|
|
|
|
|
|
|
import (
|
2016-11-30 21:49:57 +00:00
|
|
|
"context"
|
2016-11-26 18:48:49 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2017-01-31 21:55:02 +00:00
|
|
|
|
|
|
|
"github.com/containous/traefik/log"
|
|
|
|
"github.com/containous/traefik/safe"
|
|
|
|
"github.com/vulcand/oxy/roundrobin"
|
2016-11-26 18:48:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var singleton *HealthCheck
|
|
|
|
var once sync.Once
|
|
|
|
|
|
|
|
// GetHealthCheck Get HealtchCheck Singleton
|
|
|
|
func GetHealthCheck() *HealthCheck {
|
|
|
|
once.Do(func() {
|
|
|
|
singleton = newHealthCheck()
|
|
|
|
})
|
|
|
|
return singleton
|
|
|
|
}
|
|
|
|
|
|
|
|
// BackendHealthCheck HealthCheck configuration for a backend
|
|
|
|
type BackendHealthCheck struct {
|
|
|
|
URL string
|
|
|
|
DisabledURLs []*url.URL
|
|
|
|
lb loadBalancer
|
|
|
|
}
|
|
|
|
|
|
|
|
var launch = false
|
|
|
|
|
|
|
|
//HealthCheck struct
|
|
|
|
type HealthCheck struct {
|
|
|
|
Backends map[string]*BackendHealthCheck
|
2016-11-30 21:49:57 +00:00
|
|
|
cancel context.CancelFunc
|
2016-11-26 18:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type loadBalancer interface {
|
|
|
|
RemoveServer(u *url.URL) error
|
|
|
|
UpsertServer(u *url.URL, options ...roundrobin.ServerOption) error
|
|
|
|
Servers() []*url.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
func newHealthCheck() *HealthCheck {
|
2016-11-30 21:49:57 +00:00
|
|
|
return &HealthCheck{make(map[string]*BackendHealthCheck), nil}
|
2016-11-26 18:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBackendHealthCheck Instantiate a new BackendHealthCheck
|
|
|
|
func NewBackendHealthCheck(URL string, lb loadBalancer) *BackendHealthCheck {
|
|
|
|
return &BackendHealthCheck{URL, nil, lb}
|
|
|
|
}
|
|
|
|
|
2016-11-29 18:30:51 +00:00
|
|
|
//SetBackendsConfiguration set backends configuration
|
2017-01-31 21:55:02 +00:00
|
|
|
func (hc *HealthCheck) SetBackendsConfiguration(backends map[string]*BackendHealthCheck, parentCtx context.Context) {
|
2016-11-26 18:48:49 +00:00
|
|
|
hc.Backends = backends
|
2016-11-30 21:49:57 +00:00
|
|
|
if hc.cancel != nil {
|
|
|
|
hc.cancel()
|
|
|
|
}
|
2017-01-31 21:55:02 +00:00
|
|
|
ctx, cancel := context.WithCancel(parentCtx)
|
2016-11-30 21:49:57 +00:00
|
|
|
hc.cancel = cancel
|
|
|
|
hc.execute(ctx)
|
2016-11-26 18:48:49 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 21:49:57 +00:00
|
|
|
func (hc *HealthCheck) execute(ctx context.Context) {
|
|
|
|
for backendID, backend := range hc.Backends {
|
2017-01-31 21:55:02 +00:00
|
|
|
currentBackend := backend
|
|
|
|
currentBackendID := backendID
|
|
|
|
safe.Go(func() {
|
2016-11-30 21:49:57 +00:00
|
|
|
for {
|
|
|
|
ticker := time.NewTicker(time.Second * 30)
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-01-31 21:55:02 +00:00
|
|
|
log.Debugf("Stopping all current Healthcheck goroutines")
|
2016-11-30 21:49:57 +00:00
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2017-01-31 21:55:02 +00:00
|
|
|
log.Debugf("Refreshing Healthcheck for currentBackend %s ", currentBackendID)
|
|
|
|
enabledURLs := currentBackend.lb.Servers()
|
2016-11-30 21:49:57 +00:00
|
|
|
var newDisabledURLs []*url.URL
|
2017-01-31 21:55:02 +00:00
|
|
|
for _, url := range currentBackend.DisabledURLs {
|
|
|
|
if checkHealth(url, currentBackend.URL) {
|
|
|
|
log.Debugf("HealthCheck is up [%s]: Upsert in server list", url.String())
|
|
|
|
currentBackend.lb.UpsertServer(url, roundrobin.Weight(1))
|
2016-11-30 21:49:57 +00:00
|
|
|
} else {
|
|
|
|
newDisabledURLs = append(newDisabledURLs, url)
|
|
|
|
}
|
2016-11-26 18:48:49 +00:00
|
|
|
}
|
2017-01-31 21:55:02 +00:00
|
|
|
currentBackend.DisabledURLs = newDisabledURLs
|
2016-11-26 18:48:49 +00:00
|
|
|
|
2016-11-30 21:49:57 +00:00
|
|
|
for _, url := range enabledURLs {
|
2017-01-31 21:55:02 +00:00
|
|
|
if !checkHealth(url, currentBackend.URL) {
|
|
|
|
log.Debugf("HealthCheck has failed [%s]: Remove from server list", url.String())
|
|
|
|
currentBackend.lb.RemoveServer(url)
|
|
|
|
currentBackend.DisabledURLs = append(currentBackend.DisabledURLs, url)
|
2016-11-30 21:49:57 +00:00
|
|
|
}
|
2016-11-26 18:48:49 +00:00
|
|
|
}
|
2016-11-30 21:49:57 +00:00
|
|
|
|
2016-11-26 18:48:49 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-31 21:55:02 +00:00
|
|
|
})
|
2016-11-30 21:49:57 +00:00
|
|
|
}
|
2016-11-26 18:48:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 21:55:02 +00:00
|
|
|
func checkHealth(serverURL *url.URL, checkURL string) bool {
|
2016-11-30 21:48:09 +00:00
|
|
|
timeout := time.Duration(5 * time.Second)
|
|
|
|
client := http.Client{
|
|
|
|
Timeout: timeout,
|
|
|
|
}
|
|
|
|
resp, err := client.Get(serverURL.String() + checkURL)
|
2016-11-26 18:48:49 +00:00
|
|
|
if err != nil || resp.StatusCode != 200 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|