queue

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MPL-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package queue provides a thread-safe queue and ring-queue.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Queue

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

Queue is a thread-safe queue.

func New

func New[T any](size int) Queue[T]

New creates a new queue with the given size.

func (*Queue[T]) Pop

func (q *Queue[T]) Pop(ctx context.Context) (T, error)

Pop pops a value from the queue. It blocks until the value is popped or the context is canceled.

func (*Queue[T]) Push

func (q *Queue[T]) Push(ctx context.Context, v T) error

Push pushes a value to the queue. It blocks until the value is pushed or the context is canceled.

type Ring

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

Ring is a simple ring buffer. When `write` does full circle and reaches `read` it will overwrite the oldest data and move the `read` pointer.

func NewRing

func NewRing[T any](size int) *Ring[T]

NewRing creates a new ring buffer with the given size.

func (*Ring[T]) Init

func (r *Ring[T]) Init(size int)

Init initializes the ring buffer with the given length. It panics if the buffer is already initialized.

func (*Ring[T]) IsFull

func (r *Ring[T]) IsFull() bool

IsFull returns true if the ring buffer is full.

func (*Ring[T]) Len

func (r *Ring[T]) Len() int

Len returns the number of elements in the ring buffer.

func (*Ring[T]) Read

func (r *Ring[T]) Read() (T, bool)

Read reads the next data from the ring buffer. If the buffer is empty, it will return the zero value of the data type and false.

func (*Ring[T]) Write

func (r *Ring[T]) Write(data T)

Write writes the given data to the ring buffer. If the buffer is full, it will overwrite the oldest data. The `read` pointer will be moved to the next position.

type RingQueue

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

RingQueue is a thread-safe ring queue.

func NewRingQueue

func NewRingQueue[T any](size int) *RingQueue[T]

NewRingQueue creates a new ring queue with the given size.

func (*RingQueue[T]) Pop

func (q *RingQueue[T]) Pop(ctx context.Context) (T, error)

Pop pops a value from the queue. It blocks until the value is popped or the context is canceled.

func (*RingQueue[T]) Push

func (q *RingQueue[T]) Push(ctx context.Context, v T) error

Push pushes a value to the queue. It blocks until the value is pushed (likely) or the context is canceled.

Jump to

Keyboard shortcuts

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