fsched

package module
v0.0.0-...-eb9021d Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2014 License: BSD-3-Clause Imports: 3 Imported by: 0

README

fsched Build Status

A time-based scheduler for function callbacks

See the documentation.

Documentation

Overview

Package fsched implements a sequential scheduler for function callbacks. The scheduler keeps track of the current time (different from real time). Each call to CallNext fast-forwards to the timestamp on the earliest event, and executes the associated callback function. Note that the scheduler's time is arbitrary and must only be internally consistent; it is unrelated to any real sense of time (ie, clock cycles, seconds since epoch, etc).

While the scheduler interface is generic, it is designed to be used for simulating highly-parallel processes where accurate time is important, but the simulation is too processor-intensive to be run in real time.

Note that the scheduler is NOT thread-safe. The intended usage of the scheduler is to call CallNext sequentially. In particular, this allows scheduled callbacks to safely interact with the scheduler, for example to schedule more events.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPast  = errors.New("Event scheduled in the past")
	ErrEmpty = errors.New("Empty")
)

Functions

This section is empty.

Types

type Scheduler

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

Scheduler allows for time- and offset-based scheduling of function callbacks.

Note that Scheduler is NOT thread-safe. The intended usage of Scheduler is to call CallNext sequentially. In particular, this allows scheduled callbacks to safely interact with the Scheduler, for example to schedule more events.

func NewScheduler

func NewScheduler() *Scheduler

Returns a new Scheduler whose internal clock is set to the zero value of time.Time.

func NewSchedulerTime

func NewSchedulerTime(t time.Time) *Scheduler

Returns a new Scheduler whose internal clock is set to t.

func (*Scheduler) CallNext

func (s *Scheduler) CallNext() (interface{}, error)

Fast-forward the internal clock to match the next scheduled event, and call the associated callback, passing the (now updated) time as the single argument. Return the value returned from this call.

If there are no events scheduled, return a nil interface value and ErrEmpty.

Note that CallNext does not modify s after calling the callback. Thus, it is safe to call methods on s from within the callback.

func (*Scheduler) Empty

func (s *Scheduler) Empty() bool

Returns whether there are 0 events scheduled.

func (*Scheduler) Now

func (s *Scheduler) Now() time.Time

Returns the current value of the internal clock.

func (*Scheduler) PeekNext

func (s *Scheduler) PeekNext() (time.Time, error)

Returns the timestamp on the next scheduled event, or the zero value and ErrEmpty if no events are scheduled.

func (*Scheduler) RemoveAll

func (s *Scheduler) RemoveAll()

Remove all scheduled events from the Scheduler, but do not alter the internal clock.

func (*Scheduler) RemoveAllUpdate

func (s *Scheduler) RemoveAllUpdate()

Remove all scheduled events from the Scheduler, fast-forwarding the internal clock to match the latest scheduled event. If there are no events scheduled, do not alter the clock.

func (*Scheduler) RemoveNext

func (s *Scheduler) RemoveNext()

Remove the next scheduled event from the Scheduler, but do not alter the internal clock.

func (*Scheduler) RemoveNextUpdate

func (s *Scheduler) RemoveNextUpdate()

Fast-forward the internal clock to match the next scheduled event, and remove the event from the Scheduler. If there are no events scheduled, do not alter the clock.

func (*Scheduler) Schedule

func (s *Scheduler) Schedule(f func(time.Time) interface{}, t time.Time) error

Schedule f to be called when the internal clock reaches t.

Returns ErrPast if t is before s.Now().

func (*Scheduler) ScheduleOffset

func (s *Scheduler) ScheduleOffset(f func(time.Time) interface{}, offset time.Duration) error

Schedule f to be called when offset has elapsed.

Returns ErrPast if offset is negative.

Jump to

Keyboard shortcuts

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