toq

package
v0.0.0-...-157c9c8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package toq provides a queue for performing a timeout action after a constant period of time. It generates almost no garbage (only when it has to grow it's internal slice). It is threadsafe. It runs a Go routine only when there are timeout actions in the queue.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TimeoutAction

type TimeoutAction func()

TimeoutAction is what is called when a timeout occures. It will be called in it's own Go routine unless it is invoked from Flush.

type TimeoutQueue

type TimeoutQueue struct {
	// contains filtered or unexported fields
}

TimeoutQueue manages a queue of TimeoutActions that may be canceled before they timeout. The timeout duration is constant within a queue.

func New

func New(timeout time.Duration, capacity int) *TimeoutQueue

New returns a TimeoutQueue. This is the point at which timeout is set and cannot be changed. The capacity determines the capacity of the internal slice. The queue will grow in size as need, but will not shrink. Providing enough initial capacity will reduce the copy cost of growing the internal slice.

func (*TimeoutQueue) Add

func (tq *TimeoutQueue) Add(action TimeoutAction) Token

Add takes a TimeoutAction and adds it to the queue. The TimeoutAction will be called after the TimeoutQueue's timeout duration unless modified by a Token method.

func (*TimeoutQueue) Flush

func (tq *TimeoutQueue) Flush()

Flush calls the TimeoutAction on everything in the queue. Actions are not called in Go routines so that when Flush returns all Actions are complete.

func (*TimeoutQueue) SetTimeout

func (tq *TimeoutQueue) SetTimeout(timeout time.Duration)

SetTimeout changes the timeout duration of the queue. Everything in the queue will have it's timeout updated relative to when it was was added or reset. So if the timeout is reset from 5ms to 10ms and there is a TimeoutAction in the queueadded 3ms ago, it will go from expiring 2ms in the future to 7ms in the future.

func (*TimeoutQueue) Timeout

func (tq *TimeoutQueue) Timeout() time.Duration

Timeout duration before the TimeoutAction is called.

type Token

type Token interface {

	// Cancel will remove the TimeoutAction from the queue. The returned bool
	// indicates if the Cancel happened. Returning false means that the
	// TimeoutAction was either previously canceled or the TimeoutAction has
	// already run.
	Cancel() bool
	// Reset the timeout to the TimeoutQueue's duration. The returned bool
	// indicates if the Cancel happened. Returning false means that the
	// TimeoutAction was either previously canceled or the TimeoutAction has
	// already run.
	Reset() bool
	// contains filtered or unexported methods
}

Token represents a TimeoutAction that was registered.

Jump to

Keyboard shortcuts

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