Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update ttl for updated nodes only #2740

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions registry/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down
Loading