queue

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: MIT Imports: 5 Imported by: 2

README

queue

import "github.com/fireflycons/concurrentqueue"

Package queue implements a mutex-free channel-based queue. It is not designed for raw speed, but rather to be used as a data source for things like worker pools where reading input from a channel makes sense.

The queue is backed by a ring buffer with a default initial capacity of 256 elements that grows as needed. A constructor method is provided to set the initial capacity if desired.

Where locking is required, it is done at the ring buffer level using spinlocks to minimize contention.

Index

Variables

ErrQueueClosed is returned by Enqueue if the Close method has been called.

var ErrQueueClosed = errors.New("queue is closed")

type Queue

Queue is a concurrent queue that supports multiple producers and consumers.

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

func New
func New[T any](opts ...QueueOptFunc) *Queue[T]

New creates a new queue. You can provide optional configuration functions to customize the queue's behavior.

func (*Queue[T]) Close
func (q *Queue[T]) Close()

Close closes the queue's channels. Items can be dequeued until the queue is empty at which point reading the dequeue channel with v,ok returns false

func (*Queue[T]) Dequeue
func (q *Queue[T]) Dequeue() <-chan T

Dequeue returns the queue's read channel. Reading the channel will block if the queue is empty, or return false on v,ok if the queue has been closed.

func (*Queue[T]) Drain
func (q *Queue[T]) Drain()

Drain empties the queue of any remaining elements and releases resources.

You should call this if you do not expect your consumers to empty the queue themselves, else a goroutine and some memory will be leaked.

If called before Close, Drain will have no effect.

func (*Queue[T]) DrainTo
func (q *Queue[T]) DrainTo(other *Queue[T])

DrainTo appends all remaining elements in this queue to another queue and closes this queue.

You should call this if you do not expect your consumers to empty the queue themselves, else a goroutine and some memory will be leaked.

If called before Close, DrainTo will have no effect.

func (*Queue[T]) Enqueue
func (q *Queue[T]) Enqueue(v T) error

Enqueue adds an element to the back of the queue. If the queue is closed, an error is returned.

You must call Close() when finished appending elements to prevent the Dequeue channel from blocking when the queue empties.

func (*Queue[T]) Len
func (q *Queue[T]) Len() int

Len returns the queue length, i.e. number of items in the queue.

type QueueOptFunc

QueueOptFunc is a function that configures a Queue.

type QueueOptFunc func(*qopts)

func WithInitialCapacity
func WithInitialCapacity(capacity int) QueueOptFunc

WithInitialCapacity is a constructor function to set the initial capacity of the queue's internal ring buffer.

Generated by gomarkdoc

Documentation

Overview

Package queue implements a mutex-free channel-based queue. It is not designed for raw speed, but rather to be used as a data source for things like worker pools where reading input from a channel makes sense.

The queue is backed by a ring buffer with a default initial capacity of 256 elements that grows as needed. A constructor method is provided to set the initial capacity if desired.

Where locking is required, it is done at the ring buffer level using spinlocks to minimize contention.

Index

Constants

This section is empty.

Variables

View Source
var ErrQueueClosed = errors.New("queue is closed")

ErrQueueClosed is returned by Enqueue if the Close method has been called.

Functions

This section is empty.

Types

type Queue

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

Queue is a concurrent queue that supports multiple producers and consumers.

func New

func New[T any](opts ...QueueOptFunc) *Queue[T]

New creates a new queue. You can provide optional configuration functions to customize the queue's behavior.

func (*Queue[T]) Close

func (q *Queue[T]) Close()

Close closes the queue's channels. Items can be dequeued until the queue is empty at which point reading the dequeue channel with v,ok returns false

func (*Queue[T]) Dequeue

func (q *Queue[T]) Dequeue() <-chan T

Dequeue returns the queue's read channel. Reading the channel will block if the queue is empty, or return false on v,ok if the queue has been closed.

func (*Queue[T]) Drain

func (q *Queue[T]) Drain()

Drain empties the queue of any remaining elements and releases resources.

You should call this if you do not expect your consumers to empty the queue themselves, else a goroutine and some memory will be leaked.

If called before Close, Drain will have no effect.

func (*Queue[T]) DrainTo

func (q *Queue[T]) DrainTo(other *Queue[T])

DrainTo appends all remaining elements in this queue to another queue and closes this queue.

You should call this if you do not expect your consumers to empty the queue themselves, else a goroutine and some memory will be leaked.

If called before Close, DrainTo will have no effect.

func (*Queue[T]) Enqueue

func (q *Queue[T]) Enqueue(v T) error

Enqueue adds an element to the back of the queue. If the queue is closed, an error is returned.

You must call Close() when finished appending elements to prevent the Dequeue channel from blocking when the queue empties.

func (*Queue[T]) Len

func (q *Queue[T]) Len() int

Len returns the queue length, i.e. number of items in the queue.

type QueueOptFunc

type QueueOptFunc func(*qopts)

QueueOptFunc is a function that configures a Queue.

func WithInitialCapacity

func WithInitialCapacity(capacity int) QueueOptFunc

WithInitialCapacity is a constructor function to set the initial capacity of the queue's internal ring buffer.

Jump to

Keyboard shortcuts

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