goqueue

package module
v0.0.0-...-c5a229e Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2021 License: MIT Imports: 6 Imported by: 1

README

delayqueue

A Multi-safe Delay queue by Golang.

How To Use
type Task struct {
	ID int
}

// create a delay queue with cap 3
q := NewDelayQueue(3)
// add a Task to queue which should run after 10 seconds later
q.Offer(Task{ID: 1}, time.Now().Add(10*time.Second))

// run a goroutine to deal timer check
go q.Poll()

// get ready element from delay queue
ele := <-q.C
fmt.Println(ele) // Task{ID: 1}

// stop go q.Poll(), you can run queue by call q.Poll() then.
q.Stop()

Documentation

Overview

Package goqueue 延迟队列

Package goqueue 优先级队列(小顶堆)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DelayQueue

type DelayQueue struct {
	C chan interface{} // you will get the ready element from this chan
	// contains filtered or unexported fields
}

DelayQueue make your element delay to be done with a Millisecond precision

func NewDelayQueue

func NewDelayQueue(size int) *DelayQueue

NewDelayQueue New DelayQueue with size

func (*DelayQueue) Offer

func (dq *DelayQueue) Offer(value interface{}, runTime time.Time)

Offer add a element to queue, runTime is the future time we want to get the element

func (*DelayQueue) Poll

func (dq *DelayQueue) Poll()

Poll send the ready element to a chan, you should call this func with a new goroutine to make process unblock.

func (*DelayQueue) Stop

func (dq *DelayQueue) Stop()

Stop stop to check delayQueue, break loop in Poll func

type Element

type Element struct {
	Value    interface{}
	Priority int64
	Index    int
}

Element

type PriorityQueue

type PriorityQueue []*Element

PriorityQueue no concurrency safty, need to Lock when use with multi-goroutines

func NewPriorityQueue

func NewPriorityQueue(size int) PriorityQueue

NewPriorityQueue NewPriorityQueue

func (PriorityQueue) Len

func (pq PriorityQueue) Len() int

func (PriorityQueue) Less

func (pq PriorityQueue) Less(i, j int) bool

func (*PriorityQueue) Offer

func (pq *PriorityQueue) Offer(val interface{}, priority int64) *Element

Offer add a val with priority

func (PriorityQueue) Peek

func (pq PriorityQueue) Peek() *Element

Peek return a *Element which has the least Priority in queue

func (*PriorityQueue) PeekAndRemove

func (pq *PriorityQueue) PeekAndRemove() *Element

PeekAndShift return the first *Element and delete it from queue

func (*PriorityQueue) PeekAndShift

func (pq *PriorityQueue) PeekAndShift(priority int64) (*Element, int64)

PeekAndShift return a *Element which has the least Priority in queue and ele.Priority not more than param priority. Otherwise, return nil and priority-ele.Priority

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() interface{}

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(ele interface{})

func (PriorityQueue) Swap

func (pq PriorityQueue) Swap(i, j int)

Jump to

Keyboard shortcuts

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