uqueue

package
v3.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Queues Library

This library provides generic queue data structures for Go. It includes FIFO, lock-free concurrent FIFO, and priority queue implementations.

Features

  • Generic Implementation: FIFO and priority queues are implemented using Go generics, allowing you to queue items of any type.
  • Thread-Safety: Queue operations are safe across multiple goroutines. ConcurrentFIFOQueueImpl provides a lock-free FIFO implementation for high-concurrency FIFO workloads.
  • Timeouts for Polling: The Poll function allows consumers to wait for items with a timeout.

Structures

FIFOQueueImpl

A standard FIFO (First-In-First-Out) queue implementation.

Methods
  • NewFIFOQueue: Initializes a new FIFO queue with optional initial elements.
  • Queue: Enqueues an item to the back of the queue.
  • Fetch: Dequeues an item from the front of the queue without waiting.
  • Poll: Dequeues an item from the front of the queue or waits for a specified timeout.
  • PollContext: Dequeues an item from the front of the queue or waits until context cancellation.
  • Peek: Reads the next item without removing it.
  • Drain: Dequeues all items or up to a limit.
  • Clear: Removes all queued items.
  • Empty: Returns true when the queue has no items.
ConcurrentFIFOQueueImpl

A lock-free FIFO queue implementation based on the Michael-Scott queue algorithm.

Methods
  • NewConcurrentFIFOQueueImpl: Initializes a new concurrent FIFO queue.
  • Queue: Enqueues an item to the back of the queue.
  • Fetch: Dequeues an item from the front of the queue without waiting.
  • Poll: Dequeues an item from the front of the queue or waits for a specified timeout.
  • PollContext: Dequeues an item from the front of the queue or waits until context cancellation.
  • Peek: Reads the next item without removing it.
  • Drain: Dequeues all items or up to a limit.
  • Clear: Removes all queued items.
  • Empty: Returns true when the queue has no items.
PriorityQueueImpl

A priority queue where items can be enqueued with a priority level, and dequeuing will retrieve the highest priority item.

Methods
  • NewPrioritizedPriorityQueue: Initializes a new empty priority queue.
  • Queue: Enqueues an item with a specified priority.
  • Fetch: Dequeues the highest-priority item from the queue without waiting.
  • Poll: Dequeues the highest-priority item from the queue or waits for a specified timeout.
  • PollContext: Dequeues the highest-priority item or waits until context cancellation.
  • Peek: Reads the highest-priority item without removing it.
  • Drain: Dequeues all items or up to a limit in priority order.
  • Clear: Removes all queued items.
  • Empty: Returns true when the queue has no items.

Dependencies

The library uses the container/heap package for the priority queue implementation and an opt package for optional value handling.

Usage

FIFO queue:
package myprogram

import (
	"time"

	"github.com/kordax/basic-utils/v3/uqueue"
)

q := uqueue.NewFIFOQueue[int](1, 2, 3)
q.Queue(4)
item := q.Poll(5 * time.Second)

Priority queue:
package myprogram

import (
	"time"

	"github.com/kordax/basic-utils/v3/uqueue"
)

pq := uqueue.NewPriorityQueue[int]()
pq.Queue(1, 3) // The number 3 here is the priority.
pq.Queue(2, 1)
item := pq.Poll(5 * time.Second)

Author

Developed by @kordax (Dmitry Morozov)

Valoru Software

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConcurrentFIFOQueueImpl

type ConcurrentFIFOQueueImpl[T any] struct {
	// contains filtered or unexported fields
}

ConcurrentFIFOQueueImpl implementation that queues/dequeues items according to https://www.cs.rochester.edu/~scott/papers/1996_PODC_queues.pdf

func NewConcurrentFIFOQueueImpl

func NewConcurrentFIFOQueueImpl[T any]() *ConcurrentFIFOQueueImpl[T]

func (*ConcurrentFIFOQueueImpl[T]) Clear

func (q *ConcurrentFIFOQueueImpl[T]) Clear()

func (*ConcurrentFIFOQueueImpl[T]) Drain

func (q *ConcurrentFIFOQueueImpl[T]) Drain(limit ...int) []T

func (*ConcurrentFIFOQueueImpl[T]) Empty

func (q *ConcurrentFIFOQueueImpl[T]) Empty() bool

func (*ConcurrentFIFOQueueImpl[T]) Fetch

func (q *ConcurrentFIFOQueueImpl[T]) Fetch() uopt.Opt[T]

Fetch fetches item in the finite time.

func (*ConcurrentFIFOQueueImpl[T]) Len

func (q *ConcurrentFIFOQueueImpl[T]) Len() uint64

func (*ConcurrentFIFOQueueImpl[T]) Peek

func (q *ConcurrentFIFOQueueImpl[T]) Peek() uopt.Opt[T]

func (*ConcurrentFIFOQueueImpl[T]) Poll

func (q *ConcurrentFIFOQueueImpl[T]) Poll(timeout time.Duration) uopt.Opt[T]

Poll items fetches item in the finite time.

func (*ConcurrentFIFOQueueImpl[T]) PollContext

func (q *ConcurrentFIFOQueueImpl[T]) PollContext(ctx context.Context) uopt.Opt[T]

func (*ConcurrentFIFOQueueImpl[T]) Queue

func (q *ConcurrentFIFOQueueImpl[T]) Queue(t T)

Queue queues an item in the finite time. This operation is thread-safe yet is not "synchronized" by its nature. Implementation uses Michael-Scott CAS implementation.

type FIFOQueueImpl

type FIFOQueueImpl[T any] struct {
	// contains filtered or unexported fields
}

FIFOQueueImpl represents a generic implementation of a First-In-First-Out (FIFO) data structure. This struct uses a slice to maintain elements in the order they are added, ensuring that the oldest item (the first one added) is the first to be fetched.

Fields: - queue: Slice holding the actual elements. It grows dynamically as new elements are added.

  • ch: A communication channel utilized in the Poll() method. The channel is used to notify waiting pollers when queue state changes.

func NewFIFOQueue

func NewFIFOQueue[T any](elements ...T) *FIFOQueueImpl[T]

func (*FIFOQueueImpl[T]) Clear

func (q *FIFOQueueImpl[T]) Clear()

func (*FIFOQueueImpl[T]) Drain

func (q *FIFOQueueImpl[T]) Drain(limit ...int) []T

func (*FIFOQueueImpl[T]) Empty

func (q *FIFOQueueImpl[T]) Empty() bool

func (*FIFOQueueImpl[T]) Fetch

func (q *FIFOQueueImpl[T]) Fetch() uopt.Opt[T]

func (*FIFOQueueImpl[T]) Len

func (q *FIFOQueueImpl[T]) Len() uint64

func (*FIFOQueueImpl[T]) Peek

func (q *FIFOQueueImpl[T]) Peek() uopt.Opt[T]

func (*FIFOQueueImpl[T]) Poll

func (q *FIFOQueueImpl[T]) Poll(timeout time.Duration) uopt.Opt[T]

func (*FIFOQueueImpl[T]) PollContext

func (q *FIFOQueueImpl[T]) PollContext(ctx context.Context) uopt.Opt[T]

func (*FIFOQueueImpl[T]) Queue

func (q *FIFOQueueImpl[T]) Queue(t T)

Queue queues an item.

type PriorityQueue

type PriorityQueue[T any] interface {
	Queue(t T, priority int)
	Fetch() uopt.Opt[T]
	Poll(timeout time.Duration) uopt.Opt[T]
	PollContext(ctx context.Context) uopt.Opt[T]
	Peek() uopt.Opt[T]
	Drain(limit ...int) []T
	Clear()
	Empty() bool
	Len() uint64
}

type PriorityQueueImpl

type PriorityQueueImpl[T any] struct {
	// contains filtered or unexported fields
}

PriorityQueueImpl represents a generic implementation of a priority queue data structure. Items are organized based on their priorities, with higher-priority items being fetched before lower-priority ones. This struct uses a heap data structure (as implemented in the "container/heap" package) to efficiently manage the priorities and retrieval of items.

Fields: - queue: The underlying heap structure (prioritizedQueue) that manages the prioritized items.

  • ch: A communication channel utilized in the Poll() method. The channel is used to notify waiting pollers when queue state changes.

func NewPriorityQueue

func NewPriorityQueue[T any]() *PriorityQueueImpl[T]

func (*PriorityQueueImpl[T]) Clear

func (q *PriorityQueueImpl[T]) Clear()

func (*PriorityQueueImpl[T]) Drain

func (q *PriorityQueueImpl[T]) Drain(limit ...int) []T

func (*PriorityQueueImpl[T]) Empty

func (q *PriorityQueueImpl[T]) Empty() bool

func (*PriorityQueueImpl[T]) Fetch

func (q *PriorityQueueImpl[T]) Fetch() uopt.Opt[T]

func (*PriorityQueueImpl[T]) Len

func (q *PriorityQueueImpl[T]) Len() uint64

func (*PriorityQueueImpl[T]) Peek

func (q *PriorityQueueImpl[T]) Peek() uopt.Opt[T]

func (*PriorityQueueImpl[T]) Poll

func (q *PriorityQueueImpl[T]) Poll(timeout time.Duration) uopt.Opt[T]

func (*PriorityQueueImpl[T]) PollContext

func (q *PriorityQueueImpl[T]) PollContext(ctx context.Context) uopt.Opt[T]

func (*PriorityQueueImpl[T]) Queue

func (q *PriorityQueueImpl[T]) Queue(t T, priority int)

type Queue

type Queue[T any] interface {
	Queue(t T)
	Fetch() uopt.Opt[T]
	Poll(timeout time.Duration) uopt.Opt[T]
	PollContext(ctx context.Context) uopt.Opt[T]
	Peek() uopt.Opt[T]
	Drain(limit ...int) []T
	Clear()
	Empty() bool
	Len() uint64
}

Jump to

Keyboard shortcuts

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