Git Product home page Git Product logo

timerqueue's Introduction

GoDoc

timerqueue

The timerqueue package implements a priority queue for objects scheduled to perform actions at clock times.

Example: Scheduling timers

The following code declares an object implementing the Timer interface, creates a timerqueue, and adds three events to the timerqueue.

type event int

func (e event) OnTimer(t time.Time) {
    fmt.Printf("event.OnTimer %d fired at %v\n", int(e), t)
}

queue := timerqueue.New()
queue.Schedule(event(1), time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC))
queue.Schedule(event(2), time.Date(2015, 1, 3, 0, 0, 0, 0, time.UTC))
queue.Schedule(event(3), time.Date(2015, 1, 2, 0, 0, 0, 0, time.UTC))

Example: Peeking at the next timer to be scheduled

Using the queue initialized in the first example, the following code examines the head of the timerqueue and outputs the id and time of the event found there.

e, t := queue.PeekFirst()
if e != nil {
    fmt.Printf("Event %d will be first to fire at %v.\n", int(e.(event)), t)
    fmt.Printf("%d events remain in the timerqueue.", queue.Len())
}

Output:

Event 1 will be first to fire at 2015-01-01 00:00:00 +0000 UTC.
3 events remain in the timerqueue.

Example: Popping the next timer to be scheduled

Using the queue initialized in the first example, this code removes the next timer to be executed until the queue is empty.

for queue.Len() > 0 {
    e, t := queue.PopFirst()
    fmt.Printf("Event %d fires at %v.\n", int(e.(event)), t)
}

Output:

Event 1 fires at 2015-01-01 00:00:00 +0000 UTC.
Event 3 fires at 2015-01-02 00:00:00 +0000 UTC.
Event 2 fires at 2015-01-03 00:00:00 +0000 UTC.

Example: Issuing OnTimer callbacks with Advance

The final example shows how to dispatch OnTimer callbacks to timers using the timerqueue's Advance method.

Advance calls the OnTimer method for each timer scheduled before the requested time. Timers are removed from the timerqueue in order of their scheduling.

// Call the OnTimer method for each event scheduled before
// January 10, 2015. Pop the called timer from the queue.
queue.Advance(time.Date(2015, 1, 10, 0, 0, 0, 0, time.UTC))

Output:

event.OnTimer 1 fired at 2015-01-01 00:00:00 +0000 UTC.
event.OnTimer 3 fired at 2015-01-02 00:00:00 +0000 UTC.
event.OnTimer 2 fired at 2015-01-03 00:00:00 +0000 UTC.

timerqueue's People

Contributors

beevik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

timerqueue's Issues

need a license

`I would like to use this code but I am unwilling to without a licence.
Any chance you could add a license file to this repository so the terms of its release are clear.

Go module support

Could you add Go module support?
Even if you're no longer working on timerqueue, this will make it easier and safer to use in other projects.

Also, the project is missing a license. Under what terms can it be used?
This will currently block its code and docs from being displayed on https://pkg.go.dev/github.com/beevik/timerqueue .

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.