Documentation
¶
Overview ¶
Package dedup provides a bounded, de-duplicating channel. A value whose key is already queued is dropped instead of being enqueued a second time, which makes it suitable for coalescing repeated notifications about the same entity into a single unit of work.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("dedup: channel is closed")
ErrClosed is returned by send operations on a closed Chan.
var ErrFull = errors.New("dedup: channel is full")
ErrFull is returned by TrySend when the channel is at capacity.
Functions ¶
This section is empty.
Types ¶
type Chan ¶
type Chan[K comparable, T any] struct { // contains filtered or unexported fields }
Chan is a buffered channel of T that holds at most one queued value per key K. Keys are derived from values and stay reserved until the value is received, so a duplicate is only dropped while its predecessor is still waiting to be consumed.
Chan is safe for concurrent use. It is not a Go channel and cannot be used in a select statement; the context aware Send and Recv serve that purpose.
func NewChan ¶
func NewChan[K comparable, T any](size int, key func(T) K) (*Chan[K, T], error)
NewChan returns a Chan buffering up to size values, keyed by key. key must return the same result every time it is called for a given value.
func (*Chan[K, T]) Close ¶
func (c *Chan[K, T]) Close()
Close stops further sends. No value is queued once Close returns, and every value a successful send queued stays available to Recv. Close may be called more than once.
func (*Chan[K, T]) Recv ¶
Recv returns the next queued value, blocking until one arrives. It reports false once ctx ends, or once the channel is closed and drained. Receiving a value frees its key, so an equal value may be queued again.
func (*Chan[K, T]) Send ¶
Send queues v, blocking while the channel is full. It reports whether v was queued; a false return with a nil error means an equal key was already queued. It returns ErrClosed once the channel is closed and the context error if ctx ends first.