actionqueue

package module
v0.0.0-...-6793b93 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2018 License: MIT Imports: 4 Imported by: 1

README

actionqueue-go: A timed event queue with expiry support

Package actionqueue provides a timed event queue with expiry support.

Any event is added to the queue with two parameters, ActionTime and ExpireTime. The event will be fired upon ActionTime through the the NextEvent channel.

The caller may choose either to receive an event immediately, or at any time before it is expired. The queue takes care of the ExpireTime, and cancel expired events upon expiry.

Documentation

Overview

Package actionqueue provides a timed event queue with expiry support.

Any event is added to the queue with two parameters, ActionTime and ExpireTime. The event will be fired upon ActionTime through the the NextEvent channel.

The caller may choose either to receive an event immediately, or at any time before it is expired. The queue takes care of the ExpireTime, and cancel expired events upon expiry.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action struct {
	Value      interface{}
	ActionTime time.Time
	ExpireTime time.Time
	// contains filtered or unexported fields
}

An Action is an event that fires after ActionTime, and expires if not handled before ExpireTime.

If ExpireTime is zero-value, the action never expires.

type Queue

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

A Queue stores actions and triggers actions on time.

func New

func New() *Queue

New creates a new action queue.

func (*Queue) AddAction

func (q *Queue) AddAction(value interface{}, actionTime time.Time)

AddAction adds a new action to the queue.

func (*Queue) AddActionWithExpiry

func (q *Queue) AddActionWithExpiry(value interface{}, actionTime, expireTime time.Time)

AddActionWithExpiry adds a new action with an expireTime.

An Action is an event that triggers after ActionTime, and expires if not handled before ExpireTime.

func (*Queue) Dump

func (q *Queue) Dump() []*Action

Dump returns a copy of upcoming actions, useful when saving to disk.

To restore the actions back to a queue, insert them one by one.

Note 1: The Queue MUST be stopped before dumping, or the function will panic.

Note 2: Only one goroutine may call Dump at the same time.

func (*Queue) NextAction

func (q *Queue) NextAction() <-chan *Action

NextAction returns a channel for upcoming actions.

To receive next action, use <-q.NextAction().

Subsquential calls to NextAction returns the same channel.

func (*Queue) Run

func (q *Queue) Run(ctx context.Context)

Run runs the action queue in the background. It can be stopped by either calling Stop or canceling the context.

To receive next action, use <-q.NextAction().

func (*Queue) Stop

func (q *Queue) Stop()

Stop stops a running queue.

Jump to

Keyboard shortcuts

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