Buffer

package
v0.2.34 Latest Latest
Warning

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

Go to latest
Published: May 4, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

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

Buffer is a thread-safe, generic data structure that allows multiple goroutines to produce and consume elements in a synchronized manner. It is implemented as a queue and uses channels to synchronize the goroutines. The Buffer should be initialized with the Init method before use.

func NewBuffer

func NewBuffer[T any](bufferSize int) (*Buffer[T], error)

NewBuffer creates a new Buffer instance.

Parameters:

  • bufferSize: The size of the buffer for the send and receive channels. Must be a non-negative integer. If a negative integer is provided, the method will panic with an *ers.InvalidParameterError.

Returns:

  • *Buffer: A pointer to the newly created Buffer instance.
  • error: An error of type *ers.InvalidParameterError if the buffer size is negative.

Information: To close the buffer, just close the send-only channel. Once that is done, a cascade of events will happen:

  • The goroutine that listens for incoming messages will stop listening and exit.
  • The goroutine that sends messages from the Buffer to the receive channel will stop sending messages once the Buffer is empty, and then exit.
  • The Buffer will be cleaned up.

Of course, a Close method is also provided to manually close the Buffer but it is not necessary to call it if the send-only channel is closed.

func (*Buffer[T]) CleanBuffer

func (b *Buffer[T]) CleanBuffer()

CleanBuffer removes all elements from the Buffer, effectively resetting it to an empty state. Precalculated elements are kept as they are no longer in the buffer but in the channel. It locks the firstMutex to ensure thread-safety during the operation.

This method is safe for concurrent use by multiple goroutines.

func (*Buffer[T]) Close

func (b *Buffer[T]) Close()

Close is a method of the Buffer type that closes the Buffer.

func (*Buffer[T]) GetReceiveChannel

func (b *Buffer[T]) GetReceiveChannel() <-chan T

GetReceiveChannel returns the receive-only channel of the Buffer.

This method is safe for concurrent use by multiple goroutines.

Returns:

  • <-chan T: The receive-only channel of the Buffer.

func (*Buffer[T]) GetSendChannel

func (b *Buffer[T]) GetSendChannel() chan<- T

GetSendChannel returns the send-only channel of the Buffer.

This method is safe for concurrent use by multiple goroutines.

Returns:

  • chan<- T: The send-only channel of the Buffer.

func (*Buffer[T]) Start

func (b *Buffer[T]) Start()

Start is a method of the Buffer type that starts the Buffer by launching the goroutines that listen for incoming messages and send messages from the Buffer to the send channel.

func (*Buffer[T]) Wait

func (b *Buffer[T]) Wait()

Wait is a method of the Buffer type that waits for all goroutines launched by the Buffer to finish executing.

This method is thread-safe and can be called from multiple goroutines.

type SignalBuffer

type SignalBuffer struct {
	// contains filtered or unexported fields
}

SignalBuffer is a thread-safe buffer that can be used to signal changes to a channel. It is safe for concurrent use by multiple goroutines.

func NewSignalBuffer

func NewSignalBuffer() *SignalBuffer

NewSignalBuffer creates a new SignalBuffer instance.

Returns:

  • *SignalBuffer: A pointer to the new SignalBuffer.

func (*SignalBuffer) CleanSignalBuffer

func (b *SignalBuffer) CleanSignalBuffer()

CleanSignalBuffer removes all elements from the SignalBuffer, effectively resetting it to an empty state. Precalculated elements are kept as they are no longer in the buffer but in the channel. It locks the firstMutex to ensure thread-safety during the operation.

This method is safe for concurrent use by multiple goroutines.

func (*SignalBuffer) Close

func (b *SignalBuffer) Close()

Close is a method of the SignalBuffer type that closes the SignalBuffer.

func (*SignalBuffer) GetSignalChan

func (b *SignalBuffer) GetSignalChan() <-chan bool

GetSignalChan returns the send-only channel of the SignalBuffer.

This method is safe for concurrent use by multiple goroutines.

Returns:

  • <-chan bool: The receive-only channel of the SignalBuffer.

func (*SignalBuffer) SignalChange

func (b *SignalBuffer) SignalChange()

SignalChange signals that the SignalBuffer has changed and that the send goroutine should send a message to the send channel.

This method is safe for concurrent use by multiple goroutines.

func (*SignalBuffer) Start

func (b *SignalBuffer) Start()

Start starts the SignalBuffer by launching one goroutine that sends messages from the SignalBuffer to the send channel.

func (*SignalBuffer) Wait

func (b *SignalBuffer) Wait()

Wait is a method of the SignalBuffer type that waits for all goroutines launched by the SignalBuffer to finish executing.

This method is thread-safe and can be called from multiple goroutines.

Jump to

Keyboard shortcuts

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