lazily start watcher
This commit is contained in:
parent
bd46e60c13
commit
6fb652f78a
17
selector/cache/cache.go
vendored
17
selector/cache/cache.go
vendored
@ -23,6 +23,8 @@ type cacheSelector struct {
|
|||||||
cache map[string][]*registry.Service
|
cache map[string][]*registry.Service
|
||||||
ttls map[string]time.Time
|
ttls map[string]time.Time
|
||||||
|
|
||||||
|
once sync.Once
|
||||||
|
|
||||||
// used to close or reload watcher
|
// used to close or reload watcher
|
||||||
reload chan bool
|
reload chan bool
|
||||||
exit chan bool
|
exit chan bool
|
||||||
@ -241,6 +243,9 @@ func (c *cacheSelector) run() {
|
|||||||
// create new watcher
|
// create new watcher
|
||||||
w, err := c.so.Registry.Watch()
|
w, err := c.so.Registry.Watch()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if c.quit() {
|
||||||
|
return
|
||||||
|
}
|
||||||
log.Log(err)
|
log.Log(err)
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
continue
|
continue
|
||||||
@ -248,6 +253,9 @@ func (c *cacheSelector) run() {
|
|||||||
|
|
||||||
// watch for events
|
// watch for events
|
||||||
if err := c.watch(w); err != nil {
|
if err := c.watch(w); err != nil {
|
||||||
|
if c.quit() {
|
||||||
|
return
|
||||||
|
}
|
||||||
log.Log(err)
|
log.Log(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -324,6 +332,10 @@ func (c *cacheSelector) Options() selector.Options {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *cacheSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) {
|
func (c *cacheSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) {
|
||||||
|
c.once.Do(func() {
|
||||||
|
go c.run()
|
||||||
|
})
|
||||||
|
|
||||||
sopts := selector.SelectOptions{
|
sopts := selector.SelectOptions{
|
||||||
Strategy: c.so.Strategy,
|
Strategy: c.so.Strategy,
|
||||||
}
|
}
|
||||||
@ -401,7 +413,7 @@ func NewSelector(opts ...selector.Option) selector.Selector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c := &cacheSelector{
|
return &cacheSelector{
|
||||||
so: sopts,
|
so: sopts,
|
||||||
ttl: ttl,
|
ttl: ttl,
|
||||||
cache: make(map[string][]*registry.Service),
|
cache: make(map[string][]*registry.Service),
|
||||||
@ -409,7 +421,4 @@ func NewSelector(opts ...selector.Option) selector.Selector {
|
|||||||
reload: make(chan bool, 1),
|
reload: make(chan bool, 1),
|
||||||
exit: make(chan bool),
|
exit: make(chan bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
go c.run()
|
|
||||||
return c
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user