+autoremoval of stale discovery records
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user