Documentation
¶
Overview ¶
Package timewheel provides a generic timer wheel implementation.
TimeWheel is safe for concurrent use. Timer placement and deletion are serialized through the wheel event loop; job execution is dispatched outside the event loop. Wheel slots are owned exclusively by the event loop goroutine and are accessed without locks.
Index ¶
- Variables
- type BackpressurePolicy
- type Clock
- type Job
- type JobContext
- type JobEvent
- type JobObserver
- type Logger
- type Option
- func WithClock[T any](clk Clock) Option[T]
- func WithErrorHandler[T any](h func(recovered any)) Option[T]
- func WithJobObserver[T any](observer JobObserver[T]) Option[T]
- func WithLogger[T any](l Logger) Option[T]
- func WithWorkerPool[T any](workers int, queueSize int, policy BackpressurePolicy) Option[T]
- type RepeatMode
- type RepeatOptions
- type Stats
- type Ticker
- type TimeWheel
- func (tw *TimeWheel[T]) AddRepeatingTimer(delay time.Duration, data T, opts RepeatOptions) (TimerID, error)
- func (tw *TimeWheel[T]) AddRepeatingTimerWithContextJob(delay time.Duration, data T, job JobContext[T], opts RepeatOptions) (TimerID, error)
- func (tw *TimeWheel[T]) AddRepeatingTimerWithJob(delay time.Duration, data T, job Job[T], opts RepeatOptions) (TimerID, error)
- func (tw *TimeWheel[T]) AddTimer(delay time.Duration, data T) (TimerID, error)
- func (tw *TimeWheel[T]) AddTimerFunc(delay time.Duration, fn func()) (TimerID, error)
- func (tw *TimeWheel[T]) AddTimerWithContextJob(delay time.Duration, data T, job JobContext[T]) (TimerID, error)
- func (tw *TimeWheel[T]) AddTimerWithJob(delay time.Duration, data T, job Job[T]) (TimerID, error)
- func (tw *TimeWheel[T]) Close() error
- func (tw *TimeWheel[T]) NextFireTime(id TimerID) (time.Time, bool)
- func (tw *TimeWheel[T]) PendingTimers() []TimerInfo
- func (tw *TimeWheel[T]) RemoveTimer(id TimerID) error
- func (tw *TimeWheel[T]) Start(ctx context.Context) error
- func (tw *TimeWheel[T]) Stats() Stats
- func (tw *TimeWheel[T]) Stop() error
- func (tw *TimeWheel[T]) Wait()
- type TimerID
- type TimerInfo
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotStarted = errors.New("timewheel: not started") ErrRunning = errors.New("timewheel: already running") ErrClosed = errors.New("timewheel: closed") ErrNilContext = errors.New("timewheel: nil context") ErrNilJob = errors.New("timewheel: nil job") ErrUnknownTimer = errors.New("timewheel: unknown timer") ErrQueueFull = errors.New("timewheel: worker queue full") )
Functions ¶
This section is empty.
Types ¶
type BackpressurePolicy ¶ added in v0.2.0
type BackpressurePolicy uint8
BackpressurePolicy controls behavior when the bounded worker queue is full.
const ( // Block waits for queue capacity unless the wheel is shutting down. Block BackpressurePolicy = iota // Drop records a dropped job and does not run it when the queue is full. Drop // RunInline runs the job on the event loop when the queue is full. RunInline )
type Clock ¶ added in v0.4.0
Clock abstracts the time source used by the wheel. Implementations must be safe for concurrent use. The default clock uses the time package.
type JobContext ¶ added in v0.2.0
JobContext is the context-aware callback signature invoked when a timer fires.
type JobEvent ¶ added in v0.2.0
type JobEvent[T any] struct { TimerID TimerID Data T StartedAt time.Time FinishedAt time.Time ScheduledFor time.Time Lateness time.Duration Duration time.Duration Err error Panic any Dropped bool Skipped bool }
JobEvent describes the result of one job scheduling or execution attempt.
type JobObserver ¶ added in v0.2.0
JobObserver receives job execution, drop, and skip events.
type Option ¶
type Option[T any] func(*config[T])
Option is a functional option for New.
func WithClock ¶ added in v0.4.0
WithClock overrides the wheel's time source. A nil clock keeps the default real-time clock. Intended for tests that need deterministic time.
func WithErrorHandler ¶
WithErrorHandler registers a function called with recovered job panics.
func WithJobObserver ¶ added in v0.2.0
func WithJobObserver[T any](observer JobObserver[T]) Option[T]
WithJobObserver registers a function called for job execution, drop, and skip events.
func WithLogger ¶
WithLogger configures the logger used for internal diagnostic messages.
func WithWorkerPool ¶
func WithWorkerPool[T any](workers int, queueSize int, policy BackpressurePolicy) Option[T]
WithWorkerPool configures a fixed worker pool and bounded queue.
workers <= 0 disables the pool and runs each job in its own goroutine.
type RepeatMode ¶ added in v0.2.0
type RepeatMode uint8
RepeatMode controls how a repeating timer schedules its next execution.
const ( // FixedRate schedules the next fire when the current fire is dispatched. // Jobs may overlap. FixedRate RepeatMode = iota // FixedDelay schedules the next fire after the previous job returns. // Jobs do not overlap. FixedDelay // SkipIfRunning keeps a fixed-rate cadence but skips a fire when the // previous job for the same timer is still running. SkipIfRunning )
type RepeatOptions ¶ added in v0.2.0
type RepeatOptions struct {
// Mode defaults to FixedRate.
Mode RepeatMode
}
RepeatOptions configures repeating timer behavior.
type Stats ¶
type Stats struct {
Pending int64
Executed int64
Removed int64
Queued int64
Running int64
Dropped int64
Skipped int64
}
Stats is a snapshot of runtime counters. All fields are read atomically.
type TimeWheel ¶
type TimeWheel[T any] struct { // contains filtered or unexported fields }
TimeWheel is a generic timer wheel.
Use New to construct a wheel, Start to run it, and Close to stop and wait. The zero value is not usable.
func New ¶
func New[T any](interval time.Duration, slotNum int, defaultJob Job[T], opts ...Option[T]) (*TimeWheel[T], error)
New creates and initialises a new TimeWheel.
func (*TimeWheel[T]) AddRepeatingTimer ¶ added in v0.2.0
func (tw *TimeWheel[T]) AddRepeatingTimer(delay time.Duration, data T, opts RepeatOptions) (TimerID, error)
AddRepeatingTimer enqueues a repeating timer that uses the default job.
func (*TimeWheel[T]) AddRepeatingTimerWithContextJob ¶ added in v0.2.0
func (tw *TimeWheel[T]) AddRepeatingTimerWithContextJob(delay time.Duration, data T, job JobContext[T], opts RepeatOptions) (TimerID, error)
AddRepeatingTimerWithContextJob enqueues a repeating timer with a context-aware job.
func (*TimeWheel[T]) AddRepeatingTimerWithJob ¶ added in v0.2.0
func (tw *TimeWheel[T]) AddRepeatingTimerWithJob(delay time.Duration, data T, job Job[T], opts RepeatOptions) (TimerID, error)
AddRepeatingTimerWithJob enqueues a repeating timer with a per-timer job.
func (*TimeWheel[T]) AddTimer ¶
AddTimer enqueues a one-shot timer that uses the wheel's default job.
func (*TimeWheel[T]) AddTimerFunc ¶
AddTimerFunc enqueues a one-shot timer that calls fn after delay.
func (*TimeWheel[T]) AddTimerWithContextJob ¶ added in v0.2.0
func (tw *TimeWheel[T]) AddTimerWithContextJob(delay time.Duration, data T, job JobContext[T]) (TimerID, error)
AddTimerWithContextJob enqueues a one-shot timer with a context-aware job.
func (*TimeWheel[T]) AddTimerWithJob ¶
AddTimerWithJob enqueues a one-shot timer with a per-timer job.
func (*TimeWheel[T]) Close ¶ added in v0.2.0
Close stops the wheel and waits for the event loop and worker pool to exit.
func (*TimeWheel[T]) NextFireTime ¶
NextFireTime returns the estimated next fire time for a pending timer.
func (*TimeWheel[T]) PendingTimers ¶
PendingTimers returns a sorted snapshot of pending timers.
func (*TimeWheel[T]) RemoveTimer ¶
RemoveTimer cancels future not-yet-started executions for id.