traefik/healthcheck/healthcheck.go

113 lines
2.7 KiB
Go
Raw Normal View History

2016-11-26 18:48:49 +00:00
package healthcheck
import (
"context"
2016-11-26 18:48:49 +00:00
"github.com/containous/traefik/log"
"github.com/vulcand/oxy/roundrobin"
"net/http"
"net/url"
"sync"
"time"
)
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
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 {
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
2016-11-26 18:48:49 +00:00
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)
2016-11-26 18:48:49 +00:00
}
func (hc *HealthCheck) execute(ctx context.Context) {
for backendID, backend := range hc.Backends {
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 {
if testHealth(url, backend.URL) {
log.Debugf("Upsert %s", url.String())
backend.lb.UpsertServer(url, roundrobin.Weight(1))
} else {
newDisabledURLs = append(newDisabledURLs, url)
}
2016-11-26 18:48:49 +00:00
}
backend.DisabledURLs = newDisabledURLs
2016-11-26 18:48:49 +00:00
for _, url := range enabledURLs {
if !testHealth(url, backend.URL) {
log.Debugf("Remove %s", url.String())
backend.lb.RemoveServer(url)
backend.DisabledURLs = append(backend.DisabledURLs, url)
}
2016-11-26 18:48:49 +00:00
}
2016-11-26 18:48:49 +00:00
}
}
}(backendID, backend)
}
2016-11-26 18:48:49 +00:00
}
func testHealth(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
}