Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithMaxQueueSize ¶
WithMaxQueueSize is an ExecutorOption for the TimedExecutor that allows to specify a maxSize of the underlying queue.
Types ¶
type Executor ¶
type Executor struct {
// contains filtered or unexported fields
}
Executor defines a scheduler that executes tasks in the background at a given time. It does not spawn any additional goroutines for each task and executes the tasks sequentially (in each worker).
func NewExecutor ¶
NewExecutor is the constructor for a timed Executor that creates a scheduler with a given number of workers that execute the scheduled tasks in parallel (whenever they become due).
func (*Executor) ExecuteAfter ¶
func (t *Executor) ExecuteAfter(f func(), delay time.Duration) *ScheduledTask
ExecuteAfter executes the given function after the given delay.
func (*Executor) ExecuteAt ¶
func (t *Executor) ExecuteAt(f func(), time time.Time) *ScheduledTask
ExecuteAt executes the given function at the given time.
func (*Executor) Shutdown ¶
func (t *Executor) Shutdown(optionalShutdownFlags ...ShutdownFlag)
Shutdown shuts down the TimedExecutor and waits until the executor has shutdown gracefully.
func (*Executor) WorkerCount ¶
WorkerCount returns the amount of background workers that this executor uses.
type PriorityQueue ¶
type PriorityQueue[ElementType any] interface { // Push adds an element to the queue with the given time. Push(element ElementType, time time.Time) // Peek returns the element with the highest priority without removing it. Peek() (element ElementType, exists bool) // Pop removes the element with the highest priority from the queue. Pop() (element ElementType, exists bool) // PopUntil removes elements from the top of the queue until the given time. PopUntil(time time.Time) []ElementType // PopAll removes all elements from the queue. PopAll() []ElementType // Size returns the number of elements in the queue. Size() int // IsEmpty returns true if the queue is empty. IsEmpty() bool }
PriorityQueue is a priority queue whose elements are sorted by time.
func NewPriorityQueue ¶
func NewPriorityQueue[T any](ascending ...bool) PriorityQueue[T]
NewPriorityQueue creates a new PriorityQueue that can optionally be set to ascending order (oldest element first).
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
Queue represents a queue, that holds values that will only be released at a given time. The corresponding Poll method waits for the element to be available before it returns its value and is therefore blocking.
func (*Queue[T]) Add ¶
func (t *Queue[T]) Add(value T, scheduledTime time.Time) (addedElement *QueueElement[T])
Add inserts a new element into the queue that can be retrieved via Poll() at the specified time.
func (*Queue[T]) IsShutdown ¶
IsShutdown returns true if this queue was shutdown.
func (*Queue[T]) Poll ¶
Poll returns the first value of this queue. It waits for the scheduled time before returning and is therefore blocking. It returns nil if the queue is empty.
func (*Queue[T]) Shutdown ¶
func (t *Queue[T]) Shutdown(optionalShutdownFlags ...ShutdownFlag)
Shutdown terminates the queue. It accepts an optional list of shutdown flags that allows the caller to modify the shutdown behavior.
type QueueElement ¶
type QueueElement[T any] struct { // Value represents the value of the queued element. Value T // contains filtered or unexported fields }
QueueElement is an element in the TimedQueue. It.
func (*QueueElement[T]) Cancel ¶
func (timedQueueElement *QueueElement[T]) Cancel()
Cancel removed the given element from the queue and cancels its execution.
type ScheduledTask ¶
type ScheduledTask = QueueElement[func()]
type ShutdownFlag ¶
ShutdownFlag defines the type of the optional shutdown flags.
const ( // CancelPendingElements defines a shutdown flag, that causes the queue to be emptied on shutdown. CancelPendingElements ShutdownFlag = 1 << iota // IgnorePendingTimeouts defines a shutdown flag, that makes the queue ignore the timeouts of the remaining queued // elements. Consecutive calls to Poll will immediately return these elements. IgnorePendingTimeouts // PanicOnModificationsAfterShutdown makes the queue panic instead of ignoring consecutive writes or modifications. PanicOnModificationsAfterShutdown // DontWaitForShutdown causes the TimedExecutor to not wait for all tasks to be executed before returning from the // Shutdown method. DontWaitForShutdown ShutdownFlag = 1 << 7 )
type TaskExecutor ¶
type TaskExecutor[T comparable] struct { *Executor // contains filtered or unexported fields }
TaskExecutor is a TimedExecutor that internally manages the scheduled callbacks as tasks with a unique identifier. It allows to replace existing scheduled tasks and cancel them using the same identifier.
func NewTaskExecutor ¶
func NewTaskExecutor[T comparable](workerCount int, opts ...options.Option[Executor]) *TaskExecutor[T]
NewTaskExecutor is the constructor of the TaskExecutor.
func (*TaskExecutor[T]) Cancel ¶
func (t *TaskExecutor[T]) Cancel(identifier T) (canceled bool)
Cancel cancels a queued task.
func (*TaskExecutor[T]) ExecuteAfter ¶
func (t *TaskExecutor[T]) ExecuteAfter(identifier T, callback func(), delay time.Duration) *ScheduledTask
ExecuteAfter executes the given function after the given delay.
func (*TaskExecutor[T]) ExecuteAt ¶
func (t *TaskExecutor[T]) ExecuteAt(identifier T, callback func(), executionTime time.Time) *ScheduledTask
ExecuteAt executes the given function at the given time.