dedup

package
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2026 License: GPL-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package dedup provides a time-windowed deduplication cache suitable for suppressing recently-seen APRS/AX.25 frames across the transmit governor, the digipeater, and the iGate.

Window is generic over a key type and an optional per-entry value type. Callers that only need presence tracking use struct{} for the value; callers that need to carry a small amount of metadata (e.g. the iGate's "was the previous packet a fixed-station beacon" bit) parameterize Window with a richer value type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// TTL is the suppression window. Required; a non-positive value
	// panics in New to surface mis-configuration early.
	TTL time.Duration
	// GCThreshold is the map size at which opportunistic eviction of
	// expired entries runs. Zero means defaultGCThreshold.
	GCThreshold int
	// Now overrides the clock for tests. Zero means time.Now.
	Now func() time.Time
}

Config controls Window behavior.

type Window

type Window[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Window is a time-based dedup cache. Keys are compared for equality; entries are evicted lazily when the caller touches the Window and the entry is older than the configured TTL. A background goroutine is not used.

func New

func New[K comparable, V any](cfg Config) *Window[K, V]

New builds a Window. Panics if cfg.TTL <= 0.

func (*Window[K, V]) Has

func (w *Window[K, V]) Has(k K) bool

Has reports whether k is present within TTL without recording.

func (*Window[K, V]) Len

func (w *Window[K, V]) Len() int

Len returns the current number of tracked entries. Intended for metrics and tests; does not run GC.

func (*Window[K, V]) Peek

func (w *Window[K, V]) Peek(k K) (V, time.Time, bool)

Peek returns the most recent recorded value and time for k, and whether k is currently tracked. Does not evict or refresh. The returned time may be older than the TTL; callers that care about TTL-bounded presence should compare against the configured window.

func (*Window[K, V]) Record

func (w *Window[K, V]) Record(k K, v V)

Record stores (k, v) at the current time without returning whether k was previously present. Use after a capacity or condition check that is independent of the dedup state.

func (*Window[K, V]) Reset

func (w *Window[K, V]) Reset()

Reset clears all entries.

func (*Window[K, V]) Seen

func (w *Window[K, V]) Seen(k K, v V) (V, bool)

Seen records (k, v) at the current time and returns the previous value and true if k was already present within TTL before this call; otherwise returns (zero, false) after recording (k, v). The clock is sampled once per call.

func (*Window[K, V]) SetTTL

func (w *Window[K, V]) SetTTL(ttl time.Duration)

SetTTL replaces the suppression window. Existing entries are kept and will be re-evaluated against the new TTL on the next touch. A non-positive duration is ignored so callers driving this from a config reload cannot accidentally disable suppression.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL