remove ticker
This commit is contained in:
parent
6fb652f78a
commit
d48735793d
68
selector/cache/cache.go
vendored
68
selector/cache/cache.go
vendored
@ -88,30 +88,43 @@ func (c *cacheSelector) get(service string) ([]*registry.Service, error) {
|
|||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
|
|
||||||
|
// get does the actual request for a service
|
||||||
|
// it also caches it
|
||||||
|
get := func(service string) ([]*registry.Service, error) {
|
||||||
|
// ask the registry
|
||||||
|
services, err := c.so.Registry.GetService(service)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// cache results
|
||||||
|
c.set(service, c.cp(services))
|
||||||
|
return services, nil
|
||||||
|
}
|
||||||
|
|
||||||
// check the cache first
|
// check the cache first
|
||||||
services, ok := c.cache[service]
|
services, ok := c.cache[service]
|
||||||
|
|
||||||
|
// cache miss or no services
|
||||||
|
if !ok || len(services) == 0 {
|
||||||
|
return get(service)
|
||||||
|
}
|
||||||
|
|
||||||
|
// got cache but lets check ttl
|
||||||
ttl, kk := c.ttls[service]
|
ttl, kk := c.ttls[service]
|
||||||
|
|
||||||
// got results, copy and return
|
// within ttl so return cache
|
||||||
if ok && len(services) > 0 {
|
if kk && time.Since(ttl) < c.ttl {
|
||||||
// only return if its less than the ttl
|
return c.cp(services), nil
|
||||||
if kk && time.Since(ttl) < c.ttl {
|
|
||||||
return c.cp(services), nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// cache miss or ttl expired
|
// expired entry so get service
|
||||||
|
if services, err := get(service); err == nil {
|
||||||
// now ask the registry
|
return services, nil
|
||||||
services, err := c.so.Registry.GetService(service)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// we didn't have any results so cache
|
// return expired cache as last resort
|
||||||
c.cache[service] = c.cp(services)
|
return c.cp(services), nil
|
||||||
c.ttls[service] = time.Now().Add(c.ttl)
|
|
||||||
return services, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *cacheSelector) set(service string, services []*registry.Service) {
|
func (c *cacheSelector) set(service string, services []*registry.Service) {
|
||||||
@ -232,8 +245,6 @@ func (c *cacheSelector) update(res *registry.Result) {
|
|||||||
// reloads the watcher if Init is called
|
// reloads the watcher if Init is called
|
||||||
// and returns when Close is called
|
// and returns when Close is called
|
||||||
func (c *cacheSelector) run() {
|
func (c *cacheSelector) run() {
|
||||||
go c.tick()
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// exit early if already dead
|
// exit early if already dead
|
||||||
if c.quit() {
|
if c.quit() {
|
||||||
@ -262,27 +273,6 @@ func (c *cacheSelector) run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check cache and expire on each tick
|
|
||||||
func (c *cacheSelector) tick() {
|
|
||||||
t := time.NewTicker(time.Minute)
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-t.C:
|
|
||||||
c.Lock()
|
|
||||||
for service, expiry := range c.ttls {
|
|
||||||
if d := time.Since(expiry); d > c.ttl {
|
|
||||||
// TODO: maybe refresh the cache rather than blowing it away
|
|
||||||
c.del(service)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c.Unlock()
|
|
||||||
case <-c.exit:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// watch loops the next event and calls update
|
// watch loops the next event and calls update
|
||||||
// it returns if there's an error
|
// it returns if there's an error
|
||||||
func (c *cacheSelector) watch(w registry.Watcher) error {
|
func (c *cacheSelector) watch(w registry.Watcher) error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user