fix node cache ttl updates

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
Jörn Friedrich Dreyer
2024-11-15 14:33:48 +01:00
parent ee61c32ab9
commit 50176f6f6b
5 changed files with 22 additions and 12 deletions
+12 -7
View File
@@ -176,6 +176,9 @@ func (c *cache) get(service string) ([]*registry.Service, error) {
// cache results
cp := util.Copy(services)
c.Lock()
for _, s := range services {
c.updateNodeTTLs(service, s.Nodes)
}
c.set(service, services)
c.Unlock()
@@ -210,13 +213,14 @@ func (c *cache) get(service string) ([]*registry.Service, error) {
func (c *cache) set(service string, services []*registry.Service) {
c.cache[service] = services
c.ttls[service] = time.Now().Add(c.opts.TTL)
for _, s := range services {
for _, n := range s.Nodes {
if c.nttls[s.Name] == nil {
c.nttls[s.Name] = make(map[string]time.Time)
}
c.nttls[s.Name][n.Id] = time.Now().Add(c.opts.TTL)
}
}
func (c *cache) updateNodeTTLs(name string, nodes []*registry.Node) {
if c.nttls[name] == nil {
c.nttls[name] = make(map[string]time.Time)
}
for _, node := range nodes {
c.nttls[name][node.Id] = time.Now().Add(c.opts.TTL)
}
}
@@ -260,6 +264,7 @@ func (c *cache) update(res *registry.Result) {
switch res.Action {
case "create", "update":
c.updateNodeTTLs(res.Service.Name, res.Service.Nodes)
if service == nil {
c.set(res.Service.Name, append(services, res.Service))
return
+2 -2
View File
@@ -1884,7 +1884,7 @@ github.com/xrash/smetrics
# github.com/yashtewari/glob-intersection v0.2.0
## explicit; go 1.17
github.com/yashtewari/glob-intersection
# go-micro.dev/v4 v4.11.0 => github.com/butonic/go-micro/v4 v4.11.1-0.20241028133844-ac2cdce96c71
# go-micro.dev/v4 v4.11.0 => github.com/butonic/go-micro/v4 v4.11.1-0.20241115112658-b5d4de5ed9b3
## explicit; go 1.18
go-micro.dev/v4
go-micro.dev/v4/api
@@ -2406,4 +2406,4 @@ stash.kopano.io/kgol/rndm
# github.com/egirna/icap-client => github.com/fschade/icap-client v0.0.0-20240802074440-aade4a234387
# github.com/unrolled/secure => github.com/DeepDiver1975/secure v0.0.0-20240611112133-abc838fb797c
# github.com/go-micro/plugins/v4/store/nats-js-kv => github.com/kobergj/plugins/v4/store/nats-js-kv v0.0.0-20240807130109-f62bb67e8c90
# go-micro.dev/v4 => github.com/butonic/go-micro/v4 v4.11.1-0.20241028133844-ac2cdce96c71
# go-micro.dev/v4 => github.com/butonic/go-micro/v4 v4.11.1-0.20241115112658-b5d4de5ed9b3