timed

package
v0.0.0-...-31dbb72 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: Apache-2.0, BSD-2-Clause Imports: 11 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithMaxQueueSize

func WithMaxQueueSize(maxSize int) options.Option[Executor]

WithMaxQueueSize is an ExecutorOption for the TimedExecutor that allows to specify a maxSize of the underlying queue.

func WithMaxSize

func WithMaxSize[T any](maxSize int) options.Option[Queue[T]]

WithMaxSize is an Option for the timed.Queue that allows to specify a maxSize of the 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

func NewExecutor(workerCount int, opts ...options.Option[Executor]) (timedExecutor *Executor)

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) Size

func (t *Executor) Size() int

Size returns the amount of jobs that are currently scheduled for execution.

func (*Executor) WorkerCount

func (t *Executor) WorkerCount() int

WorkerCount returns the amount of background workers that this executor uses.

type HeapKey

type HeapKey time.Time

func (HeapKey) CompareTo

func (t HeapKey) CompareTo(other HeapKey) int

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 NewQueue

func NewQueue[T any](opts ...options.Option[Queue[T]]) (queue *Queue[T])

NewQueue is the constructor for the timed Queue.

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

func (t *Queue[T]) IsShutdown() bool

IsShutdown returns true if this queue was shutdown.

func (*Queue[T]) Poll

func (t *Queue[T]) Poll(waitIfEmpty bool) T

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.

func (*Queue[T]) Size

func (t *Queue[T]) Size() int

Size returns the amount of elements that are currently enqueued in this queue.

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

type ShutdownFlag = bitmask.BitMask

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.

Jump to

Keyboard shortcuts

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