Skip to content

Commit

Permalink
lazily start watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Asim Aslam committed Oct 26, 2017
1 parent bd46e60 commit 6fb652f
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions selector/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type cacheSelector struct {
cache map[string][]*registry.Service
ttls map[string]time.Time

once sync.Once

// used to close or reload watcher
reload chan bool
exit chan bool
Expand Down Expand Up @@ -241,13 +243,19 @@ func (c *cacheSelector) run() {
// create new watcher
w, err := c.so.Registry.Watch()
if err != nil {
if c.quit() {
return
}
log.Log(err)
time.Sleep(time.Second)
continue
}

// watch for events
if err := c.watch(w); err != nil {
if c.quit() {
return
}
log.Log(err)
continue
}
Expand Down Expand Up @@ -324,6 +332,10 @@ func (c *cacheSelector) Options() selector.Options {
}

func (c *cacheSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) {
c.once.Do(func() {
go c.run()
})

sopts := selector.SelectOptions{
Strategy: c.so.Strategy,
}
Expand Down Expand Up @@ -401,15 +413,12 @@ func NewSelector(opts ...selector.Option) selector.Selector {
}
}

c := &cacheSelector{
return &cacheSelector{
so: sopts,
ttl: ttl,
cache: make(map[string][]*registry.Service),
ttls: make(map[string]time.Time),
reload: make(chan bool, 1),
exit: make(chan bool),
}

go c.run()
return c
}

0 comments on commit 6fb652f

Please sign in to comment.