Prevent read/write map concurrency issue

This commit is contained in:
Blair McMillan 2018-12-03 14:59:31 +10:00
parent d0d8db7c45
commit a9593bad66

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)