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 exit chan bool
// indicate whether its running // indicate whether its running
running bool watchedRunning map[string]bool
// status of the registry // status of the registry
// used to hold onto the cache // used to hold onto the cache
// in failure state // in failure state
@ -179,7 +179,7 @@ func (c *cache) get(service string) ([]*registry.Service, error) {
c.watched[service] = true c.watched[service] = true
// only kick it off if not running // only kick it off if not running
if !c.running { if !c.watchedRunning[service] {
go c.run(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 // it creates a new watcher if there's a problem
func (c *cache) run(service string) { func (c *cache) run(service string) {
c.Lock() c.Lock()
c.running = true c.watchedRunning[service] = true
c.Unlock() c.Unlock()
// reset watcher on exit // reset watcher on exit
defer func() { defer func() {
c.Lock() c.Lock()
c.watched = make(map[string]bool) c.watched = make(map[string]bool)
c.running = false c.watchedRunning[service] = false
c.Unlock() c.Unlock()
}() }()
@ -475,11 +475,12 @@ func New(r registry.Registry, opts ...Option) Cache {
} }
return &cache{ return &cache{
Registry: r, Registry: r,
opts: options, opts: options,
watched: make(map[string]bool), watched: make(map[string]bool),
cache: make(map[string][]*registry.Service), watchedRunning: make(map[string]bool),
ttls: make(map[string]time.Time), cache: make(map[string][]*registry.Service),
exit: make(chan bool), ttls: make(map[string]time.Time),
exit: make(chan bool),
} }
} }