diff options
Diffstat (limited to 'dedupe.go')
| -rw-r--r-- | dedupe.go | 18 |
1 files changed, 14 insertions, 4 deletions
@@ -8,13 +8,17 @@ import ( "time" ) +// PacketDeduplicator is a structure that prevents processing of duplicate packets. +// It keeps a record of seen packets and the time they were last seen. type PacketDeduplicator struct { - hasher hash.Hash - expiresAfter time.Duration - sync.RWMutex - seen map[string]time.Time + hasher hash.Hash // hasher is used for generating packet identifiers. + expiresAfter time.Duration // expiresAfter defines the duration after which a seen packet record expires. + sync.RWMutex // RWMutex is used to protect the seen map from concurrent access. + seen map[string]time.Time // seen maps a packet identifier to the last time it was seen. } +// NewDeduplicator creates a new PacketDeduplicator with a given hasher and expiration duration for packet records. +// It starts a background goroutine to periodically clean up expired packet records. func NewDeduplicator(hasher hash.Hash, expiresAfter time.Duration) *PacketDeduplicator { pd := PacketDeduplicator{ seen: make(map[string]time.Time), @@ -36,6 +40,9 @@ func NewDeduplicator(hasher hash.Hash, expiresAfter time.Duration) *PacketDedupl return &pd } + +// Seen checks whether a packet with the given sender and packetID has been seen before. +// If not, it records the packet as seen and returns false. Otherwise, it returns true. func (p *PacketDeduplicator) Seen(sender, packetID uint32) bool { asString := fmt.Sprintf("%d-%d", sender, packetID) p.RLock() @@ -49,6 +56,9 @@ func (p *PacketDeduplicator) Seen(sender, packetID uint32) bool { p.RUnlock() return true } + +// SeenData checks whether the data has been seen before based on its hashed value. +// If not, it records the data as seen and returns false. Otherwise, it returns true. func (p *PacketDeduplicator) SeenData(data []byte) bool { hashed := p.hasher.Sum(data) asHex := hex.EncodeToString(hashed) |
