discovery stale records dropout implemented
This commit is contained in:
@@ -150,21 +150,34 @@ func (r *Registry) UpdateHeartbeat(id string, instanceID string, status string,
|
||||
if instanceID != "" && entry.InstanceID != instanceID {
|
||||
continue
|
||||
}
|
||||
wasHealthy := entry.isHealthyAt(now)
|
||||
entry.Status = status
|
||||
entry.LastHeartbeat = ts
|
||||
entry.Healthy = entry.isHealthyAt(now)
|
||||
|
||||
results = append(results, UpdateResult{
|
||||
Entry: *entry,
|
||||
IsNew: false,
|
||||
WasHealthy: wasHealthy,
|
||||
BecameHealthy: !wasHealthy && entry.Healthy,
|
||||
})
|
||||
results = append(results, updateHeartbeatEntry(entry, status, ts, now))
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func (r *Registry) UpdateHeartbeatByKey(key string, status string, ts time.Time, now time.Time) (UpdateResult, bool) {
|
||||
key = strings.TrimSpace(key)
|
||||
if key == "" {
|
||||
return UpdateResult{}, false
|
||||
}
|
||||
if status == "" {
|
||||
status = "ok"
|
||||
}
|
||||
if ts.IsZero() {
|
||||
ts = now
|
||||
}
|
||||
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
entry := r.entries[key]
|
||||
if entry == nil {
|
||||
return UpdateResult{}, false
|
||||
}
|
||||
|
||||
return updateHeartbeatEntry(entry, status, ts, now), true
|
||||
}
|
||||
|
||||
func (r *Registry) Delete(key string) bool {
|
||||
key = strings.TrimSpace(key)
|
||||
if key == "" {
|
||||
@@ -181,6 +194,20 @@ func (r *Registry) Delete(key string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func updateHeartbeatEntry(entry *RegistryEntry, status string, ts time.Time, now time.Time) UpdateResult {
|
||||
wasHealthy := entry.isHealthyAt(now)
|
||||
entry.Status = status
|
||||
entry.LastHeartbeat = ts
|
||||
entry.Healthy = entry.isHealthyAt(now)
|
||||
|
||||
return UpdateResult{
|
||||
Entry: *entry,
|
||||
IsNew: false,
|
||||
WasHealthy: wasHealthy,
|
||||
BecameHealthy: !wasHealthy && entry.Healthy,
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteStale removes entries whose heartbeat timeout has elapsed.
|
||||
func (r *Registry) DeleteStale(now time.Time) []string {
|
||||
r.mu.Lock()
|
||||
|
||||
Reference in New Issue
Block a user