1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00

fix(registry): fix watch services only first time (#2517)

This commit is contained in:
JellyTony 2022-06-29 15:49:22 +08:00 committed by GitHub
parent 9960cd1d36
commit 65a2e4ed02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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),
}
}