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.
type ProcessFunc ¶
type ProcessFunc[T comparable] func(context.Context, T) error
Click to show internal directories.
Click to hide internal directories.