This commit is contained in:
emile 2015-10-08 17:56:45 +02:00 committed by Vincent Demeester
parent fd234c683c
commit 46e162e6a9
4 changed files with 64 additions and 16 deletions

View file

@ -3,6 +3,7 @@ package main
import (
"errors"
"strings"
"time"
)
type GlobalConfiguration struct {
@ -12,6 +13,7 @@ type GlobalConfiguration struct {
TraefikLogsFile string
CertFile, KeyFile string
LogLevel string
BackendsThrottleDuration time.Duration
Docker *DockerProvider
File *FileProvider
Web *WebProvider
@ -28,6 +30,7 @@ func NewGlobalConfiguration() *GlobalConfiguration {
globalConfiguration.Port = ":80"
globalConfiguration.GraceTimeOut = 10
globalConfiguration.LogLevel = "ERROR"
globalConfiguration.BackendsThrottleDuration = time.Duration(2 * time.Second)
return globalConfiguration
}

View file

@ -107,6 +107,16 @@ For example:
#
# CertFile = "traefik.crt"
# KeyFile = "traefik.key"
# Backends throttle duration: minimum duration between 2 events from providers
# before applying a new configuration. It avoids unnecessary reloads if multiples events
# are sent in a short amount of time.
#
# Optional
# Default: "2s"
#
# BackendsThrottleDuration = "5s"
```

View file

@ -59,6 +59,8 @@ func main() {
var configurationRouter *mux.Router
var configurationChan = make(chan configMessage, 10)
defer close(configurationChan)
var configurationChanValidated = make(chan configMessage, 10)
defer close(configurationChanValidated)
var sigs = make(chan os.Signal, 1)
defer close(sigs)
var stopChan = make(chan bool)
@ -96,10 +98,33 @@ func main() {
// listen new configurations from providers
go func() {
lastReceivedConfiguration := time.Unix(0, 0)
lastConfigs := make(map[string]*configMessage)
for {
configMsg := <-configurationChan
log.Infof("Configuration receveived from provider %s: %#v", configMsg.providerName, configMsg.configuration)
lastConfigs[configMsg.providerName] = &configMsg
if time.Now().After(lastReceivedConfiguration.Add(time.Duration(globalConfiguration.BackendsThrottleDuration))) {
log.Infof("Last %s config received more than %s, OK", configMsg.providerName, globalConfiguration.BackendsThrottleDuration)
// last config received more than n s ago
configurationChanValidated <- configMsg
} else {
log.Infof("Last %s config received less than %s, waiting...", configMsg.providerName, globalConfiguration.BackendsThrottleDuration)
go func() {
<-time.After(globalConfiguration.BackendsThrottleDuration)
if time.Now().After(lastReceivedConfiguration.Add(time.Duration(globalConfiguration.BackendsThrottleDuration))) {
log.Infof("Waited for %s config, OK", configMsg.providerName)
configurationChanValidated <- *lastConfigs[configMsg.providerName]
}
}()
}
lastReceivedConfiguration = time.Now()
}
}()
go func() {
for {
configMsg := <-configurationChanValidated
log.Debugf("Configuration %s", spew.Sdump(configMsg.configuration))
if configMsg.configuration == nil {
log.Info("Skipping empty configuration")
} else if reflect.DeepEqual(currentConfigurations[configMsg.providerName], configMsg.configuration) {
@ -120,7 +145,7 @@ func main() {
newsrv := prepareServer(configurationRouter, globalConfiguration, oldServer, loggerMiddleware, metrics)
go startServer(newsrv, globalConfiguration)
srv = newsrv
time.Sleep(2 * time.Second)
time.Sleep(1 * time.Second)
if oldServer != nil {
log.Info("Stopping old server")
oldServer.Close()

View file

@ -44,6 +44,16 @@
# CertFile = "traefik.crt"
# KeyFile = "traefik.key"
# Backends throttle duration: minimum duration between 2 events from providers
# before applying a new configuration. It avoids unnecessary reloads if multiples events
# are sent in a short amount of time.
#
# Optional
# Default: "2s"
#
# BackendsThrottleDuration = "5s"
################################################################
# Web configuration backend
################################################################