dedup

package
v3.11.2 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

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

View Source
var ErrClosed = errors.New("dedup: channel is closed")

ErrClosed is returned by send operations on a closed Chan.

View Source
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]) Len

func (c *Chan[K, T]) Len() int

Len returns the number of queued values.

func (*Chan[K, T]) Recv

func (c *Chan[K, T]) Recv(ctx context.Context) (T, bool)

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

func (c *Chan[K, T]) Send(ctx context.Context, v T) (bool, error)

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.

func (*Chan[K, T]) TryRecv

func (c *Chan[K, T]) TryRecv() (T, bool)

TryRecv returns the next queued value without blocking, reporting false if none is queued.

func (*Chan[K, T]) TrySend

func (c *Chan[K, T]) TrySend(v T) (bool, error)

TrySend queues v without blocking. It reports whether v was queued; a false return with a nil error means an equal key was already queued. It returns ErrFull if the channel is at capacity and ErrClosed once it is closed.

Jump to

Keyboard shortcuts

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