Documentation
¶
Overview ¶
Package pending provides a small in-memory scheduler for deferred tasks.
Tasks are keyed by ID, so scheduling with the same ID replaces the previous task (debouncing). The manager supports cancellation, graceful shutdown, and optional concurrency limits with block or drop strategies.
Runtime state can be inspected with Stats(), while IsPending() and TimeRemaining() provide per-task introspection.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrTaskDropped = errors.New("pending: task dropped due to concurrency limit")
ErrTaskDropped is reported to TelemetryHandler.OnFailed when StrategyDrop rejects a task because no concurrency slot is available.
Functions ¶
This section is empty.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager coordinates the lifecycle of delayed tasks, ensuring thread-safety and providing concurrency control via semaphores.
func NewManager ¶
NewManager initializes a new Manager with the provided options.
Example ¶
package main
import (
"context"
"errors"
"time"
)
type exampleTelemetry struct{}
func (exampleTelemetry) OnScheduled(id string, d time.Duration) {}
func (exampleTelemetry) OnRescheduled(id string) {}
func (exampleTelemetry) OnExecuted(id string, duration time.Duration) {}
func (exampleTelemetry) OnCancelled(id string) {}
func (exampleTelemetry) OnFailed(id string, err error) {
_ = errors.Is(err, ErrTaskDropped)
}
func main() {
mgr := NewManager(
WithLimit(1, StrategyDrop),
WithLogger(exampleTelemetry{}),
)
defer mgr.Shutdown(context.Background())
mgr.Schedule("email:user-42", 2*time.Second, func(ctx context.Context) {})
}
func (*Manager) Cancel ¶
Cancel immediately stops a pending task by its ID and prevents it from running. If the task is already running, its context is cancelled.
func (*Manager) IsPending ¶ added in v0.3.0
IsPending reports whether a task exists and has not started execution yet.
func (*Manager) Schedule ¶
Schedule plans a task for execution after duration d. If a task with the same id already exists, the previous one is cancelled and replaced (debouncing). If the manager is not accepting new tasks, Schedule does nothing.
func (*Manager) Shutdown ¶
Shutdown stops the manager, cancels all pending timers, and waits for currently executing tasks to complete or for the context to time out.
type Option ¶
type Option func(*Manager)
Option configures a Manager.
func WithLogger ¶
func WithLogger(logger TelemetryHandler) Option
WithLogger attaches a custom TelemetryHandler.
type Stats ¶ added in v0.1.1
type Stats struct {
// Pending is the number of scheduled tasks that are not currently executing.
Pending int
// Running is the number of tasks currently executing.
Running int
// Status indicates whether the manager is accepting new tasks, draining existing ones, or fully closed.
Status Status
}
Stats is a point-in-time snapshot of manager state.
type Status ¶ added in v0.2.0
type Status int
Status represents the current lifecycle state of a Manager.
type Strategy ¶
type Strategy int
Strategy defines how the manager behaves when the concurrency limit is reached.
type Task ¶
Task defines the function signature for a scheduled action. The provided context is cancelled if the manager shuts down or the task is replaced.
type TelemetryHandler ¶
type TelemetryHandler interface {
OnScheduled(id string, d time.Duration)
OnRescheduled(id string)
OnExecuted(id string, duration time.Duration)
OnCancelled(id string)
OnFailed(id string, err error)
}
TelemetryHandler receives lifecycle events for scheduled tasks.
Implementations can be used to emit logs, metrics, or traces.
