Documentation ¶
Overview ¶
Package sched is used to find the next time event from multiple rules.
Example (SchedScheduler) ¶
package main import ( "fmt" "github.com/mb0/sched" "sync/atomic" "time" ) func main() { type action struct { sched.Interval Do func(time.Time) } var ( now = time.Now() done = make(chan struct{}) count int32 ) a := &action{ sched.Interval{Start: now, Duration: 10 * time.Millisecond}, func(t time.Time) { fmt.Println("action triggered after", t.Sub(now)) }, } trigger := func(ev sched.Event) { switch a := ev.Rule.(type) { case *action: a.Do(ev.Time) default: fmt.Println("unknown rule", a) } if atomic.AddInt32(&count, 1) >= 3 { close(done) } } s := sched.New(now, trigger) // equivalent to s.Sched.Add(a); s.Start() s.Add(a) <-done s.Stop() }
Output: action triggered after 10ms action triggered after 20ms action triggered after 30ms
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Rule ¶
type Rule interface { // Next returns the next point in time after now. // If the returned time is not after now this rule has ended. Next(now time.Time) time.Time }
Rule represents a deterministic sequence of points in time. Implementations must be comparable.
type Schedule ¶
type Schedule struct { Last Event // contains filtered or unexported fields }
Schedule is a time based queue of events sourced from multiple rules.
func NewSchedule ¶
NewSchedule returns an empty Schedule beginning at now.
func (*Schedule) Next ¶
Next returns the next event. The event rule is nil if no event is scheduled. The returned event rule is rescheduled or removed from the queue.
type Scheduler ¶
Scheduler schedules rules and calls its handler with events.
func (*Scheduler) Add ¶
Add adds rule r to the schedule. Adding a rule will automatically start the scheduler.
Click to show internal directories.
Click to hide internal directories.