ring

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDisposed ring buffer is closed
	ErrDisposed = moerr.NewInvalidStateNoCtx("ring buffer closed")
	// ErrTimeout ring buffer timeout
	ErrTimeout = moerr.NewInvalidStateNoCtx("ring buffer timeout")
)

Functions

This section is empty.

Types

type RingBuffer

type RingBuffer[V any] struct {
	// contains filtered or unexported fields
}

RingBuffer is a MPMC buffer that achieves threadsafety with CAS operations only. A put on full or get on empty call will block until an item is put or retrieved. Calling Dispose on the RingBuffer will unblock any blocked threads with an error. This buffer is similar to the buffer described here: http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue with some minor additions.

func NewRingBuffer

func NewRingBuffer[V any](size uint64) *RingBuffer[V]

NewRingBuffer will allocate, initialize, and return a ring buffer with the specified size.

func (*RingBuffer[V]) Cap

func (rb *RingBuffer[V]) Cap() uint64

Cap returns the capacity of this ring buffer.

func (*RingBuffer[V]) Dispose

func (rb *RingBuffer[V]) Dispose()

Dispose will dispose of this queue and free any blocked threads in the Put and/or Get methods. Calling those methods on a disposed queue will return an error.

func (*RingBuffer[V]) Get

func (rb *RingBuffer[V]) Get() (V, bool, error)

Get will return the next item in the queue. This call will block if the queue is empty. This call will unblock when an item is added to the queue or Dispose is called on the queue. An error will be returned if the queue is disposed.

func (*RingBuffer[V]) IsDisposed

func (rb *RingBuffer[V]) IsDisposed() bool

IsDisposed will return a bool indicating if this queue has been disposed.

func (*RingBuffer[V]) Len

func (rb *RingBuffer[V]) Len() uint64

Len returns the number of items in the queue.

func (*RingBuffer[V]) MustGet

func (rb *RingBuffer[V]) MustGet() V

MustGet is similar to Get, but panic if returns error or not found

func (*RingBuffer[V]) Offer

func (rb *RingBuffer[V]) Offer(item V) (bool, error)

Offer adds the provided item to the queue if there is space. If the queue is full, this call will return false. An error will be returned if the queue is disposed.

func (*RingBuffer[V]) Poll

func (rb *RingBuffer[V]) Poll(timeout time.Duration) (V, bool, error)

Poll will return the next item in the queue. This call will block if the queue is empty. This call will unblock when an item is added to the queue, Dispose is called on the queue, or the timeout is reached. An error will be returned if the queue is disposed or a timeout occurs. A non-positive timeout will block indefinitely.

func (*RingBuffer[V]) Put

func (rb *RingBuffer[V]) Put(item V) error

Put adds the provided item to the queue. If the queue is full, this call will block until an item is added to the queue or Dispose is called on the queue. An error will be returned if the queue is disposed.

func (*RingBuffer[V]) Reset

func (rb *RingBuffer[V]) Reset()

Reset reset the ring buffer, the call to reset needs to ensure that no concurrent reads or writes occur on the ringbuffer.

Jump to

Keyboard shortcuts

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