From 65a2e4ed029e905494e86a15fd059d4eda136245 Mon Sep 17 00:00:00 2001 From: JellyTony <106534114+JellyTony@users.noreply.github.com> Date: Wed, 29 Jun 2022 15:49:22 +0800 Subject: [PATCH] fix(registry): fix watch services only first time (#2517) --- registry/cache/cache.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/registry/cache/cache.go b/registry/cache/cache.go index e2d259ad..5a5f0ae8 100644 --- a/registry/cache/cache.go +++ b/registry/cache/cache.go @@ -42,7 +42,7 @@ type cache struct { exit chan bool // indicate whether its running - running bool + watchedRunning map[string]bool // status of the registry // used to hold onto the cache // in failure state @@ -179,7 +179,7 @@ func (c *cache) get(service string) ([]*registry.Service, error) { c.watched[service] = true // only kick it off if not running - if !c.running { + if !c.watchedRunning[service] { go c.run(service) } @@ -318,14 +318,14 @@ func (c *cache) update(res *registry.Result) { // it creates a new watcher if there's a problem func (c *cache) run(service string) { c.Lock() - c.running = true + c.watchedRunning[service] = true c.Unlock() // reset watcher on exit defer func() { c.Lock() c.watched = make(map[string]bool) - c.running = false + c.watchedRunning[service] = false c.Unlock() }() @@ -475,11 +475,12 @@ func New(r registry.Registry, opts ...Option) Cache { } return &cache{ - Registry: r, - opts: options, - watched: make(map[string]bool), - cache: make(map[string][]*registry.Service), - ttls: make(map[string]time.Time), - exit: make(chan bool), + Registry: r, + opts: options, + watched: make(map[string]bool), + watchedRunning: make(map[string]bool), + cache: make(map[string][]*registry.Service), + ttls: make(map[string]time.Time), + exit: make(chan bool), } }