Buffer

package
v0.3.35 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DiscardAnyMessage added in v0.3.19

func DiscardAnyMessage[T any](receiver Receiver[T])

DiscardAnyMessage is a function that discards all messages from a receiver.

Parameters:

  • receiver: The receiver of messages.

Behaviors:

  • Use go DiscardAnyMessage(receiver) to discard all messages from the receiver.

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]() *Buffer[T]

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 implements the Runner interface.

func (*Buffer[T]) IsClosed added in v0.3.18

func (b *Buffer[T]) IsClosed() bool

IsClosed implements the Runner interface.

func (*Buffer[T]) Receive added in v0.3.18

func (b *Buffer[T]) Receive() (T, bool)

Receive implements the Receiver interface.

func (*Buffer[T]) Send added in v0.3.18

func (b *Buffer[T]) Send(msg T) bool

Send implements the Sender interface.

func (*Buffer[T]) Start

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

Start implements the Runner interface.

type BufferCondition added in v0.3.18

type BufferCondition int

BufferCondition is an enumeration of the possible conditions of the Buffer.

const (
	// IsEmpty indicates that the Buffer is empty.
	IsEmpty BufferCondition = iota

	// IsRunning indicates that the Buffer is running.
	IsRunning
)

func (BufferCondition) String added in v0.3.18

func (bc BufferCondition) String() string

String implements Common.Enum interface.

type ChannelThrough added in v0.3.19

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

ChannelThrough is a type of buffer that sends messages from multiple receivers to a single sender.

func NewChannelThrough added in v0.3.19

func NewChannelThrough[T any](sender ur.SenderRunner[T], receivers ...Receiver[T]) *ChannelThrough[T]

NewChannelThrough creates a new channel through buffer.

Parameters:

  • sender: The sender of messages.
  • receivers: The receivers of messages.

Returns:

  • *ChannelThrough: The new channel through buffer.

Behaviors:

  • It ignores nil receivers.
  • If the sender is nil, it will discard all messages from the receivers.
  • If no receivers are provided, the buffer will close immediately.
  • Because it closes automatically, there is no Close() method. Thus, if all receivers are closed, the handler will close the sender in a cascading manner.

func (*ChannelThrough[T]) IsRunning added in v0.3.19

func (ct *ChannelThrough[T]) IsRunning() bool

IsRunning is a method that returns true if the handler is running.

Returns:

  • bool: True if the handler is running, false otherwise.

func (*ChannelThrough[T]) Run added in v0.3.19

func (ct *ChannelThrough[T]) Run()

Run is a method that runs the handler.

type Debugger added in v0.3.19

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

Debugger is a struct that provides a way to print debug messages.

func NewDebugger added in v0.3.19

func NewDebugger(logger *log.Logger) *Debugger

NewDebugger is a function that creates a new debugger.

Parameters:

  • logger: The logger to use.

Returns:

  • *Debugger: The new debugger.

func (*Debugger) Close added in v0.3.19

func (d *Debugger) Close()

Close implements the Runner interface.

func (*Debugger) GetDebugMode added in v0.3.19

func (d *Debugger) GetDebugMode() bool

GetDebugMode is a function that returns the debug mode.

Returns:

  • bool: The debug mode.

func (*Debugger) IsClosed added in v0.3.19

func (d *Debugger) IsClosed() bool

IsClosed returns true if the runner is closed, false otherwise.

func (*Debugger) Printf added in v0.3.19

func (d *Debugger) Printf(format string, v ...interface{})

Printf is a function that prints formatted text.

'\n' is always appended to the end of the format string.

Parameters:

  • format: The format string.
  • v: The values to print.

func (*Debugger) Println added in v0.3.19

func (d *Debugger) Println(v ...interface{})

Println is a function that prints a line.

Parameters:

  • v: The values to print.

func (*Debugger) Start added in v0.3.19

func (d *Debugger) Start()

Start implements the Runner interface.

func (*Debugger) ToggleDebugMode added in v0.3.19

func (d *Debugger) ToggleDebugMode(active bool)

ToggleDebugMode is a function that toggles the debug mode.

Parameters:

  • active: The flag to set the debug mode.

func (*Debugger) Write added in v0.3.19

func (d *Debugger) Write(p []byte) (n int, err error)

Write is a function that writes to the debugger.

'\n' is always appended to the end of the bytes.

Parameters:

  • p: The bytes to write.

Returns:

  • int: Always the length of the bytes.
  • error: Always nil.

type Receiver added in v0.3.18

type Receiver[T any] interface {
	// Receive receives a message from the Buffer.
	//
	// Returns:
	//   - T: The message received.
	//   - bool: False if the Buffer is closed, true otherwise.
	Receive() (T, bool)
}

Receiver is the interface that wraps the Receive method.

type Redirect added in v0.3.19

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

Redirect is a handler that redirects messages from a receiver to multiple senders.

func NewRedirect added in v0.3.19

func NewRedirect[T any](receiver Receiver[T], senders ...ur.SenderRunner[T]) *Redirect[T]

NewRedirect creates a new redirect handler.

Parameters:

  • receiver: The receiver of messages.
  • senders: The senders of messages.

Returns:

  • *Redirect: The new redirect handler.

Behaviors:

  • It ignores nil senders.
  • Because it closes automatically, there is no Close() method. Thus, if the receiver is closed, the handler will close all senders in a cascading manner.
  • If no senders are provided, the handler will discard all messages from the receiver.

func (*Redirect[T]) IsRunning added in v0.3.19

func (r *Redirect[T]) IsRunning() bool

IsRunning is a method that returns true if the handler is running.

Returns:

  • bool: True if the handler is running, false otherwise.

func (*Redirect[T]) Run added in v0.3.19

func (r *Redirect[T]) Run()

Run is a method that runs the handler.

Jump to

Keyboard shortcuts

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