scheduler

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package scheduler provides three orthogonal in-process scheduling primitives: EventMap (lightweight time + group + phase event queue), TaskScheduler (callback-based delayed/repeating tasks), and Buckets (frequency-bucketed scheduling by relevance). All share a Clock interface for time-source injection in tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	Name     string
	Interval time.Duration
}

Bucket declares a tick frequency for items bucketed into it.

type Buckets

type Buckets[T any] struct {
	// contains filtered or unexported fields
}

Buckets groups items by relevance and ticks each group at a different frequency. Useful for game AI / observability polling where "hot" items need fast updates and "cold" ones don't.

func NewBuckets

func NewBuckets[T any](buckets []Bucket, bucketer func(T) string) *Buckets[T]

NewBuckets constructs a Buckets given the bucket definitions and a bucketer that maps each item to a bucket name. If the bucketer returns a name not in the configured set, the item is silently dropped.

func (*Buckets[T]) Add

func (b *Buckets[T]) Add(id uint64, item T)

Add inserts or re-buckets an item under its bucketer-derived bucket.

func (*Buckets[T]) Remove

func (b *Buckets[T]) Remove(id uint64)

Remove deletes an item.

func (*Buckets[T]) Tick

func (b *Buckets[T]) Tick(now time.Time, fn func(item T))

Tick invokes fn on every item whose bucket interval has elapsed since its last tick.

type Clock

type Clock interface {
	Now() time.Time
}

Clock supplies the current time. Tests inject a mock clock to drive schedulers deterministically.

type EventID

type EventID uint32

EventID is an opaque identifier supplied by the caller.

type EventMap

type EventMap struct {
	// contains filtered or unexported fields
}

EventMap is a min-heap-backed event queue keyed by due time. Schedule and Update are O(log n); Update emits due events in chronological order.

func NewEventMap

func NewEventMap(clock Clock) *EventMap

NewEventMap constructs an EventMap. Pass SystemClock{} for real time.

func (*EventMap) Cancel

func (m *EventMap) Cancel(id EventID)

Cancel removes an event. No-op if not present.

func (*EventMap) Reschedule

func (m *EventMap) Reschedule(id EventID, delay time.Duration)

Reschedule moves an existing event to a new due time. No-op if id not found.

func (*EventMap) Schedule

func (m *EventMap) Schedule(id EventID, delay time.Duration, group GroupID)

Schedule inserts an event due `delay` after the current clock time. If the group is non-zero, any existing event in the same group is cancelled.

func (*EventMap) Update

func (m *EventMap) Update(now time.Time, due *[]EventID)

Update drains events whose due time is at or before `now` into `due`, preserving the caller's slice for allocation-free reuse. Events are emitted in chronological order.

type GroupID

type GroupID uint32

GroupID groups events such that only one in a group is active at a time (new schedules into a group cancel any existing). Use 0 for ungrouped.

type MockClock

type MockClock struct {
	// contains filtered or unexported fields
}

MockClock is a deterministic clock for tests. Advance with Add.

func NewMockClock

func NewMockClock(start time.Time) *MockClock

func (*MockClock) Add

func (m *MockClock) Add(d time.Duration)

func (*MockClock) Now

func (m *MockClock) Now() time.Time

type SystemClock

type SystemClock struct{}

SystemClock returns time.Now().

func (SystemClock) Now

func (SystemClock) Now() time.Time

type TaskHandle

type TaskHandle uint64

TaskHandle identifies a scheduled task. Use it to Cancel before fire.

type TaskOpt

type TaskOpt func(*task)

TaskOpt customises a scheduled task.

func WithPredicate

func WithPredicate(pred func() bool) TaskOpt

WithPredicate makes the task fire only when pred() returns true. Returning false skips this firing without consuming a repeat.

func WithRepeats

func WithRepeats(n int) TaskOpt

WithRepeats fires the task n additional times at the same interval after the initial fire. -1 = forever.

type TaskScheduler

type TaskScheduler struct {
	// contains filtered or unexported fields
}

TaskScheduler is a callback-based scheduler supporting one-shot and repeating tasks with conditional predicates.

func NewTaskScheduler

func NewTaskScheduler(clock Clock) *TaskScheduler

NewTaskScheduler returns a TaskScheduler. Update() must be called from a single goroutine — usually the tick loop.

func (*TaskScheduler) Cancel

func (s *TaskScheduler) Cancel(h TaskHandle)

Cancel marks a task cancelled; the next Update drop pass removes it.

func (*TaskScheduler) Schedule

func (s *TaskScheduler) Schedule(delay time.Duration, fn func(ctx context.Context), opts ...TaskOpt) TaskHandle

Schedule adds a task due after `delay`. Returns a handle for cancellation.

func (*TaskScheduler) Update

func (s *TaskScheduler) Update(ctx context.Context, now time.Time)

Update fires all due tasks at or before `now`. Must be called from a single goroutine.

Jump to

Keyboard shortcuts

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