Documentation
¶
Overview ¶
Package timerqueue implements a priority queue for objects scheduled at a particular time.
Index ¶
- type Queue
- func (q *Queue) Advance(tm time.Time)
- func (q *Queue) Clear()
- func (q *Queue) GetTime(t Timer) (tm time.Time, err error)
- func (q *Queue) IsScheduled(t Timer) bool
- func (q *Queue) Len() int
- func (q *Queue) PeekFirst() (t Timer, tm time.Time)
- func (q *Queue) PopFirst() (t Timer, tm time.Time)
- func (q *Queue) Schedule(t Timer, tm time.Time)
- func (q *Queue) Unschedule(t Timer)
- type Timer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Queue is a time-sorted collection of Timer objects.
Example ¶
Schedule several events with a timerqueue, and dispatch them by calling Advance.
package main
import (
"fmt"
"time"
"github.com/beevik/timerqueue"
)
type event int
func (e event) OnTimer(t time.Time) {
fmt.Printf(" Event %d executed at %v\n", int(e), t)
}
// Schedule several events with a timerqueue, and dispatch
// them by calling Advance.
func main() {
queue := timerqueue.New()
// Schedule an event each day from Jan 1 to Jan 7, 2015.
tm := time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)
for i := 1; i <= 7; i++ {
queue.Schedule(event(i), tm)
tm = tm.Add(24 * time.Hour)
}
fmt.Println("Advancing to Jan 4...")
queue.Advance(time.Date(2015, 1, 4, 0, 0, 0, 0, time.UTC))
fmt.Println("Advancing to Jan 10...")
queue.Advance(time.Date(2015, 1, 10, 0, 0, 0, 0, time.UTC))
}
Output: Advancing to Jan 4... Event 1 executed at 2015-01-01 00:00:00 +0000 UTC Event 2 executed at 2015-01-02 00:00:00 +0000 UTC Event 3 executed at 2015-01-03 00:00:00 +0000 UTC Event 4 executed at 2015-01-04 00:00:00 +0000 UTC Advancing to Jan 10... Event 5 executed at 2015-01-05 00:00:00 +0000 UTC Event 6 executed at 2015-01-06 00:00:00 +0000 UTC Event 7 executed at 2015-01-07 00:00:00 +0000 UTC
func (*Queue) Advance ¶
Advance executes OnTimer callbacks for all timers scheduled to be run before the time 'tm'. Executed timers are removed from the timer queue.
func (*Queue) GetTime ¶
GetTime returns the time at which the timer is scheduled. If the timer isn't currently scheduled, an error is returned.
func (*Queue) IsScheduled ¶
IsScheduled returns true if the timer is currently scheduled.
func (*Queue) PeekFirst ¶
PeekFirst returns the next timer to be scheduled and the time at which it is scheduled to run. It does not modify the contents of the timer queue.
func (*Queue) PopFirst ¶
PopFirst removes and returns the next timer to be scheduled and the time at which it is scheduled to run.
func (*Queue) Schedule ¶
Schedule schedules a timer for exectuion at time tm. If the timer was already scheduled, it is rescheduled.
func (*Queue) Unschedule ¶
Unschedule unschedules a timer's execution.