timed

package
v1.0.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2023 License: Apache-2.0, BSD-2-Clause Imports: 9 Imported by: 2

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(maxSize int) options.Option[Queue]

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 Queue

type Queue 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(opts ...options.Option[Queue]) (queue *Queue)

NewQueue is the constructor for the timed Queue.

func (*Queue) Add

func (t *Queue) Add(value any, scheduledTime time.Time) (addedElement *QueueElement)

Add inserts a new element into the queue that can be retrieved via Poll() at the specified time.

func (*Queue) IsShutdown

func (t *Queue) IsShutdown() bool

IsShutdown returns true if this queue was shutdown.

func (*Queue) Poll

func (t *Queue) Poll(waitIfEmpty bool) any

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

func (t *Queue) 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) Size

func (t *Queue) Size() int

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

type QueueElement

type QueueElement struct {
	// Value represents the value of the queued element.
	Value any
	// contains filtered or unexported fields
}

QueueElement is an element in the TimedQueue. It.

func (*QueueElement) Cancel

func (timedQueueElement *QueueElement) Cancel()

Cancel removed the given element from the queue and cancels its execution.

type ScheduledTask

type ScheduledTask = QueueElement

ScheduledTask is.

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