Documentation
¶
Overview ¶
Package perkey provides a scheduler that serializes work per key while allowing work for different keys to execute concurrently.
Typical use-case: event-sourced aggregates, where you want to process commands per aggregate ID sequentially, but different aggregates in parallel.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrSchedulerClosed = &SchedulerError{"scheduler is closed"}
ErrSchedulerClosed is returned when Do is called on a closed scheduler.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*config)
Option configures a Scheduler.
func WithBufferSize ¶
WithBufferSize sets the task buffer size per worker (default: 64).
type Scheduler ¶
type Scheduler[K comparable] struct { // contains filtered or unexported fields }
Scheduler runs tasks (functions) such that for any given key K, tasks are executed sequentially, in submission order. Tasks for *different* keys can proceed in parallel.
func (*Scheduler[K]) Close ¶
func (s *Scheduler[K]) Close()
Close stops accepting new tasks and shuts down all workers. It waits for in-flight Do operations to finish enqueueing before closing worker channels. Existing tasks in queues will still be processed.
func (*Scheduler[K]) Do ¶
Do schedules fn to run for the given key. It blocks until fn finishes and returns its error. All fn calls for the same key are executed sequentially.
func (*Scheduler[K]) DoContext ¶
DoContext is like Do but respects context cancellation. If the context is cancelled while waiting to enqueue or waiting for completion, it returns the context error. Note that if a task is already enqueued, it will still execute even if the caller's context is cancelled.
type SchedulerError ¶
type SchedulerError struct {
// contains filtered or unexported fields
}
SchedulerError is a simple error implementation.
func (*SchedulerError) Error ¶
func (e *SchedulerError) Error() string