docker backoff, routes middleware

This commit is contained in:
emile 2015-09-12 19:22:44 +02:00
parent 0881151a44
commit 07b520fe23
4 changed files with 63 additions and 21 deletions

View file

@ -1,7 +1,6 @@
# /træfɪk/ # /træfɪk/
* Providers recovery * Providers recovery
* Use Negroni middlewares for metrics, grace, logs
* tls client verification * tls client verification
* Default configuration values * Default configuration values
* Retry with streams * Retry with streams
@ -28,3 +27,5 @@
* ~~SSL frontend support~~ * ~~SSL frontend support~~
* ~~Static files~~ * ~~Static files~~
* ~~Metrics~~ * ~~Metrics~~
* ~~Dockerfile~~
* ~~Use Negroni middlewares for metrics, grace, logs~~

View file

@ -6,15 +6,17 @@ import (
"github.com/BurntSushi/ty/fun" "github.com/BurntSushi/ty/fun"
"github.com/fsouza/go-dockerclient" "github.com/fsouza/go-dockerclient"
"github.com/leekchan/gtf" "github.com/leekchan/gtf"
"github.com/cenkalti/backoff"
"strconv" "strconv"
"strings" "strings"
"text/template" "text/template"
"errors"
"time"
) )
type DockerProvider struct { type DockerProvider struct {
Watch bool Watch bool
Endpoint string Endpoint string
dockerClient *docker.Client
Filename string Filename string
Domain string Domain string
} }
@ -62,50 +64,60 @@ var DockerFuncMap = template.FuncMap{
"getHost": getHost, "getHost": getHost,
} }
func (provider *DockerProvider) Provide(configurationChan chan<- *Configuration) { func (provider *DockerProvider) Provide(configurationChan chan <- *Configuration) {
if client, err := docker.NewClient(provider.Endpoint); err != nil { if dockerClient, err := docker.NewClient(provider.Endpoint); err != nil {
log.Fatalf("Failed to create a client for docker, error: %s", err) log.Fatalf("Failed to create a client for docker, error: %s", err)
} else { } else {
provider.dockerClient = client err := dockerClient.Ping()
_, err := provider.dockerClient.Info()
if err != nil { if err != nil {
log.Fatalf("Docker connection error %+v", err) log.Fatalf("Docker connection error %+v", err)
} }
log.Debug("Docker connection established") log.Debug("Docker connection established")
dockerEvents := make(chan *docker.APIEvents) dockerEvents := make(chan *docker.APIEvents)
if provider.Watch { if provider.Watch {
provider.dockerClient.AddEventListener(dockerEvents) dockerClient.AddEventListener(dockerEvents)
log.Debug("Docker listening")
go func() { go func() {
for { operation := func() error {
event := <-dockerEvents for {
if event == nil { event := <-dockerEvents
log.Fatalf("Docker connection error %+v", err) if event == nil {
} return errors.New("Docker event nil")
if event.Status == "start" || event.Status == "die" { // log.Fatalf("Docker connection error")
log.Debug("Docker event receveived %+v", event) }
configuration := provider.loadDockerConfig() if (event.Status == "start" || event.Status == "die" ) {
if configuration != nil { log.Debug("Docker event receveived %+v", event)
configurationChan <- configuration configuration := provider.loadDockerConfig(dockerClient)
if configuration != nil {
configurationChan <- configuration
}
} }
} }
} }
notify := func(err error, time time.Duration) {
log.Error("Docker connection error %+v, retrying in %s", err, time)
}
err := backoff.RetryNotify(operation, backoff.NewExponentialBackOff(), notify)
if err != nil {
log.Fatalf("Cannot connect to docker server %+v", err)
}
}() }()
} }
configuration := provider.loadDockerConfig() configuration := provider.loadDockerConfig(dockerClient)
configurationChan <- configuration configurationChan <- configuration
} }
} }
func (provider *DockerProvider) loadDockerConfig() *Configuration { func (provider *DockerProvider) loadDockerConfig(dockerClient *docker.Client) *Configuration {
configuration := new(Configuration) configuration := new(Configuration)
containerList, _ := provider.dockerClient.ListContainers(docker.ListContainersOptions{}) containerList, _ := dockerClient.ListContainers(docker.ListContainersOptions{})
containersInspected := []docker.Container{} containersInspected := []docker.Container{}
hosts := map[string][]docker.Container{} hosts := map[string][]docker.Container{}
// get inspect containers // get inspect containers
for _, container := range containerList { for _, container := range containerList {
containerInspected, _ := provider.dockerClient.InspectContainer(container.ID) containerInspected, _ := dockerClient.InspectContainer(container.ID)
containersInspected = append(containersInspected, *containerInspected) containersInspected = append(containersInspected, *containerInspected)
} }

28
middlewares/routes.go Normal file
View file

@ -0,0 +1,28 @@
/*
Copyright
*/
package middlewares
import (
"log"
"net/http"
"github.com/gorilla/mux"
"encoding/json"
)
type Routes struct {
router *mux.Router
}
func NewRoutes(router *mux.Router) *Routes {
return &Routes{router}
}
func (router *Routes) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
routeMatch :=mux.RouteMatch{}
if(router.router.Match(r, &routeMatch)){
json, _ := json.Marshal(routeMatch.Handler)
log.Println("Request match route ", json)
}
next(rw, r)
}

View file

@ -142,6 +142,7 @@ func main() {
var negroni = negroni.New() var negroni = negroni.New()
negroni.Use(metrics) negroni.Use(metrics)
negroni.Use(loggerMiddleware) negroni.Use(loggerMiddleware)
//negroni.Use(middlewares.NewRoutes(configurationRouter))
negroni.UseHandler(configurationRouter) negroni.UseHandler(configurationRouter)
srv = &graceful.Server{ srv = &graceful.Server{