From a9593bad66fe0c0983b71378d6028c92b05af62c Mon Sep 17 00:00:00 2001 From: Blair McMillan Date: Mon, 3 Dec 2018 14:59:31 +1000 Subject: [PATCH] Prevent read/write map concurrency issue --- registry/consul_registry.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/registry/consul_registry.go b/registry/consul_registry.go index 52ccf759..02725b5d 100644 --- a/registry/consul_registry.go +++ b/registry/consul_registry.go @@ -190,16 +190,17 @@ func (c *consulRegistry) Register(s *Service, opts ...RegisterOption) error { // use first node node := s.Nodes[0] - // get existing hash + // get existing hash and last checked time c.Lock() v, ok := c.register[s.Name] + lastChecked := c.lastChecked[s.Name] c.Unlock() // if it's already registered and matches then just pass the check if ok && v == h { if options.TTL == time.Duration(0) { // ensure that our service hasn't been deregistered by Consul - if time.Since(c.lastChecked[s.Name]) <= getDeregisterTTL(regInterval) { + if time.Since(lastChecked) <= getDeregisterTTL(regInterval) { return nil } services, _, err := c.Client.Health().Checks(s.Name, c.queryOptions)