2015-09-11 14:37:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-10-19 21:29:25 +00:00
|
|
|
"crypto/tls"
|
2015-11-01 15:35:01 +00:00
|
|
|
"errors"
|
2015-09-24 15:16:13 +00:00
|
|
|
fmtlog "log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"reflect"
|
2015-11-01 15:35:01 +00:00
|
|
|
"runtime"
|
2015-09-24 15:16:13 +00:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2015-09-24 12:32:37 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-09-12 13:10:03 +00:00
|
|
|
"github.com/codegangsta/negroni"
|
2015-09-15 19:38:54 +00:00
|
|
|
"github.com/emilevauge/traefik/middlewares"
|
2015-11-01 15:35:01 +00:00
|
|
|
"github.com/emilevauge/traefik/provider"
|
|
|
|
"github.com/emilevauge/traefik/types"
|
2015-09-11 14:37:13 +00:00
|
|
|
"github.com/gorilla/mux"
|
2015-09-25 22:20:45 +00:00
|
|
|
"github.com/mailgun/manners"
|
2015-09-24 12:32:37 +00:00
|
|
|
"github.com/mailgun/oxy/cbreaker"
|
2015-09-11 14:37:13 +00:00
|
|
|
"github.com/mailgun/oxy/forward"
|
|
|
|
"github.com/mailgun/oxy/roundrobin"
|
2015-09-12 13:10:03 +00:00
|
|
|
"github.com/thoas/stats"
|
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
2015-11-24 08:12:20 +00:00
|
|
|
"sync"
|
2015-09-11 14:37:13 +00:00
|
|
|
)
|
|
|
|
|
2015-09-11 23:55:10 +00:00
|
|
|
var (
|
2015-09-22 04:16:21 +00:00
|
|
|
globalConfigFile = kingpin.Arg("conf", "Main configration file.").Default("traefik.toml").String()
|
2015-10-14 20:04:56 +00:00
|
|
|
version = kingpin.Flag("version", "Get Version.").Short('v').Bool()
|
2015-09-22 04:16:21 +00:00
|
|
|
currentConfigurations = make(configs)
|
|
|
|
metrics = stats.New()
|
|
|
|
oxyLogger = &OxyLogger{}
|
2015-09-11 23:55:10 +00:00
|
|
|
)
|
2015-09-11 14:37:13 +00:00
|
|
|
|
|
|
|
func main() {
|
2015-09-25 22:20:45 +00:00
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
2015-10-14 20:04:56 +00:00
|
|
|
kingpin.Version(Version + " built on the " + BuildDate)
|
2015-09-11 23:55:10 +00:00
|
|
|
kingpin.Parse()
|
2015-09-19 11:02:59 +00:00
|
|
|
fmtlog.SetFlags(fmtlog.Lshortfile | fmtlog.LstdFlags)
|
2015-09-25 22:20:45 +00:00
|
|
|
var srv *manners.GracefulServer
|
2015-09-11 14:37:13 +00:00
|
|
|
var configurationRouter *mux.Router
|
2015-11-01 15:35:01 +00:00
|
|
|
var configurationChan = make(chan types.ConfigMessage, 10)
|
2015-09-16 17:08:01 +00:00
|
|
|
defer close(configurationChan)
|
2015-11-01 15:35:01 +00:00
|
|
|
var configurationChanValidated = make(chan types.ConfigMessage, 10)
|
2015-10-08 15:56:45 +00:00
|
|
|
defer close(configurationChanValidated)
|
2015-09-11 14:37:13 +00:00
|
|
|
var sigs = make(chan os.Signal, 1)
|
2015-09-25 20:02:55 +00:00
|
|
|
defer close(sigs)
|
2015-09-25 22:20:45 +00:00
|
|
|
var stopChan = make(chan bool)
|
|
|
|
defer close(stopChan)
|
2015-11-01 15:35:01 +00:00
|
|
|
var providers = []provider.Provider{}
|
2015-09-11 14:37:13 +00:00
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
2015-11-24 08:12:20 +00:00
|
|
|
var serverLock sync.Mutex
|
2015-09-11 14:37:13 +00:00
|
|
|
|
|
|
|
// load global configuration
|
2015-09-24 15:16:13 +00:00
|
|
|
globalConfiguration := LoadFileConfig(*globalConfigFile)
|
2015-09-11 14:37:13 +00:00
|
|
|
|
2015-09-24 15:16:13 +00:00
|
|
|
loggerMiddleware := middlewares.NewLogger(globalConfiguration.AccessLogsFile)
|
2015-09-12 13:20:56 +00:00
|
|
|
defer loggerMiddleware.Close()
|
|
|
|
|
2015-09-11 14:37:13 +00:00
|
|
|
// logging
|
2015-09-24 15:16:13 +00:00
|
|
|
level, err := log.ParseLevel(strings.ToLower(globalConfiguration.LogLevel))
|
2015-09-11 14:37:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error getting level", err)
|
|
|
|
}
|
2015-09-24 12:32:37 +00:00
|
|
|
log.SetLevel(level)
|
2015-09-11 14:48:52 +00:00
|
|
|
|
2015-09-24 15:16:13 +00:00
|
|
|
if len(globalConfiguration.TraefikLogsFile) > 0 {
|
|
|
|
fi, err := os.OpenFile(globalConfiguration.TraefikLogsFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
2015-09-12 13:20:56 +00:00
|
|
|
defer fi.Close()
|
2015-09-11 14:37:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error opening file", err)
|
2015-09-12 13:10:03 +00:00
|
|
|
} else {
|
2015-09-24 12:32:37 +00:00
|
|
|
log.SetOutput(fi)
|
|
|
|
log.SetFormatter(&log.TextFormatter{DisableColors: true, FullTimestamp: true, DisableSorting: true})
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-24 12:32:37 +00:00
|
|
|
} else {
|
|
|
|
log.SetFormatter(&log.TextFormatter{FullTimestamp: true, DisableSorting: true})
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-10-13 12:15:32 +00:00
|
|
|
log.Debugf("Global configuration loaded %+v", globalConfiguration)
|
2015-09-24 15:16:13 +00:00
|
|
|
configurationRouter = LoadDefaultConfig(globalConfiguration)
|
2015-09-11 14:37:13 +00:00
|
|
|
|
|
|
|
// listen new configurations from providers
|
|
|
|
go func() {
|
2015-10-08 15:56:45 +00:00
|
|
|
lastReceivedConfiguration := time.Unix(0, 0)
|
2015-11-01 15:35:01 +00:00
|
|
|
lastConfigs := make(map[string]*types.ConfigMessage)
|
2015-09-11 14:37:13 +00:00
|
|
|
for {
|
2015-09-22 04:16:21 +00:00
|
|
|
configMsg := <-configurationChan
|
2015-11-01 15:35:01 +00:00
|
|
|
log.Infof("Configuration receveived from provider %s: %#v", configMsg.ProviderName, configMsg.Configuration)
|
|
|
|
lastConfigs[configMsg.ProviderName] = &configMsg
|
2015-10-17 12:14:20 +00:00
|
|
|
if time.Now().After(lastReceivedConfiguration.Add(time.Duration(globalConfiguration.ProvidersThrottleDuration))) {
|
2015-11-01 15:35:01 +00:00
|
|
|
log.Infof("Last %s config received more than %s, OK", configMsg.ProviderName, globalConfiguration.ProvidersThrottleDuration)
|
2015-10-08 15:56:45 +00:00
|
|
|
// last config received more than n s ago
|
|
|
|
configurationChanValidated <- configMsg
|
|
|
|
} else {
|
2015-11-01 15:35:01 +00:00
|
|
|
log.Infof("Last %s config received less than %s, waiting...", configMsg.ProviderName, globalConfiguration.ProvidersThrottleDuration)
|
2015-10-08 15:56:45 +00:00
|
|
|
go func() {
|
2015-10-17 12:14:20 +00:00
|
|
|
<-time.After(globalConfiguration.ProvidersThrottleDuration)
|
|
|
|
if time.Now().After(lastReceivedConfiguration.Add(time.Duration(globalConfiguration.ProvidersThrottleDuration))) {
|
2015-11-01 15:35:01 +00:00
|
|
|
log.Infof("Waited for %s config, OK", configMsg.ProviderName)
|
|
|
|
configurationChanValidated <- *lastConfigs[configMsg.ProviderName]
|
2015-10-08 15:56:45 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
lastReceivedConfiguration = time.Now()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
configMsg := <-configurationChanValidated
|
2015-11-01 15:35:01 +00:00
|
|
|
if configMsg.Configuration == nil {
|
|
|
|
log.Info("Skipping empty Configuration")
|
|
|
|
} else if reflect.DeepEqual(currentConfigurations[configMsg.ProviderName], configMsg.Configuration) {
|
2015-09-11 14:37:13 +00:00
|
|
|
log.Info("Skipping same configuration")
|
|
|
|
} else {
|
2015-09-22 04:16:21 +00:00
|
|
|
// Copy configurations to new map so we don't change current if LoadConfig fails
|
|
|
|
newConfigurations := make(configs)
|
|
|
|
for k, v := range currentConfigurations {
|
|
|
|
newConfigurations[k] = v
|
|
|
|
}
|
2015-11-01 15:35:01 +00:00
|
|
|
newConfigurations[configMsg.ProviderName] = configMsg.Configuration
|
2015-09-22 04:16:21 +00:00
|
|
|
|
|
|
|
newConfigurationRouter, err := LoadConfig(newConfigurations, globalConfiguration)
|
2015-09-24 12:32:37 +00:00
|
|
|
if err == nil {
|
2015-11-24 08:12:20 +00:00
|
|
|
serverLock.Lock()
|
2015-09-22 04:16:21 +00:00
|
|
|
currentConfigurations = newConfigurations
|
2015-09-19 11:02:59 +00:00
|
|
|
configurationRouter = newConfigurationRouter
|
2015-09-25 20:02:55 +00:00
|
|
|
oldServer := srv
|
2015-10-19 21:29:25 +00:00
|
|
|
newsrv, err := prepareServer(configurationRouter, globalConfiguration, oldServer, loggerMiddleware, metrics)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error preparing server: ", err)
|
|
|
|
}
|
2015-09-25 22:20:45 +00:00
|
|
|
go startServer(newsrv, globalConfiguration)
|
|
|
|
srv = newsrv
|
2015-10-08 15:56:45 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2015-09-25 22:20:45 +00:00
|
|
|
if oldServer != nil {
|
|
|
|
log.Info("Stopping old server")
|
|
|
|
oldServer.Close()
|
|
|
|
}
|
2015-11-24 08:12:20 +00:00
|
|
|
serverLock.Unlock()
|
2015-09-24 12:32:37 +00:00
|
|
|
} else {
|
2015-09-19 11:02:59 +00:00
|
|
|
log.Error("Error loading new configuration, aborted ", err)
|
|
|
|
}
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// configure providers
|
2015-09-24 15:16:13 +00:00
|
|
|
if globalConfiguration.Docker != nil {
|
|
|
|
providers = append(providers, globalConfiguration.Docker)
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-24 15:16:13 +00:00
|
|
|
if globalConfiguration.Marathon != nil {
|
|
|
|
providers = append(providers, globalConfiguration.Marathon)
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-24 15:16:13 +00:00
|
|
|
if globalConfiguration.File != nil {
|
|
|
|
if len(globalConfiguration.File.Filename) == 0 {
|
2015-09-11 14:37:13 +00:00
|
|
|
// no filename, setting to global config file
|
2015-09-24 15:16:13 +00:00
|
|
|
globalConfiguration.File.Filename = *globalConfigFile
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-24 15:16:13 +00:00
|
|
|
providers = append(providers, globalConfiguration.File)
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-24 15:16:13 +00:00
|
|
|
if globalConfiguration.Web != nil {
|
|
|
|
providers = append(providers, globalConfiguration.Web)
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-24 15:16:13 +00:00
|
|
|
if globalConfiguration.Consul != nil {
|
|
|
|
providers = append(providers, globalConfiguration.Consul)
|
2015-09-21 16:05:56 +00:00
|
|
|
}
|
2015-10-01 10:04:25 +00:00
|
|
|
if globalConfiguration.Etcd != nil {
|
|
|
|
providers = append(providers, globalConfiguration.Etcd)
|
|
|
|
}
|
|
|
|
if globalConfiguration.Zookeeper != nil {
|
|
|
|
providers = append(providers, globalConfiguration.Zookeeper)
|
|
|
|
}
|
|
|
|
if globalConfiguration.Boltdb != nil {
|
|
|
|
providers = append(providers, globalConfiguration.Boltdb)
|
|
|
|
}
|
2015-09-11 14:37:13 +00:00
|
|
|
|
|
|
|
// start providers
|
|
|
|
for _, provider := range providers {
|
2015-09-24 12:32:37 +00:00
|
|
|
log.Infof("Starting provider %v %+v", reflect.TypeOf(provider), provider)
|
2015-09-11 14:37:13 +00:00
|
|
|
currentProvider := provider
|
|
|
|
go func() {
|
2015-10-01 10:04:25 +00:00
|
|
|
err := currentProvider.Provide(configurationChan)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error starting provider %s", err)
|
|
|
|
}
|
2015-09-11 14:37:13 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
sig := <-sigs
|
2015-09-24 12:32:37 +00:00
|
|
|
log.Infof("I have to go... %+v", sig)
|
2015-09-25 22:20:45 +00:00
|
|
|
log.Info("Stopping server")
|
|
|
|
srv.Close()
|
|
|
|
stopChan <- true
|
2015-09-11 14:37:13 +00:00
|
|
|
}()
|
|
|
|
|
2015-09-25 20:02:55 +00:00
|
|
|
//negroni.Use(middlewares.NewCircuitBreaker(oxyLogger))
|
|
|
|
//negroni.Use(middlewares.NewRoutes(configurationRouter))
|
2015-10-19 21:29:25 +00:00
|
|
|
|
|
|
|
var er error
|
2015-11-24 08:12:20 +00:00
|
|
|
serverLock.Lock()
|
2015-10-19 21:29:25 +00:00
|
|
|
srv, er = prepareServer(configurationRouter, globalConfiguration, nil, loggerMiddleware, metrics)
|
|
|
|
if er != nil {
|
|
|
|
log.Fatal("Error preparing server: ", er)
|
|
|
|
}
|
2015-09-25 22:20:45 +00:00
|
|
|
go startServer(srv, globalConfiguration)
|
2015-11-24 08:12:20 +00:00
|
|
|
serverLock.Unlock()
|
2015-09-12 13:10:03 +00:00
|
|
|
|
2015-09-25 22:20:45 +00:00
|
|
|
<-stopChan
|
|
|
|
log.Info("Shutting down")
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 21:29:25 +00:00
|
|
|
func createTLSConfig(certFile string, keyFile string) (*tls.Config, error) {
|
|
|
|
config := &tls.Config{}
|
|
|
|
if config.NextProtos == nil {
|
|
|
|
config.NextProtos = []string{"http/1.1"}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
config.Certificates = make([]tls.Certificate, 1)
|
|
|
|
if len(certFile) > 0 && len(keyFile) > 0 {
|
|
|
|
config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2015-09-25 22:20:45 +00:00
|
|
|
func startServer(srv *manners.GracefulServer, globalConfiguration *GlobalConfiguration) {
|
|
|
|
log.Info("Starting server")
|
2015-09-25 20:02:55 +00:00
|
|
|
if len(globalConfiguration.CertFile) > 0 && len(globalConfiguration.KeyFile) > 0 {
|
|
|
|
err := srv.ListenAndServeTLS(globalConfiguration.CertFile, globalConfiguration.KeyFile)
|
|
|
|
if err != nil {
|
2015-10-19 21:29:25 +00:00
|
|
|
log.Fatal("Error creating server: ", err)
|
2015-09-25 20:02:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := srv.ListenAndServe()
|
|
|
|
if err != nil {
|
2015-10-19 21:29:25 +00:00
|
|
|
log.Fatal("Error creating server: ", err)
|
2015-09-25 20:02:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-25 22:20:45 +00:00
|
|
|
log.Info("Server stopped")
|
2015-09-25 20:02:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 21:29:25 +00:00
|
|
|
func prepareServer(router *mux.Router, globalConfiguration *GlobalConfiguration, oldServer *manners.GracefulServer, middlewares ...negroni.Handler) (*manners.GracefulServer, error) {
|
2015-09-25 22:20:45 +00:00
|
|
|
log.Info("Preparing server")
|
2015-09-25 20:02:55 +00:00
|
|
|
// middlewares
|
|
|
|
var negroni = negroni.New()
|
|
|
|
for _, middleware := range middlewares {
|
|
|
|
negroni.Use(middleware)
|
|
|
|
}
|
|
|
|
negroni.UseHandler(router)
|
2015-10-19 21:29:25 +00:00
|
|
|
tlsConfig, err := createTLSConfig(globalConfiguration.CertFile, globalConfiguration.KeyFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error creating TLS config %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-25 20:02:55 +00:00
|
|
|
|
2015-09-25 22:20:45 +00:00
|
|
|
if oldServer == nil {
|
|
|
|
return manners.NewWithServer(
|
|
|
|
&http.Server{
|
2015-10-19 21:29:25 +00:00
|
|
|
Addr: globalConfiguration.Port,
|
|
|
|
Handler: negroni,
|
|
|
|
TLSConfig: tlsConfig,
|
|
|
|
}), nil
|
2015-09-25 20:02:55 +00:00
|
|
|
}
|
2015-11-06 17:11:57 +00:00
|
|
|
server, err := oldServer.HijackListener(&http.Server{
|
|
|
|
Addr: globalConfiguration.Port,
|
|
|
|
Handler: negroni,
|
|
|
|
}, tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error hijacking server %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return server, nil
|
2015-09-25 20:02:55 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 17:11:57 +00:00
|
|
|
// LoadConfig returns a new gorrilla.mux Route from the specified global configuration and the dynamic
|
|
|
|
// provider configurations.
|
2015-09-22 04:16:21 +00:00
|
|
|
func LoadConfig(configurations configs, globalConfiguration *GlobalConfiguration) (*mux.Router, error) {
|
2015-09-11 14:37:13 +00:00
|
|
|
router := mux.NewRouter()
|
2015-09-12 13:10:03 +00:00
|
|
|
router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
|
2015-09-11 14:37:13 +00:00
|
|
|
backends := map[string]http.Handler{}
|
2015-09-22 04:16:21 +00:00
|
|
|
for _, configuration := range configurations {
|
|
|
|
for frontendName, frontend := range configuration.Frontends {
|
|
|
|
log.Debugf("Creating frontend %s", frontendName)
|
2015-10-30 10:33:41 +00:00
|
|
|
fwd, _ := forward.New(forward.Logger(oxyLogger), forward.PassHostHeader(frontend.PassHostHeader))
|
2015-09-22 04:16:21 +00:00
|
|
|
newRoute := router.NewRoute().Name(frontendName)
|
|
|
|
for routeName, route := range frontend.Routes {
|
|
|
|
log.Debugf("Creating route %s %s:%s", routeName, route.Rule, route.Value)
|
2015-11-23 14:41:16 +00:00
|
|
|
newRouteReflect, err := invoke(newRoute, route.Rule, route.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-22 04:16:21 +00:00
|
|
|
newRoute = newRouteReflect[0].Interface().(*mux.Route)
|
2015-09-25 09:44:19 +00:00
|
|
|
}
|
2015-09-22 04:16:21 +00:00
|
|
|
if backends[frontend.Backend] == nil {
|
|
|
|
log.Debugf("Creating backend %s", frontend.Backend)
|
|
|
|
var lb http.Handler
|
|
|
|
rr, _ := roundrobin.New(fwd)
|
2015-10-01 10:04:25 +00:00
|
|
|
if configuration.Backends[frontend.Backend] == nil {
|
|
|
|
return nil, errors.New("Backend not found: " + frontend.Backend)
|
|
|
|
}
|
2015-11-01 15:35:01 +00:00
|
|
|
lbMethod, err := types.NewLoadBalancerMethod(configuration.Backends[frontend.Backend].LoadBalancer)
|
2015-09-22 04:16:21 +00:00
|
|
|
if err != nil {
|
2015-11-01 15:35:01 +00:00
|
|
|
configuration.Backends[frontend.Backend].LoadBalancer = &types.LoadBalancer{Method: "wrr"}
|
2015-09-25 09:44:19 +00:00
|
|
|
}
|
2015-09-22 04:16:21 +00:00
|
|
|
switch lbMethod {
|
2015-11-01 15:35:01 +00:00
|
|
|
case types.Drr:
|
2015-10-01 10:04:25 +00:00
|
|
|
log.Infof("Creating load-balancer drr")
|
2015-09-22 04:16:21 +00:00
|
|
|
rebalancer, _ := roundrobin.NewRebalancer(rr, roundrobin.RebalancerLogger(oxyLogger))
|
|
|
|
lb = rebalancer
|
|
|
|
for serverName, server := range configuration.Backends[frontend.Backend].Servers {
|
|
|
|
url, err := url.Parse(server.URL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-01 10:04:25 +00:00
|
|
|
log.Infof("Creating server %s %s", serverName, url.String())
|
2015-09-22 04:16:21 +00:00
|
|
|
rebalancer.UpsertServer(url, roundrobin.Weight(server.Weight))
|
|
|
|
}
|
2015-11-01 15:35:01 +00:00
|
|
|
case types.Wrr:
|
2015-10-01 10:04:25 +00:00
|
|
|
log.Infof("Creating load-balancer wrr")
|
2015-09-24 18:15:14 +00:00
|
|
|
lb = middlewares.NewWebsocketUpgrader(rr)
|
2015-09-22 04:16:21 +00:00
|
|
|
for serverName, server := range configuration.Backends[frontend.Backend].Servers {
|
|
|
|
url, err := url.Parse(server.URL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-01 10:04:25 +00:00
|
|
|
log.Infof("Creating server %s %s", serverName, url.String())
|
2015-09-22 04:16:21 +00:00
|
|
|
rr.UpsertServer(url, roundrobin.Weight(server.Weight))
|
2015-09-25 09:44:19 +00:00
|
|
|
}
|
2015-09-19 11:02:59 +00:00
|
|
|
}
|
2015-09-22 04:16:21 +00:00
|
|
|
var negroni = negroni.New()
|
|
|
|
if configuration.Backends[frontend.Backend].CircuitBreaker != nil {
|
2015-10-01 10:04:25 +00:00
|
|
|
log.Infof("Creating circuit breaker %s", configuration.Backends[frontend.Backend].CircuitBreaker.Expression)
|
2015-09-22 04:16:21 +00:00
|
|
|
negroni.Use(middlewares.NewCircuitBreaker(lb, configuration.Backends[frontend.Backend].CircuitBreaker.Expression, cbreaker.Logger(oxyLogger)))
|
|
|
|
} else {
|
|
|
|
negroni.UseHandler(lb)
|
|
|
|
}
|
|
|
|
backends[frontend.Backend] = negroni
|
2015-09-25 09:44:19 +00:00
|
|
|
} else {
|
2015-10-01 10:04:25 +00:00
|
|
|
log.Infof("Reusing backend %s", frontend.Backend)
|
2015-09-25 09:44:19 +00:00
|
|
|
}
|
2015-09-22 04:16:21 +00:00
|
|
|
// stream.New(backends[frontend.Backend], stream.Retry("IsNetworkError() && Attempts() <= " + strconv.Itoa(globalConfiguration.Replay)), stream.Logger(oxyLogger))
|
2015-09-25 09:44:19 +00:00
|
|
|
|
2015-09-22 04:16:21 +00:00
|
|
|
newRoute.Handler(backends[frontend.Backend])
|
|
|
|
err := newRoute.GetError()
|
|
|
|
if err != nil {
|
2015-11-06 17:11:57 +00:00
|
|
|
log.Errorf("Error building route: %s", err)
|
2015-09-22 04:16:21 +00:00
|
|
|
}
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-19 11:02:59 +00:00
|
|
|
return router, nil
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 17:11:57 +00:00
|
|
|
// Invoke calls the specified method with the specified arguments on the specified interface.
|
|
|
|
// It uses the go(lang) reflect package.
|
2015-11-23 14:41:16 +00:00
|
|
|
func invoke(any interface{}, name string, args ...interface{}) ([]reflect.Value, error) {
|
2015-09-11 14:37:13 +00:00
|
|
|
inputs := make([]reflect.Value, len(args))
|
2015-09-15 20:32:09 +00:00
|
|
|
for i := range args {
|
2015-09-11 14:37:13 +00:00
|
|
|
inputs[i] = reflect.ValueOf(args[i])
|
|
|
|
}
|
2015-11-23 14:41:16 +00:00
|
|
|
method := reflect.ValueOf(any).MethodByName(name)
|
|
|
|
if method.IsValid() {
|
|
|
|
return method.Call(inputs), nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("Method not found: " + name)
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|