remove ticker
This commit is contained in:
parent
6fb652f78a
commit
d48735793d
72
selector/cache/cache.go
vendored
72
selector/cache/cache.go
vendored
@ -88,32 +88,45 @@ func (c *cacheSelector) get(service string) ([]*registry.Service, error) {
|
|||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
|
|
||||||
// check the cache first
|
// get does the actual request for a service
|
||||||
services, ok := c.cache[service]
|
// it also caches it
|
||||||
ttl, kk := c.ttls[service]
|
get := func(service string) ([]*registry.Service, error) {
|
||||||
|
// ask the registry
|
||||||
// got results, copy and return
|
|
||||||
if ok && len(services) > 0 {
|
|
||||||
// only return if its less than the ttl
|
|
||||||
if kk && time.Since(ttl) < c.ttl {
|
|
||||||
return c.cp(services), nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// cache miss or ttl expired
|
|
||||||
|
|
||||||
// now ask the registry
|
|
||||||
services, err := c.so.Registry.GetService(service)
|
services, err := c.so.Registry.GetService(service)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// we didn't have any results so cache
|
// cache results
|
||||||
c.cache[service] = c.cp(services)
|
c.set(service, c.cp(services))
|
||||||
c.ttls[service] = time.Now().Add(c.ttl)
|
|
||||||
return services, nil
|
return services, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check the cache first
|
||||||
|
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]
|
||||||
|
|
||||||
|
// within ttl so return cache
|
||||||
|
if kk && time.Since(ttl) < c.ttl {
|
||||||
|
return c.cp(services), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// expired entry so get service
|
||||||
|
if services, err := get(service); err == nil {
|
||||||
|
return services, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// return expired cache as last resort
|
||||||
|
return c.cp(services), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *cacheSelector) set(service string, services []*registry.Service) {
|
func (c *cacheSelector) set(service string, services []*registry.Service) {
|
||||||
c.cache[service] = services
|
c.cache[service] = services
|
||||||
c.ttls[service] = time.Now().Add(c.ttl)
|
c.ttls[service] = time.Now().Add(c.ttl)
|
||||||
@ -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