debouncer

package
v0.0.0-...-56fe054 Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBufferFull = errors.New("debouncer buffer full")
)

Functions

This section is empty.

Types

type DebouncerOpts

type DebouncerOpts[T comparable] struct {
	// Name should be a unique name for this debouncer group. It is
	// also used a name label value for the metrics.
	Name string
	// BufferSize is the maximum number of pending events to buffer.
	BufferSize int

	// ErrorHandler is the function that is called when a process for a given
	// key returns an error while running.
	ErrorHandler ErrorFunc[T]
	// ProcessHandler is the function that is called once a process for a given
	// key should be run.
	ProcessHandler ProcessFunc[T]
	// MinWait is the cooldown period after receiving an event. If another event with the
	// same key arrives during this period, the timer resets and we wait another MinWait duration.
	MinWait time.Duration
	// MaxWait is the maximum time any event will wait before processing. Even if new events
	// for the same key keep arriving, we guarantee processing after MaxWait from the first event.
	MaxWait time.Duration
	Reg     prometheus.Registerer
	// contains filtered or unexported fields
}

DebouncerOpts hold all the options to create a debouncer group.

type ErrorFunc

type ErrorFunc[T comparable] func(T, error)

type Group

type Group[T comparable] struct {
	// contains filtered or unexported fields
}

func NewGroup

func NewGroup[T comparable](opts DebouncerOpts[T]) (*Group[T], error)

NewGroup creates a new debouncer group for processing events with unique keys.

A debouncer group helps optimize expensive operations by: 1. Grouping identical events that occur in rapid succession 2. Processing each unique key only once after waiting periods expire

Example usage:

group := debouncer.NewGroup(DebouncerOpts[string]{
	BufferSize:     1000,
	ProcessHandler: func(ctx context.Context, key string) error {
	  // This is where you perform the expensive operation
	  return doSuperExpensiveCommand(key)
	}
	MinWait:        time.Second * 10,
	MaxWait:        time.Minute,
})

// Start the debouncer group.
group.Start(ctx)

// Queue events
if err := group.Add("user-1"); err != nil {
  // Do something with the error.
}
// Adding the same key resets MinWait but not MaxWait
if err := group.Add("user-1"); err != nil {
  // Do something with the error.
}

The event will be processed when either MinWait expires (after the most recent add) or MaxWait expires (after the first add), whichever comes first.

func (*Group[T]) Add

func (g *Group[T]) Add(value T) error

Add will create a new debouncer for the given Key if it doesn't exist yet. If a key has already a debouncer it will either reset the MinWait timer for this key, or if they key is already running its process be no-op.

func (*Group[T]) Start

func (g *Group[T]) Start(ctx context.Context)

func (*Group[T]) Stop

func (g *Group[T]) Stop()

type ProcessFunc

type ProcessFunc[T comparable] func(context.Context, T) error

Jump to

Keyboard shortcuts

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