cleanup contention.go docs, Observe param name

Signed-off-by: redwrasse <mail@redwrasse.io>
This commit is contained in:
redwrasse 2023-09-16 22:21:15 -07:00
parent 840d486923
commit 55fa8c9f56

View File

@ -27,8 +27,7 @@ import (
type TimeoutDetector struct { type TimeoutDetector struct {
mu sync.Mutex // protects all mu sync.Mutex // protects all
maxDuration time.Duration maxDuration time.Duration
// map from event to time // map from event to last seen time of event.
// time is the last seen time of the event.
records map[uint64]time.Time records map[uint64]time.Time
} }
@ -40,7 +39,7 @@ func NewTimeoutDetector(maxDuration time.Duration) *TimeoutDetector {
} }
} }
// Reset resets the NewTimeoutDetector. // Reset resets the TimeoutDetector.
func (td *TimeoutDetector) Reset() { func (td *TimeoutDetector) Reset() {
td.mu.Lock() td.mu.Lock()
defer td.mu.Unlock() defer td.mu.Unlock()
@ -48,9 +47,11 @@ func (td *TimeoutDetector) Reset() {
td.records = make(map[uint64]time.Time) td.records = make(map[uint64]time.Time)
} }
// Observe observes an event for given id. It returns false and exceeded duration // Observe observes an event of given id. It computes
// if the interval is longer than the expectation. // the time elapsed between successive events of given id.
func (td *TimeoutDetector) Observe(which uint64) (bool, time.Duration) { // It returns whether this time elapsed exceeds the expectation,
// and the amount by which it exceeds the expectation.
func (td *TimeoutDetector) Observe(id uint64) (bool, time.Duration) {
td.mu.Lock() td.mu.Lock()
defer td.mu.Unlock() defer td.mu.Unlock()
@ -58,12 +59,12 @@ func (td *TimeoutDetector) Observe(which uint64) (bool, time.Duration) {
now := time.Now() now := time.Now()
exceed := time.Duration(0) exceed := time.Duration(0)
if pt, found := td.records[which]; found { if pt, found := td.records[id]; found {
exceed = now.Sub(pt) - td.maxDuration exceed = now.Sub(pt) - td.maxDuration
if exceed > 0 { if exceed > 0 {
ok = false ok = false
} }
} }
td.records[which] = now td.records[id] = now
return ok, exceed return ok, exceed
} }