perkey

package
v0.30.2 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: MIT Imports: 2 Imported by: 0

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

View Source
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

func WithBufferSize(size int) Option

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 New

func New[K comparable](opts ...Option) *Scheduler[K]

New creates a new Scheduler.

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

func (s *Scheduler[K]) Do(key K, fn func() error) error

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

func (s *Scheduler[K]) DoContext(ctx context.Context, key K, fn func() error) error

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

Jump to

Keyboard shortcuts

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