Skip to content

Commit cdb6a0d

Browse files
committed
invalidate service if node was not updated
Signed-off-by: Jörn Friedrich Dreyer <[email protected]>
1 parent b318b7f commit cdb6a0d

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

registry/cache/cache.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type cache struct {
4242
sg singleflight.Group
4343
cache map[string][]*registry.Service
4444
ttls map[string]time.Time
45+
nttls map[string]map[string]time.Time // node ttls
4546
watched map[string]bool
4647

4748
// used to stop the cache
@@ -94,6 +95,17 @@ func (c *cache) isValid(services []*registry.Service, ttl time.Time) bool {
9495
return false
9596
}
9697

98+
// a node did not get updated
99+
for _, s := range services {
100+
for _, n := range s.Nodes {
101+
nttl := c.nttls[s.Name][n.Id]
102+
if time.Since(nttl) > 0 {
103+
delete(c.nttls, s.Name)
104+
return false
105+
}
106+
}
107+
}
108+
97109
// ok
98110
return true
99111
}
@@ -115,6 +127,7 @@ func (c *cache) del(service string) {
115127
// otherwise delete entries
116128
delete(c.cache, service)
117129
delete(c.ttls, service)
130+
delete(c.nttls, service)
118131
}
119132

120133
func (c *cache) get(service string) ([]*registry.Service, error) {
@@ -128,7 +141,7 @@ func (c *cache) get(service string) ([]*registry.Service, error) {
128141
// make a copy
129142
cp := util.Copy(services)
130143

131-
// got services && within ttl so return cache
144+
// got services, nodes && within ttl so return cache
132145
if c.isValid(cp, ttl) {
133146
c.RUnlock()
134147
// return services
@@ -197,6 +210,14 @@ func (c *cache) get(service string) ([]*registry.Service, error) {
197210
func (c *cache) set(service string, services []*registry.Service) {
198211
c.cache[service] = services
199212
c.ttls[service] = time.Now().Add(c.opts.TTL)
213+
for _, s := range services {
214+
for _, n := range s.Nodes {
215+
if c.nttls[s.Name] == nil {
216+
c.nttls[s.Name] = make(map[string]time.Time)
217+
}
218+
c.nttls[s.Name][n.Id] = time.Now().Add(c.opts.TTL)
219+
}
220+
}
200221
}
201222

202223
func (c *cache) update(res *registry.Result) {
@@ -483,6 +504,7 @@ func New(r registry.Registry, opts ...Option) Cache {
483504
watchedRunning: make(map[string]bool),
484505
cache: make(map[string][]*registry.Service),
485506
ttls: make(map[string]time.Time),
507+
nttls: make(map[string]map[string]time.Time),
486508
exit: make(chan bool),
487509
}
488510
}

0 commit comments

Comments
 (0)