@@ -42,6 +42,7 @@ type cache struct {
42
42
sg singleflight.Group
43
43
cache map [string ][]* registry.Service
44
44
ttls map [string ]time.Time
45
+ nttls map [string ]map [string ]time.Time // node ttls
45
46
watched map [string ]bool
46
47
47
48
// used to stop the cache
@@ -94,6 +95,17 @@ func (c *cache) isValid(services []*registry.Service, ttl time.Time) bool {
94
95
return false
95
96
}
96
97
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
+
97
109
// ok
98
110
return true
99
111
}
@@ -115,6 +127,7 @@ func (c *cache) del(service string) {
115
127
// otherwise delete entries
116
128
delete (c .cache , service )
117
129
delete (c .ttls , service )
130
+ delete (c .nttls , service )
118
131
}
119
132
120
133
func (c * cache ) get (service string ) ([]* registry.Service , error ) {
@@ -128,7 +141,7 @@ func (c *cache) get(service string) ([]*registry.Service, error) {
128
141
// make a copy
129
142
cp := util .Copy (services )
130
143
131
- // got services && within ttl so return cache
144
+ // got services, nodes && within ttl so return cache
132
145
if c .isValid (cp , ttl ) {
133
146
c .RUnlock ()
134
147
// return services
@@ -197,6 +210,14 @@ func (c *cache) get(service string) ([]*registry.Service, error) {
197
210
func (c * cache ) set (service string , services []* registry.Service ) {
198
211
c .cache [service ] = services
199
212
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
+ }
200
221
}
201
222
202
223
func (c * cache ) update (res * registry.Result ) {
@@ -483,6 +504,7 @@ func New(r registry.Registry, opts ...Option) Cache {
483
504
watchedRunning : make (map [string ]bool ),
484
505
cache : make (map [string ][]* registry.Service ),
485
506
ttls : make (map [string ]time.Time ),
507
+ nttls : make (map [string ]map [string ]time.Time ),
486
508
exit : make (chan bool ),
487
509
}
488
510
}
0 commit comments