ax(node): rename ttl to timeToLive in MessageDeduplicator
AX Principle 1 — predictable names over short names. The field `ttl` requires context to decode; `timeToLive` is self-describing without a comment. Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
parent
67ecffed94
commit
dc2f415fc2
1 changed files with 8 additions and 8 deletions
|
|
@ -61,18 +61,18 @@ type MessageHandler func(conn *PeerConnection, msg *Message)
|
|||
// if dedup.IsDuplicate(msg.ID) { continue }
|
||||
// dedup.Mark(msg.ID)
|
||||
type MessageDeduplicator struct {
|
||||
seen map[string]time.Time
|
||||
mutex sync.RWMutex
|
||||
ttl time.Duration
|
||||
seen map[string]time.Time
|
||||
mutex sync.RWMutex
|
||||
timeToLive time.Duration
|
||||
}
|
||||
|
||||
// dedup := node.NewMessageDeduplicator(5 * time.Minute)
|
||||
// dedup.Mark(msg.ID)
|
||||
// if dedup.IsDuplicate(msg.ID) { continue }
|
||||
func NewMessageDeduplicator(ttl time.Duration) *MessageDeduplicator {
|
||||
func NewMessageDeduplicator(timeToLive time.Duration) *MessageDeduplicator {
|
||||
deduplicator := &MessageDeduplicator{
|
||||
seen: make(map[string]time.Time),
|
||||
ttl: ttl,
|
||||
seen: make(map[string]time.Time),
|
||||
timeToLive: timeToLive,
|
||||
}
|
||||
return deduplicator
|
||||
}
|
||||
|
|
@ -92,13 +92,13 @@ func (deduplicator *MessageDeduplicator) Mark(msgID string) {
|
|||
deduplicator.mutex.Unlock()
|
||||
}
|
||||
|
||||
// go dedup.Cleanup() // call periodically; entries older than ttl are dropped
|
||||
// go dedup.Cleanup() // call periodically; entries older than timeToLive are dropped
|
||||
func (deduplicator *MessageDeduplicator) Cleanup() {
|
||||
deduplicator.mutex.Lock()
|
||||
defer deduplicator.mutex.Unlock()
|
||||
now := time.Now()
|
||||
for id, seen := range deduplicator.seen {
|
||||
if now.Sub(seen) > deduplicator.ttl {
|
||||
if now.Sub(seen) > deduplicator.timeToLive {
|
||||
delete(deduplicator.seen, id)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue