timeutil

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2022 License: MIT Imports: 6 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type C

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

C converts timeouts to cancellation. This simulates the behaviour of other objects with a SetDeadline method, but using a cancellation function. SetDeadline should be called before each call to WithCancel, just as when working with, for example, an os.File, SetDeadline should be called prior to each call to Read. For example, one can use a context and its cancel function:

ctx, cancel := context.WithCancel(parentContext)
cancellable := timeutil.NewCancellable(cancel)
defer cancellable.Stop()
cancellable.SetDeadline(deadline)
cancellable.WithCancel(func() error {
	// do something or other
	// checking ctx for cancellation
	// The context ctx will fire if the deadline is exceeded.
})

func NewCancellable

func NewCancellable(cancel context.CancelFunc) *C

NewCancellable creates a new C, starting a background worker handling timeouts. The user must call Stop, otherwise resources will leak. See the documentation for C for further discussion and example usage.

func (*C) SetDeadline

func (c *C) SetDeadline(t time.Time) error

SetDeadline sets the deadline for future WithCancel calls and any currently-blocked WithCancel calls. A zero value for t means WithCancel will not time out.

func (*C) Stop

func (c *C) Stop()

Stop stops the background timeout worker.

func (*C) WithCancel

func (c *C) WithCancel(f func() error) error

WithCancel permits the wrapped cancel function to be called whilst executing f.

type CallbackFunc

type CallbackFunc func(x interface{})

CallbackFunc is the function that will be called on timeout.

type LessFunc

type LessFunc func(x interface{}, y interface{}) bool

LessFunc returns true iff x < y.

type Schedule

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

Schedule automates calling a callback function at certain times, with certain data.

func NewSchedule

func NewSchedule(f CallbackFunc, less LessFunc) *Schedule

NewSchedule returns a new Schedule with given callback function. Note: There is currently no way to stop the background workers associated with a Schedule.

func (*Schedule) Insert

func (w *Schedule) Insert(t time.Time, x interface{}) (match interface{})

Insert adds the item x to be passed to the callback function at time t. If a matching item already exists at that time position, it will be replaced and the matching item returned.

func (*Schedule) Len

func (w *Schedule) Len() (n int)

Len returns the number of items in the Schedule.

func (*Schedule) Remove

func (w *Schedule) Remove(t time.Time, x interface{}) (match interface{})

Remove removes and returns the item matching x at time t from the stack, or nil if no match for x was found in the stack at time t.

type Stack

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

Stack is a stack of user items, ordered by time.

func NewStack

func NewStack(less LessFunc) *Stack

NewStack returns a new stack.

func (*Stack) Insert

func (s *Stack) Insert(t time.Time, x interface{}) (match interface{})

Insert inserts the item x in the stack at time t. If a matching item already exists at that time position, it will be replaced and the matching item returned.

func (*Stack) Len

func (s *Stack) Len() (n int)

Len returns the number of items in the stack.

func (*Stack) MinTime

func (s *Stack) MinTime() (t time.Time)

MinTime returns the earliest time for an item in the stack. If the stack is empty, this returns the zero time.

func (*Stack) Pop

func (s *Stack) Pop() (x interface{})

Pop pops the earliest item off the stack. If the stack is empty, this returns nil.

func (*Stack) Prune

func (s *Stack) Prune(t time.Time) (S []interface{})

Prune removes and returns a slice of all items in the stack with time before or equal to t.

func (*Stack) Remove

func (s *Stack) Remove(t time.Time, x interface{}) (match interface{})

Remove removes and returns the item matching x at time t from the stack, or nil if no match for x was found in the stack at time t.

Jump to

Keyboard shortcuts

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