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 ¶
- Variables
- type Scheduler
- func (s *Scheduler) CallNext() (interface{}, error)
- func (s *Scheduler) Empty() bool
- func (s *Scheduler) Now() time.Time
- func (s *Scheduler) PeekNext() (time.Time, error)
- func (s *Scheduler) RemoveAll()
- func (s *Scheduler) RemoveAllUpdate()
- func (s *Scheduler) RemoveNext()
- func (s *Scheduler) RemoveNextUpdate()
- func (s *Scheduler) Schedule(f func(time.Time) interface{}, t time.Time) error
- func (s *Scheduler) ScheduleOffset(f func(time.Time) interface{}, offset time.Duration) error
Constants ¶
This section is empty.
Variables ¶
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 ¶
Returns a new Scheduler whose internal clock is set to t.
func (*Scheduler) CallNext ¶
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) PeekNext ¶
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.