From 029e2e491e284325e7f97fe6fa41e00b065b6bf9 Mon Sep 17 00:00:00 2001 From: Marcelo Salazar R Date: Wed, 19 Aug 2015 15:28:22 -0300 Subject: [PATCH 1/3] Adding retries to backend service in the startup Signed-off-by: Marcelo Salazar R --- bridge/bridge.go | 21 ++++++++++++++++++--- bridge/types.go | 2 ++ registrator.go | 9 +++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/bridge/bridge.go b/bridge/bridge.go index 4901c0901..4ee20b933 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -9,6 +9,7 @@ import ( "strconv" "strings" "sync" + "time" dockerapi "github.com/fsouza/go-dockerclient" ) @@ -32,10 +33,24 @@ func New(docker *dockerapi.Client, adapterUri string, config Config) *Bridge { log.Fatal("Unrecognized adapter:", adapterUri) } adapter := factory.New(uri) - err = adapter.Ping() - if err != nil { - log.Fatalf("%s: %s", uri.Scheme, err) + + attempt := 0 + for config.RetryAttempts == -1 || attempt <= config.RetryAttempts { + log.Printf("Connecting to backend (%v/%v)", attempt, config.RetryAttempts) + + err = adapter.Ping() + if err == nil { + break + } + + if err != nil && attempt == config.RetryAttempts { + log.Fatalf("%s: %s", uri.Scheme, err) + } + + time.Sleep(time.Duration(config.RetryInterval) * time.Millisecond) + attempt++ } + log.Println("Using", uri.Scheme, "adapter:", uri) return &Bridge{ docker: docker, diff --git a/bridge/types.go b/bridge/types.go index 6560f4683..da9e65e8c 100644 --- a/bridge/types.go +++ b/bridge/types.go @@ -25,6 +25,8 @@ type Config struct { RefreshTtl int RefreshInterval int DeregisterCheck string + RetryAttempts int + RetryInterval int } type Service struct { diff --git a/registrator.go b/registrator.go index df3227cbe..32ef2c133 100644 --- a/registrator.go +++ b/registrator.go @@ -23,6 +23,8 @@ var refreshTtl = flag.Int("ttl", 0, "TTL for services (default is no expiry)") var forceTags = flag.String("tags", "", "Append tags for all registered services") var resyncInterval = flag.Int("resync", 0, "Frequency with which services are resynchronized") var deregister = flag.String("deregister", "always", "Deregister exited services \"always\" or \"on-success\"") +var retryAttempts = flag.Int("retry-attempts", 0, "Max retry attempts to establish a connection with the backend. Use -1 for infinite retries") +var retryInterval = flag.Int("retry-interval", 2000, "Interval (in millisecond) between retry-attempts.") func getopt(name, def string) string { if env := os.Getenv(name); env != "" { @@ -49,12 +51,17 @@ func main() { if *hostIp != "" { log.Println("Forcing host IP to", *hostIp) } + if (*refreshTtl == 0 && *refreshInterval > 0) || (*refreshTtl > 0 && *refreshInterval == 0) { assert(errors.New("-ttl and -ttl-refresh must be specified together or not at all")) } else if *refreshTtl > 0 && *refreshTtl <= *refreshInterval { assert(errors.New("-ttl must be greater than -ttl-refresh")) } + if *retryInterval <= 0 { + assert(errors.New("-retry-interval must be greater than 0")) + } + docker, err := dockerapi.NewClient(getopt("DOCKER_HOST", "unix:///tmp/docker.sock")) assert(err) @@ -69,6 +76,8 @@ func main() { RefreshTtl: *refreshTtl, RefreshInterval: *refreshInterval, DeregisterCheck: *deregister, + RetryAttempts: *retryAttempts, + RetryInterval: *retryInterval, }) // Start event listener before listing containers to avoid missing anything From d3863e3c5016f382776712fb2c57bc4716a7aa7b Mon Sep 17 00:00:00 2001 From: Marcelo Salazar R Date: Wed, 19 Aug 2015 15:48:51 -0300 Subject: [PATCH 2/3] Added retry parameters documentation Signed-off-by: Marcelo Salazar R --- docs/user/run.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/user/run.md b/docs/user/run.md index 86f10a059..c7aa891dd 100644 --- a/docs/user/run.md +++ b/docs/user/run.md @@ -35,6 +35,8 @@ Option | Description ------ | ----------- `-internal` | Use exposed ports instead of published ports `-ip ` | Force IP address used for registering services +`-retry-attempts` | Max retry attempts to establish a connection with the backend +`-retry-interval` | Interval (in millisecond) between retry-attempts `-tags ` | Force comma-separated tags on all registered services `-deregister ` | Deregister existed services "always" or "on-success". Default: always `-ttl ` | TTL for services. Default: 0, no expiry (supported backends only) @@ -52,6 +54,8 @@ argument. For registry backends that support TTL expiry, Registrator can both set and refresh service TTLs with `-ttl` and `-ttl-refresh`. +If you want unlimited retry-attempts use `-retry-attempts -1`. + The `-resync` options controls how often Registrator will query Docker for all containers and reregister all services. This allows Registrator and the service registry to get back in sync if they fall out of sync. From 02bda14e22b8ebe9815bd0e04f9ef31a4ec0fdcb Mon Sep 17 00:00:00 2001 From: Marcelo Salazar R Date: Fri, 21 Aug 2015 22:20:16 -0300 Subject: [PATCH 3/3] Removed unused attributes --- bridge/types.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/bridge/types.go b/bridge/types.go index da9e65e8c..6560f4683 100644 --- a/bridge/types.go +++ b/bridge/types.go @@ -25,8 +25,6 @@ type Config struct { RefreshTtl int RefreshInterval int DeregisterCheck string - RetryAttempts int - RetryInterval int } type Service struct {