Documentation
¶
Overview ¶
Package queue provides an unbounded, goroutine-safe FIFO queue that delivers its items through a Go channel.
New returns a Queue and starts a pump goroutine. Producers call Queue.Add, which never blocks because the items wait in a ring buffer that grows as needed. Consumers receive the items in FIFO order from the channel returned by Queue.Next. The options ChanLen and InitialBufferSize set the channel capacity and the initial buffer length.
Queue.Close discards every item that has not been received yet, closes the channel and waits for the pump goroutine to exit. Close is idempotent, and Add after Close is a no-op. There is no drain operation: to process every item, count the handled items, for example with a sync.WaitGroup, and call Close after the last one.
See the README for the full method table, the Close semantics and a clean shutdown example.
Index ¶
Constants ¶
const ( // DefaultChanLen is the consumer-channel capacity used by New // when no ChanLen option is passed. DefaultChanLen = 32 // DefaultInitialBufferSize is the initial ring-buffer capacity used // by New when no InitialBufferSize option is passed. DefaultInitialBufferSize = 32 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChanLen ¶
type ChanLen int
ChanLen is an Option that sets the capacity of the buffered channel a Queue exposes to consumers. It is a pure throughput knob: a larger value lets more items sit ready to be consumed and reduces how often the internal pump goroutine has to run. It must be >= 0; New panics otherwise.
type InitialBufferSize ¶
type InitialBufferSize int
InitialBufferSize is an Option that sets the initial capacity of the internal ring-buffer that holds items the consumer channel cannot accept yet. The buffer grows automatically as needed. It must be >= 1; New panics otherwise.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is an optional configuration value for New. The available options are ChanLen and InitialBufferSize.
type Queue ¶
Queue is an unbounded, goroutine-safe FIFO queue of items of type T that exposes a channel for consuming them. Items are added via Add and received from the channel returned by Next. An internal ring-buffer holds items until the consumer channel can accept them, so Add never blocks. Call Close when the queue is no longer needed; the consumer channel is closed, every item still queued is discarded, and the background pump goroutine exits.
func New ¶
New returns a new Queue of items of type T, configured by the optional Options. Without options the consumer channel has capacity DefaultChanLen and the internal ring-buffer starts at DefaultInitialBufferSize; the ring-buffer grows without bound, so Add never blocks regardless of the configuration. A background goroutine pumps items from the ring-buffer into the channel; call Close to stop it and release resources.
New panics if a ChanLen below 0 or an InitialBufferSize below 1 is configured.