RWSafe

package
v0.2.12 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2024 License: MIT Imports: 4 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 (*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]) Init

func (b *Buffer[T]) Init(bufferSize int) (chan<- T, <-chan T)

Init initializes a Buffer instance. It ensures the initialization is done only once, even if called multiple times. It creates two channels and a condition variable, and starts two goroutines:

  • one that listens for incoming messages on the send channel and adds them to the Buffer.
  • another that sends messages from the Buffer to the receive channel.

This method should be called before using the Buffer.

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:

  • A send-only channel of type T. Messages sent to this channel are added to the Buffer.
  • A receive-only channel of type T. Messages from the Buffer are sent to this channel.

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.

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 RWSafe

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

RWSafe is a thread-safe, generic data structure that allows multiple goroutines to read and write to a value in a synchronized manner.

func NewRWSafe

func NewRWSafe[T any](value T) *RWSafe[T]

NewRWSafe is a function that creates and returns a new instance of a RWSafe.

Parameters:

  • value: The initial value to be stored in the RWSafe.

Returns:

  • *RWSafe[T]: A pointer to the newly created RWSafe.

func (*RWSafe[T]) DoRead added in v0.2.9

func (rw *RWSafe[T]) DoRead(f func(T))

DoRead is a method of the RWSafe type. It is used to perform a read operation on the value stored in the RWSafe. Through the function parameter, the caller can access the value in a read-only manner.

Parameters:

  • f: A function that takes a value of type T as a parameter and returns nothing.

func (*RWSafe[T]) DoWrite added in v0.2.9

func (rw *RWSafe[T]) DoWrite(f func(*T))

DoWrite is a method of the RWSafe type. It is used to perform a write operation on the value stored in the RWSafe. Through the function parameter, the caller can access the value in a read-write manner.

Parameters:

  • f: A function that takes a pointer to a value of type T as a parameter and returns nothing.

func (*RWSafe[T]) Get

func (rw *RWSafe[T]) Get() T

Get is a method of the RWSafe type. It is used to retrieve the value stored in the RWSafe.

Returns:

  • T: The value stored in the RWSafe.

func (*RWSafe[T]) Set

func (rw *RWSafe[T]) Set(value T)

Set is a method of the RWSafe type. It is used to set the value stored in the RWSafe.

Parameters:

  • value: The new value to be stored in the RWSafe.

Jump to

Keyboard shortcuts

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