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,23 +3,25 @@ package main
import ( import (
"errors" "errors"
"strings" "strings"
"time"
) )
type GlobalConfiguration struct { type GlobalConfiguration struct {
Port string Port string
GraceTimeOut int64 GraceTimeOut int64
AccessLogsFile string AccessLogsFile string
TraefikLogsFile string TraefikLogsFile string
CertFile, KeyFile string CertFile, KeyFile string
LogLevel string LogLevel string
Docker *DockerProvider BackendsThrottleDuration time.Duration
File *FileProvider Docker *DockerProvider
Web *WebProvider File *FileProvider
Marathon *MarathonProvider Web *WebProvider
Consul *ConsulProvider Marathon *MarathonProvider
Etcd *EtcdProvider Consul *ConsulProvider
Zookeeper *ZookepperProvider Etcd *EtcdProvider
Boltdb *BoltDbProvider Zookeeper *ZookepperProvider
Boltdb *BoltDbProvider
} }
func NewGlobalConfiguration() *GlobalConfiguration { func NewGlobalConfiguration() *GlobalConfiguration {
@ -28,6 +30,7 @@ func NewGlobalConfiguration() *GlobalConfiguration {
globalConfiguration.Port = ":80" globalConfiguration.Port = ":80"
globalConfiguration.GraceTimeOut = 10 globalConfiguration.GraceTimeOut = 10
globalConfiguration.LogLevel = "ERROR" globalConfiguration.LogLevel = "ERROR"
globalConfiguration.BackendsThrottleDuration = time.Duration(2 * time.Second)
return globalConfiguration return globalConfiguration
} }

View file

@ -107,6 +107,16 @@ For example:
# #
# CertFile = "traefik.crt" # CertFile = "traefik.crt"
# KeyFile = "traefik.key" # 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 configurationRouter *mux.Router
var configurationChan = make(chan configMessage, 10) var configurationChan = make(chan configMessage, 10)
defer close(configurationChan) defer close(configurationChan)
var configurationChanValidated = make(chan configMessage, 10)
defer close(configurationChanValidated)
var sigs = make(chan os.Signal, 1) var sigs = make(chan os.Signal, 1)
defer close(sigs) defer close(sigs)
var stopChan = make(chan bool) var stopChan = make(chan bool)
@ -96,10 +98,33 @@ func main() {
// listen new configurations from providers // listen new configurations from providers
go func() { go func() {
lastReceivedConfiguration := time.Unix(0, 0)
lastConfigs := make(map[string]*configMessage)
for { for {
configMsg := <-configurationChan configMsg := <-configurationChan
log.Infof("Configuration receveived from provider %s: %#v", configMsg.providerName, configMsg.configuration) 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 { if configMsg.configuration == nil {
log.Info("Skipping empty configuration") log.Info("Skipping empty configuration")
} else if reflect.DeepEqual(currentConfigurations[configMsg.providerName], configMsg.configuration) { } else if reflect.DeepEqual(currentConfigurations[configMsg.providerName], configMsg.configuration) {
@ -120,7 +145,7 @@ func main() {
newsrv := prepareServer(configurationRouter, globalConfiguration, oldServer, loggerMiddleware, metrics) newsrv := prepareServer(configurationRouter, globalConfiguration, oldServer, loggerMiddleware, metrics)
go startServer(newsrv, globalConfiguration) go startServer(newsrv, globalConfiguration)
srv = newsrv srv = newsrv
time.Sleep(2 * time.Second) time.Sleep(1 * time.Second)
if oldServer != nil { if oldServer != nil {
log.Info("Stopping old server") log.Info("Stopping old server")
oldServer.Close() oldServer.Close()

View file

@ -44,6 +44,16 @@
# CertFile = "traefik.crt" # CertFile = "traefik.crt"
# KeyFile = "traefik.key" # 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 # Web configuration backend
################################################################ ################################################################