queue

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 3 Imported by: 10

README

queue GitHub release

GitHub go.mod Go version of a Go module GoDoc reference example

CodeFactor Go Report Card codecov

lint-test grype codeql


Queue is a Go package providing different thread-safe generic queue implementations.

Available queues:

Blocking Queue
  • Waits for the queue have elements available before retrieving from it.

Documentation

Overview

Package queue provides different thread-safe generic queue implementations.

The blocking waits for the queue have elements available before retrieving from it. The blocking queue can be refilled in order to make all elements available again.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoElementsAvailable = errors.New("no elements available in the queue")

ErrNoElementsAvailable is an error returned whenever there are no elements available to be extracted from a queue.

Functions

This section is empty.

Types

type Blocking

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

Blocking provides a read-only queue for a list of T.

It supports operations for retrieving and adding elements to a FIFO queue. If there are no elements available the retrieve operations wait until elements are added to the queue.

func NewBlocking

func NewBlocking[T any](
	elems []T,
	opts ...Option,
) *Blocking[T]

NewBlocking returns a new Blocking Queue containing the given elements.

func (*Blocking[T]) Get added in v0.6.0

func (q *Blocking[T]) Get() (v T, _ error)

Get removes and returns the head of the elements queue. If no element is available it returns an ErrNoElementsAvailable error.

It does not actually remove elements from the elements slice, but it's incrementing the underlying index.

func (*Blocking[T]) Peek added in v0.3.0

func (q *Blocking[T]) Peek() T

Peek retrieves but does not return the head of the queue.

func (*Blocking[T]) Put added in v0.6.0

func (q *Blocking[T]) Put(elem T)

Put inserts the element to the tail the queue, while also increasing the queue size.

func (*Blocking[T]) Reset

func (q *Blocking[T]) Reset()

Reset sets the queue elements index to 0. The queue will be in its initial state.

func (*Blocking[T]) Size added in v0.6.0

func (q *Blocking[T]) Size() int

Size returns the number of elements in the queue.

func (*Blocking[T]) Take

func (q *Blocking[T]) Take() (v T)

Take removes and returns the head of the elements queue. If no element is available it waits until the queue has an element available.

It does not actually remove elements from the elements slice, but it's incrementing the underlying index.

type Option added in v0.6.0

type Option interface {
	// contains filtered or unexported methods
}

An Option configures a Queue using the functional options paradigm.

func WithCapacity added in v0.6.0

func WithCapacity(capacity int) Option

WithCapacity specifies a fixed capacity for a queue.

type Queue added in v0.6.0

type Queue[T any] interface {
	// Peek retrieves but does not remove the head of the queue.
	Peek() T

	// Get retrieves and removes the head of the queue.
	Get() (T, error)

	// Put inserts the element to the tail of the queue.
	Put(T)
}

Queue is a collection that orders elements in a FIFO order. This interface provides basic methods for adding and extracting elements from the queue. Items are extracted from the head of the queue and added to the tail of the queue.

Jump to

Keyboard shortcuts

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