dynchan

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2024 License: MIT Imports: 2 Imported by: 0

README

Dynchan - Go channel with a dynamic buffer

Go Reference Go Report Card Tests Coverage Status

This is dynchan, a library that provides a Go-like channel with a dynamic buffer.

The buffer in a normal Go channel (when it has one) has a fixed size and behavior. The buffer in a dynchan Chan[T] resizes dynamically (so sends never block) and can also change behavior. This library includes a buffer with normal FIFO behavior, plus another that has heap (a.k.a. priority-queue) behavior. Other behaviors can be added by implementing the Buffer interface.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

type Buffer[T any] interface {
	// Enqueue adds an item to the buffer.
	// It must be safe for concurrent use by multiple goroutines.
	Enqueue(T)

	// Dequeue removes and returns an item from the buffer, and true.
	// If the buffer is empty and closed, Dequeue returns the zero value and false.
	// If the buffer is empty but not closed,
	// Dequeue blocks until an item is added to the buffer or the buffer is closed.
	// It must be safe for concurrent use by multiple goroutines.
	Dequeue() (T, bool)

	// Close closes the buffer,
	// signaling that no more items will be added to it.
	Close()
}

Buffer is a dynamically sized buffer for use in a dynamic channel.

func NewFifo

func NewFifo[T any]() Buffer[T]

NewFifo creates a new FIFO queue for use as a buffer in a dynamic channel.

func NewHeap

func NewHeap[T cmp.Ordered]() Buffer[T]

NewHeap creates a new heap, a.k.a. a priority queue, for use as a buffer in a dynamic channel. It is equivalent to calling NewHeapFunc with cmp.Less.

func NewHeapFunc

func NewHeapFunc[T any](less func(T, T) bool) Buffer[T]

NewHeapFunc creates a new heap, a.k.a. a priority queue, for use as a buffer in a dynamic channel. The less function is used to compare elements in the heap, and the top of the heap is always the "least" element.

type Chan

type Chan[T any] struct {
	Send chan<- T
	Recv <-chan T
	// contains filtered or unexported fields
}

Chan is a dynamic channel. It is like a Go channel but with a dynamically sized buffer. (Normal Go channels have a fixed-size buffer.) Because of this, sends never block. To send items to the Chan, use its Send field. To receive items from the Chan, use Recv. To signal the end of input, close the Send channel.

func New

func New[T any]() Chan[T]

New creates a new Chan. This is equivalent to calling NewWithBuffer with a FIFO queue from NewFifo. You must close the Chan's Send channel when you are done sending items. You must also call the Chan's Close method when you are done receiving items.

func NewWithBuffer

func NewWithBuffer[T any](b Buffer[T]) Chan[T]

NewWithBuffer creates a new Chan with a given buffer. This can be NewFifo for normal channel semantics, NewHeap for priority-queue semantics, or any type implementing the Buffer interface. You must close the Chan's Send channel when you are done sending items. You must also call the Chan's Close method when you are done receiving items.

func (*Chan[T]) Close added in v0.2.0

func (dc *Chan[T]) Close()

Close closes the receiving end of the Chan.

Jump to

Keyboard shortcuts

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