Documentation
¶
Index ¶
- Constants
- Variables
- type Element
- type MultiLevelQueue
- func (mq *MultiLevelQueue[T]) AddLevel(level any) error
- func (mq *MultiLevelQueue[T]) Dequeue(level any) (T, error)
- func (mq *MultiLevelQueue[T]) DequeueIf(level any, cond func(T) (bool, error)) (T, error)
- func (mq *MultiLevelQueue[T]) Enqueue(level any, obj ...T) error
- func (mq *MultiLevelQueue[T]) HasLevel(level any) bool
- func (mq *MultiLevelQueue[T]) Length(level any) int
- func (mq *MultiLevelQueue[T]) TotalLength() int
- func (mq *MultiLevelQueue[T]) WaitDequeue(ctx context.Context, level any) (T, error)
- func (mq *MultiLevelQueue[T]) WaitDequeueAll(ctx context.Context) (T, error)
- func (mq *MultiLevelQueue[T]) WaitDequeueIf(ctx context.Context, level any, cond func(T) (bool, error), ...) (T, error)
- type PriorityQueue
- func (pq *PriorityQueue[T]) Dequeue() (Element[T], error)
- func (pq *PriorityQueue[T]) Enqueue(priority any, values ...T) error
- func (pq *PriorityQueue[T]) ForceAging() error
- func (pq *PriorityQueue[T]) HasLevel(level int) bool
- func (pq *PriorityQueue[T]) HasPriority(priority any) bool
- func (pq *PriorityQueue[T]) JustDequeue() *Element[T]
- func (pq *PriorityQueue[T]) JustWaitDequeue(ctx context.Context) *Element[T]
- func (pq *PriorityQueue[T]) Length(priority any) int
- func (pq *PriorityQueue[T]) SetAgingInterval(interval time.Duration)
- func (pq *PriorityQueue[T]) SetAgingTimeSlice(priority any, timeslice time.Duration) error
- func (pq *PriorityQueue[T]) SetDefaultAgingTimeSlice(timeslice time.Duration) error
- func (pq *PriorityQueue[T]) SetPriority(priority any, level int) error
- func (pq *PriorityQueue[T]) TotalLength() int
- func (pq *PriorityQueue[T]) WaitDequeue(ctx context.Context) (Element[T], error)
- type Queue
- func (q *Queue[T]) Back() (T, error)
- func (q *Queue[T]) Clear()
- func (q *Queue[T]) Dequeue() (T, error)
- func (q *Queue[T]) DequeueIf(cond func(t T) (bool, error)) (T, error)
- func (q *Queue[T]) Enqueue(obj ...T) error
- func (q *Queue[T]) Front() (T, error)
- func (q *Queue[T]) Length() int
- func (q *Queue[T]) WaitDequeue(ctx context.Context) (T, error)
- func (q *Queue[T]) WaitDequeueIf(ctx context.Context, cond func(t T) (bool, error), ...) (T, error)
- type Queuer
- type WaitResult
Constants ¶
const NoAging = time.Duration(0)
Variables ¶
var ErrEmpty = errors.New("queue is empty")
var ErrExistedLevel = errors.New("level has already existed")
var ErrNotExistedLevel = errors.New("not found level")
var ErrNotMeetCondition = errors.New("not meet condition")
var ErrTimeout = errors.New("timeout")
Functions ¶
This section is empty.
Types ¶
type Element ¶
type Element[T any] struct { // contains filtered or unexported fields }
Element is a wrapper of an element in priority queue. It contains some metadata.
func (Element[T]) OriginalLevel ¶
OriginalLevel returns the original priority level value.
func (Element[T]) OriginalPriority ¶
OriginalPriority returns the original priority.
type MultiLevelQueue ¶
type MultiLevelQueue[T any] struct { // contains filtered or unexported fields }
MultiLevelQueue is a queue supporting multiple inner queues.
func DefaultMultiLevelQueue ¶
func DefaultMultiLevelQueue[T any]() *MultiLevelQueue[T]
DefaultMultiLevelQueue returns an empty MultiLevelQueue with the default inner Queue.
func NewMultiLevelQueue ¶
func NewMultiLevelQueue[T any](queuer func() Queuer[T]) *MultiLevelQueue[T]
NewMultiLevelQueue returns an empty MultiLevelQueue with a customized Queuer.
func (*MultiLevelQueue[T]) AddLevel ¶
func (mq *MultiLevelQueue[T]) AddLevel(level any) error
AddLevel adds a new queue in MultiLevelQueue.
func (*MultiLevelQueue[T]) Dequeue ¶
func (mq *MultiLevelQueue[T]) Dequeue(level any) (T, error)
Dequeue returns the first element of a specific queue.
func (*MultiLevelQueue[T]) DequeueIf ¶
func (mq *MultiLevelQueue[T]) DequeueIf(level any, cond func(T) (bool, error)) (T, error)
DequeueIf returns the first element of a specific level queue if it sastifies the condition.
func (*MultiLevelQueue[T]) Enqueue ¶
func (mq *MultiLevelQueue[T]) Enqueue(level any, obj ...T) error
Enqueue pushes some values into queue with a given level.
func (*MultiLevelQueue[T]) HasLevel ¶
func (mq *MultiLevelQueue[T]) HasLevel(level any) bool
HasLevel returns true if the level has existed in MultiLevelQueue.
func (*MultiLevelQueue[T]) Length ¶
func (mq *MultiLevelQueue[T]) Length(level any) int
Length returns the number of elements with a given level.
func (*MultiLevelQueue[T]) TotalLength ¶
func (mq *MultiLevelQueue[T]) TotalLength() int
TotalLength returns the number of elements in MultiLevelQueue.
func (*MultiLevelQueue[T]) WaitDequeue ¶
func (mq *MultiLevelQueue[T]) WaitDequeue(ctx context.Context, level any) (T, error)
WaitDequeueIf returns the first element of a specific queue. It blocks the current process if the queue is empty.
func (*MultiLevelQueue[T]) WaitDequeueAll ¶
func (mq *MultiLevelQueue[T]) WaitDequeueAll(ctx context.Context) (T, error)
func (*MultiLevelQueue[T]) WaitDequeueIf ¶
func (mq *MultiLevelQueue[T]) WaitDequeueIf( ctx context.Context, level any, cond func(T) (bool, error), retrigger func(T) <-chan time.Time, ) (T, error)
WaitDequeueIf returns the first element of a specific queue if it sastifies the condition. It blocks the current process if the queue is empty.
type PriorityQueue ¶
type PriorityQueue[T any] struct { // contains filtered or unexported fields }
PriorityQueue implements a fast, thread-safe priority queue.
func Default ¶
func Default[T any]() *PriorityQueue[T]
Default returns an empty PriorityQueue with the default MultiLevelQueue.
func New ¶
func New[T any](core *MultiLevelQueue[Element[T]]) *PriorityQueue[T]
New returns an empty PriorityQueue with the customized metadata.
func (*PriorityQueue[T]) Dequeue ¶
func (pq *PriorityQueue[T]) Dequeue() (Element[T], error)
Dequeue pops the first element which has the highest priority in PriorityQueue.
func (*PriorityQueue[T]) Enqueue ¶
func (pq *PriorityQueue[T]) Enqueue(priority any, values ...T) error
Enqueue pushes some values into PriorityQueue with a specific Priority.
func (*PriorityQueue[T]) ForceAging ¶
func (pq *PriorityQueue[T]) ForceAging() error
ForceAging do checking aging immediately no matter of the aging-interval.
func (*PriorityQueue[T]) HasLevel ¶
func (pq *PriorityQueue[T]) HasLevel(level int) bool
HasLevel returns true if the level value has existed in PriorityQueue.
func (*PriorityQueue[T]) HasPriority ¶
func (pq *PriorityQueue[T]) HasPriority(priority any) bool
HasPriority returns true if the Priority has existed in PriorityQueue.
func (*PriorityQueue[T]) JustDequeue ¶
func (pq *PriorityQueue[T]) JustDequeue() *Element[T]
JustDequeue is a wrapper method of Dequeue, it returns nil when the queue is empty, and panics if got other errors.
func (*PriorityQueue[T]) JustWaitDequeue ¶
func (pq *PriorityQueue[T]) JustWaitDequeue(ctx context.Context) *Element[T]
JustWaitDequeue is a wrapper function of WaitDequeue, it returns nil if the timeout is reached, and panic if got other errors.
func (*PriorityQueue[T]) Length ¶
func (pq *PriorityQueue[T]) Length(priority any) int
Length returns the number of elements with a given priority.
func (*PriorityQueue[T]) SetAgingInterval ¶
func (pq *PriorityQueue[T]) SetAgingInterval(interval time.Duration)
SetAgingInterval sets the interval. The PriorityQueue will check the aging every the interval passes. If the interval is unset, it will be chosen automatically (equal to the least aging timeslice of all levels)
func (*PriorityQueue[T]) SetAgingTimeSlice ¶
func (pq *PriorityQueue[T]) SetAgingTimeSlice(priority any, timeslice time.Duration) error
SetAgingTimeSlice sets the aging time slice for a specific level. If an element existed for more than this time slice, it will be moved to the next higher level.
func (*PriorityQueue[T]) SetDefaultAgingTimeSlice ¶
func (pq *PriorityQueue[T]) SetDefaultAgingTimeSlice(timeslice time.Duration) error
SetDefaultAgingTimeSlice sets the default aging time slice for Priority levels which have not set the aging yet. If an element existed for more than this time slice, it will be moved to the next higher level.
func (*PriorityQueue[T]) SetPriority ¶
func (pq *PriorityQueue[T]) SetPriority(priority any, level int) error
SetPriority assigns a new Priority with a level value.
func (*PriorityQueue[T]) TotalLength ¶
func (pq *PriorityQueue[T]) TotalLength() int
TotalLength returns the total number of elements in PriorityQueue.
func (*PriorityQueue[T]) WaitDequeue ¶
func (pq *PriorityQueue[T]) WaitDequeue(ctx context.Context) (Element[T], error)
WaitDequeue returns first element which has the highest priority. Differ from Dequeue, this method will blocks the current process if the queue is empty.
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
Queue is a default implementation of Queuer. It is a wrapper of slice. It uses mutex to keep the queue thread-safe.
func (*Queue[T]) DequeueIf ¶
DequeueIf pops the first element in queue if it satifies the condition.
func (*Queue[T]) WaitDequeue ¶
WaitDequeue is the blocking-version of Dequeue.
type Queuer ¶
type Queuer[T any] interface { // Enqueue push an element into the queue. Enqueue(obj ...T) error // DequeueIf returns and removes the element at the begining of the queue if // the element meets the condition. This function is non-blocking. DequeueIf(func(t T) (bool, error)) (T, error) // Dequeue returns and removes the element at the begining of the queue. // This function is non-blocking. Dequeue() (T, error) // WaitDequeueIf is the same as DequeueIf, but this function is blocking. // // If the condition was not sastified, this function will check again when // the returned channel of retrigger function sends a signal. // // If you want returns immediately when the condition was not sastified, // pass a nil value to the retrigger parameter. // // For example: // // No retrigger. // queue.WaitDequeueIf(ctx, nil, nil) // // // Retrigger after some time. // queue.WaitDequeueIf( // ctx, // func(t time.Time) (bool, error) { // return time.Now().After(t), nil // }, // func(t time.Time) <- chan time.Time { // // Approach 1: Check after every 5 minutes. // return time.After(5*time.Minutes) // // // Approach 2: Re-check at t. // return time.After(time.Since(t)) // } // ) WaitDequeueIf( ctx context.Context, cond func(t T) (bool, error), retrigger func(T) <-chan time.Time, ) (T, error) // WaitDequeue is the same as Dequeue, but this function is blocking. WaitDequeue(ctx context.Context) (T, error) // Front returns the element at the begining of the queue. Front() (T, error) // Back returns the element at the end of the queue. Back() (T, error) // Clear removes all elements in the queue. Clear() // Length returns the number of elements in the queue. Length() int }
Queuer represents for a thread-safe queue.