Queue with delayed items
go get github.com/lcd1232/dqueue
package main
import (
"context"
"fmt"
"time"
"github.com/lcd1232/dqueue"
)
func main() {
q := dqueue.NewQueue()
defer q.Stop(context.Background())
// Insert "some value" with delay 1 hour
q.Insert("some value", time.Hour)
q.Insert(2, time.Minute)
v, ok := q.Pop()
fmt.Println(v, ok) // Prints <nil> false
// Due to golang timer implementation it can take a little more time
time.Sleep(time.Minute+time.Second)
v, ok = q.Pop()
fmt.Println(v, ok) // Prints 2 true
// Use context cancellation
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
v, ok = q.PopCtx(ctx)
fmt.Println(v, ok) // Prints <nil> false
// Get value from channel
select {
case v = <- q.Channel():
fmt.Println(v, ok) // Prints some value false
}
}
dqueue uses semantic versioning. API should not change between patch and minor releases. New minor versions may add additional features to the API.
"The code in this project is licensed under MIT license."