Merge pull request #341 from sneat/consul-read-write-map

Prevent read/write map concurrency issue
This commit is contained in:
Asim Aslam 2018-12-03 06:24:31 +00:00 committed by GitHub
commit 489573afb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -190,16 +190,17 @@ func (c *consulRegistry) Register(s *Service, opts ...RegisterOption) error {
// use first node // use first node
node := s.Nodes[0] node := s.Nodes[0]
// get existing hash // get existing hash and last checked time
c.Lock() c.Lock()
v, ok := c.register[s.Name] v, ok := c.register[s.Name]
lastChecked := c.lastChecked[s.Name]
c.Unlock() c.Unlock()
// if it's already registered and matches then just pass the check // if it's already registered and matches then just pass the check
if ok && v == h { if ok && v == h {
if options.TTL == time.Duration(0) { if options.TTL == time.Duration(0) {
// ensure that our service hasn't been deregistered by Consul // 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 return nil
} }
services, _, err := c.Client.Health().Checks(s.Name, c.queryOptions) services, _, err := c.Client.Health().Checks(s.Name, c.queryOptions)