From bf6427ad2366033a222aecbb0b1d77b4c9871cd1 Mon Sep 17 00:00:00 2001 From: Alexandre Beslic Date: Mon, 25 May 2015 12:36:27 -0700 Subject: [PATCH 1/3] Additional check enforcing ttl >= 10s for Consul and ephemeral entries Signed-off-by: Alexandre Beslic --- pkg/store/consul.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/store/consul.go b/pkg/store/consul.go index d0e8e4f729..beb3ec74a3 100644 --- a/pkg/store/consul.go +++ b/pkg/store/consul.go @@ -16,6 +16,10 @@ const ( // watched key has changed. This affects the minimum time it takes to // cancel a watch. DefaultWatchWaitTime = 15 * time.Second + + // MinimumTimeToLive is the minimum TTL value allowed by Consul for + // Ephemeral entries + MinimumTimeToLive = 10 * time.Second ) // Consul embeds the client and watches @@ -81,8 +85,11 @@ func (s *Consul) setTimeout(time time.Duration) { } // SetEphemeralTTL sets the ttl for ephemeral nodes -func (s *Consul) setEphemeralTTL(time time.Duration) { - s.ephemeralTTL = time +func (s *Consul) setEphemeralTTL(ttl time.Duration) { + if ttl < MinimumTimeToLive { + log.Fatal("Consul does not allow a ttl < 10s, please specify a ttl >= 10s") + } + s.ephemeralTTL = ttl } // CreateEphemeralSession creates the a global session From 96241c6b794ec0200cc78ee50bbf1c2a7fe2fece Mon Sep 17 00:00:00 2001 From: Alexandre Beslic Date: Tue, 26 May 2015 11:01:17 -0700 Subject: [PATCH 2/3] pull out the log.Fatal from pkg/store to the discovery level Signed-off-by: Alexandre Beslic --- discovery/kv/kv.go | 10 +++++++++- pkg/store/consul.go | 10 +++++++--- pkg/store/store.go | 2 ++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/discovery/kv/kv.go b/discovery/kv/kv.go index 64850bfeda..0627788833 100644 --- a/discovery/kv/kv.go +++ b/discovery/kv/kv.go @@ -61,7 +61,15 @@ func (s *Discovery) Initialize(uris string, heartbeat time.Duration, ttl time.Du EphemeralTTL: s.ttl, }, ) - return err + + if err != nil { + if err == store.ErrInvalidTTL { + log.Fatal(err) + } + return err + } + + return nil } // Watch the store until either there's a store error or we receive a stop request. diff --git a/pkg/store/consul.go b/pkg/store/consul.go index beb3ec74a3..3335bdff2a 100644 --- a/pkg/store/consul.go +++ b/pkg/store/consul.go @@ -56,7 +56,10 @@ func InitializeConsul(endpoints []string, options *Config) (Store, error) { s.setTimeout(options.ConnectionTimeout) } if options.EphemeralTTL != 0 { - s.setEphemeralTTL(options.EphemeralTTL) + err := s.setEphemeralTTL(options.EphemeralTTL) + if err != nil { + return nil, err + } } } @@ -85,11 +88,12 @@ func (s *Consul) setTimeout(time time.Duration) { } // SetEphemeralTTL sets the ttl for ephemeral nodes -func (s *Consul) setEphemeralTTL(ttl time.Duration) { +func (s *Consul) setEphemeralTTL(ttl time.Duration) error { if ttl < MinimumTimeToLive { - log.Fatal("Consul does not allow a ttl < 10s, please specify a ttl >= 10s") + return ErrInvalidTTL } s.ephemeralTTL = ttl + return nil } // CreateEphemeralSession creates the a global session diff --git a/pkg/store/store.go b/pkg/store/store.go index 0fc390a232..a372a3dc95 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -23,6 +23,8 @@ const ( ) var ( + // ErrInvalidTTL is a specific error to consul + ErrInvalidTTL = errors.New("Invalid TTL, please change the value to the miminum allowed ttl for the chosen store") // ErrNotSupported is exported ErrNotSupported = errors.New("Backend storage not supported yet, please choose another one") // ErrNotImplemented is exported From 814932ccb7669f58a233e3eb7d73f7f5fd87aa5f Mon Sep 17 00:00:00 2001 From: Alexandre Beslic Date: Tue, 26 May 2015 12:32:00 -0700 Subject: [PATCH 3/3] Simplify check Signed-off-by: Alexandre Beslic --- discovery/kv/kv.go | 9 +-------- pkg/store/consul.go | 3 +-- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/discovery/kv/kv.go b/discovery/kv/kv.go index 0627788833..1424c0716f 100644 --- a/discovery/kv/kv.go +++ b/discovery/kv/kv.go @@ -62,14 +62,7 @@ func (s *Discovery) Initialize(uris string, heartbeat time.Duration, ttl time.Du }, ) - if err != nil { - if err == store.ErrInvalidTTL { - log.Fatal(err) - } - return err - } - - return nil + return err } // Watch the store until either there's a store error or we receive a stop request. diff --git a/pkg/store/consul.go b/pkg/store/consul.go index 3335bdff2a..e055a03e70 100644 --- a/pkg/store/consul.go +++ b/pkg/store/consul.go @@ -56,8 +56,7 @@ func InitializeConsul(endpoints []string, options *Config) (Store, error) { s.setTimeout(options.ConnectionTimeout) } if options.EphemeralTTL != 0 { - err := s.setEphemeralTTL(options.EphemeralTTL) - if err != nil { + if err := s.setEphemeralTTL(options.EphemeralTTL); err != nil { return nil, err } }