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 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 ¶
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.
type Clock ¶
Clock supplies the current time. Tests inject a mock clock to drive schedulers deterministically.
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 ¶
NewEventMap constructs an EventMap. Pass SystemClock{} for real time.
func (*EventMap) Reschedule ¶
Reschedule moves an existing event to a new due time. No-op if id not found.
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 ¶
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 ¶
WithPredicate makes the task fire only when pred() returns true. Returning false skips this firing without consuming a repeat.
func WithRepeats ¶
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.