+autoremoval of stale discovery records

This commit is contained in:
Stephan D
2026-02-27 02:53:55 +01:00
parent 55ddfff4f2
commit fc5d44364b
3 changed files with 225 additions and 12 deletions

View File

@@ -181,6 +181,26 @@ func (r *Registry) Delete(key string) bool {
return true
}
// DeleteStale removes entries whose heartbeat timeout has elapsed.
func (r *Registry) DeleteStale(now time.Time) []string {
r.mu.Lock()
defer r.mu.Unlock()
removed := make([]string, 0)
for key, entry := range r.entries {
if entry == nil || !entry.isStaleAt(now) {
continue
}
delete(r.entries, key)
r.unindexEntry(key, entry)
removed = append(removed, key)
}
if len(removed) == 0 {
return nil
}
return removed
}
func (r *Registry) List(now time.Time, onlyHealthy bool) []RegistryEntry {
r.mu.Lock()
defer r.mu.Unlock()
@@ -789,3 +809,17 @@ func (e *RegistryEntry) isHealthyAt(now time.Time) bool {
}
return now.Sub(e.LastHeartbeat) <= timeout
}
func (e *RegistryEntry) isStaleAt(now time.Time) bool {
if e == nil {
return false
}
if e.LastHeartbeat.IsZero() {
return true
}
timeout := time.Duration(e.Health.TimeoutSec) * time.Second
if timeout <= 0 {
timeout = time.Duration(DefaultHealthTimeoutSec) * time.Second
}
return now.Sub(e.LastHeartbeat) > timeout
}