dqueue

package module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2020 License: MIT Imports: 4 Imported by: 1

README

Go Report Card Build Status Coverage Godoc Releases LICENSE

dqueue

Queue with delayed items

Installation

go get github.com/lcd1232/dqueue

Usage

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
    }
}

Project versioning

dqueue uses semantic versioning. API should not change between patch and minor releases. New minor versions may add additional features to the API.

Licensing

"The code in this project is licensed under MIT license."

Documentation

Index

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
}

func NewQueue

func NewQueue() *Queue

func (*Queue) Channel

func (q *Queue) Channel() <-chan interface{}

Channel returns channel which can be used to retrieve value from queue.

func (*Queue) Insert

func (q *Queue) Insert(value interface{}, delay time.Duration)

func (*Queue) Length added in v1.2.0

func (q *Queue) Length() int

Length returns total items in queue.

func (*Queue) Pop

func (q *Queue) Pop() (value interface{}, success bool)

func (*Queue) PopCtx

func (q *Queue) PopCtx(ctx context.Context) (value interface{}, success bool)

func (*Queue) Stop

func (q *Queue) Stop(ctx context.Context) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL