structs

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultBufferSize is the default buffer size (64K entries)
	DefaultBufferSize = 1 << 16
	// MinBufferSize is the minimum buffer size (64 entries)
	MinBufferSize = 1 << 6
)

Variables

This section is empty.

Functions

func AddWellKnownMetaProperties

func AddWellKnownMetaProperties(props ...string)

Adds new well-known meta properties, but only if they don't already exist.

func GetWellKnownMetaProperties

func GetWellKnownMetaProperties() []string

Returns a copy of a slice with all well-known meta properties.

func HasWellKnownMetaProperty

func HasWellKnownMetaProperty(prop string) bool

Reports if the specified property exists in well-known meta properties.

func RemoveWellKnownMetaProperties

func RemoveWellKnownMetaProperties(props ...string)

Removes specified well-known meta properties.

Types

type Disruptor

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

Implements the LMAX Disruptor pattern for high-performance ring buffer messaging.

func NewDisruptor

func NewDisruptor[T any]() *Disruptor[T]

creates a new Disruptor with default buffer size (64K)

func NewDisruptorWithSize

func NewDisruptorWithSize[T any](size int) (*Disruptor[T], error)

Creates a new Disruptor with specified buffer size. Buffer size must be a power of 2 and at least MinBufferSize.

func (*Disruptor[T]) Close

func (d *Disruptor[T]) Close()

Closes the Disruptor and signals all goroutines to stop. After closing, the disruptor cannot be used again.

func (*Disruptor[T]) Consume

func (d *Disruptor[T]) Consume(handler func(T)) error

Starts consuming entries from the disruptor using the provided handler. This method blocks until the disruptor is closed. Returns error if disruptor is already closed.

func (*Disruptor[T]) IsEmpty

func (d *Disruptor[T]) IsEmpty() bool

Returns true if all entries have been processed

func (*Disruptor[T]) Publish

func (d *Disruptor[T]) Publish(entry T) bool

Attempts to add an entry to the disruptor buffer. Returns false if buffer is full or if Disruptor is closed.

type DynamicStack added in v1.2.0

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

Last-In-First-Out dynamic stack data structure.

func NewDynamicStack added in v1.2.0

func NewDynamicStack[T any](capacity int) *DynamicStack[T]

Sets capacity to default if it's <= 0.

func (*DynamicStack[T]) IsEmpty added in v1.2.0

func (s *DynamicStack[T]) IsEmpty() bool

func (*DynamicStack[T]) Peek added in v1.2.0

func (s *DynamicStack[T]) Peek() (T, bool)

Gets top element from the stack. Returns false if stack is empty.

func (*DynamicStack[T]) Pop added in v1.2.0

func (s *DynamicStack[T]) Pop() (T, bool)

Removes top element from the stack. Returns false if stack is empty.

func (*DynamicStack[T]) PopBatch added in v1.2.0

func (s *DynamicStack[T]) PopBatch(n int64) ([]T, bool)

Removes n top elements from the stack. Returns false if stack is empty.

func (*DynamicStack[T]) Push added in v1.2.0

func (s *DynamicStack[T]) Push(v T) bool

Pushes new element on top of the stack. Always returns true.

func (*DynamicStack[T]) PushBatch added in v1.2.0

func (s *DynamicStack[T]) PushBatch(v ...T) bool

Pushes new elements on top of the stack. Always returns true.

func (*DynamicStack[T]) Size added in v1.2.0

func (s *DynamicStack[T]) Size() int64

Returns amount of elements in the stack.

func (*DynamicStack[T]) ToSlice added in v1.2.0

func (s *DynamicStack[T]) ToSlice() []T

type Meta

type Meta map[string]any

Represents metadata as a map of string keys to any values.

func (Meta) String

func (m Meta) String() string

Returns a formatted string representation of well-known metadata properties. Only properties registered as well-known will be included in the output.

type Stack added in v1.2.0

type Stack[T any] interface {
	// Pushes new element on top of the stack.
	// Returns false if stack is overflowed (only if it can be overflowed).
	Push(v T) bool
	// Pushes new elements on top of the stack.
	// Returns false if stack is overflowed (only if it can be overflowed).
	PushBatch(v ...T) bool
	// Removes top element from the stack.
	// Returns false if stack is empty.
	Pop() (T, bool)
	// Removes n top elements from the stack.
	// Returns false if stack is empty.
	PopBatch(n int64) ([]T, bool)
	// Gets top element from the stack.
	// Returns false if stack is empty.
	Peek() (T, bool)
	// Returns amount of elements in the stack.
	Size() int64
	IsEmpty() bool
	ToSlice() []T
}

Represents Last-In-First-Out stack.

type StaticStack added in v1.2.0

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

Last-In-First-Out static stack data structure.

func NewStaticStack added in v1.2.0

func NewStaticStack[T any](capacity int64) *StaticStack[T]

Panics if capacity is <= 0.

func (*StaticStack[T]) Capacity added in v1.2.0

func (s *StaticStack[T]) Capacity() int64

Returns max stack size.

func (*StaticStack[T]) IsEmpty added in v1.2.0

func (s *StaticStack[T]) IsEmpty() bool

func (*StaticStack[T]) IsFull added in v1.2.0

func (s *StaticStack[T]) IsFull() bool

func (*StaticStack[T]) Peek added in v1.2.0

func (s *StaticStack[T]) Peek() (T, bool)

Gets top element from the stack. Returns false if stack is empty.

func (*StaticStack[T]) Pop added in v1.2.0

func (s *StaticStack[T]) Pop() (T, bool)

Removes top element from the stack. Returns false if stack is empty.

func (*StaticStack[T]) PopBatch added in v1.2.0

func (s *StaticStack[T]) PopBatch(n int64) ([]T, bool)

Removes n top elements from the stack. Returns false if stack is empty.

func (*StaticStack[T]) Push added in v1.2.0

func (s *StaticStack[T]) Push(v T) bool

Pushes new element on top of the stack. Returns false if stack is overflowed.

func (*StaticStack[T]) PushBatch added in v1.2.0

func (s *StaticStack[T]) PushBatch(v ...T) bool

Pushes new elements on top of the stack. Returns false if stack is overflowed (only if it can be overflowed).

func (*StaticStack[T]) Size added in v1.2.0

func (s *StaticStack[T]) Size() int64

Returns amount of elements in the stack.

func (*StaticStack[T]) ToSlice added in v1.2.0

func (s *StaticStack[T]) ToSlice() []T

type SyncQueue

type SyncQueue[T comparable] struct {
	// contains filtered or unexported fields
}

Concurrency-safe first-in-first-out (FIFO) queue.

func NewSyncQueue

func NewSyncQueue[T comparable](sizeLimit int) *SyncQueue[T]

Creates a new thread-safe queue with optional size limit. To disable size limit, set sizeLimit <= 0.

func (*SyncQueue[T]) Peek

func (q *SyncQueue[T]) Peek() (T, bool)

Returns the first element of the queue without removing it. If queue isn't empty - returns first element and true. If queue is empty - returns zero-value of T and false.

func (*SyncQueue[T]) Pop

func (q *SyncQueue[T]) Pop() (T, bool)

Removes and returns the first element of the queue. Same as Peek(), but also deletes first element in queue.

func (*SyncQueue[T]) PopN

func (q *SyncQueue[T]) PopN(n int) ([]T, bool)

Removes and returns up to n elements from the queue. If n is greater than queue size, n will be adjusted to the queue size to prevent panic. If queue is empty - returns nil and false.

func (*SyncQueue[T]) Preserve

func (q *SyncQueue[T]) Preserve()

Saves the head element of the queue for potential rollback.

func (*SyncQueue[T]) PreserveAndPop

func (q *SyncQueue[T]) PreserveAndPop() (T, bool)

Preserves the current head element and then pops it from the queue. This is equivalent to calling Preserve() followed by Pop().

func (*SyncQueue[T]) Push

func (q *SyncQueue[T]) Push(v T) error

Appends v to the end of queue. Returns error if size limit is exceeded.

func (*SyncQueue[T]) RollBack

func (q *SyncQueue[T]) RollBack()

Restores the previously preserved element to the front of the queue. Does nothing if no element was preserved.

func (*SyncQueue[T]) Size

func (q *SyncQueue[T]) Size() int

func (*SyncQueue[T]) Unwrap

func (q *SyncQueue[T]) Unwrap() []T

Returns a copy of the internal slice containing all current queue elements.

func (*SyncQueue[T]) UnwrapAndFlush

func (q *SyncQueue[T]) UnwrapAndFlush() []T

Returns a copy of all current queue elements and removes them from the queue. Same as Unwrap, but also deletes all elements in queue.

func (*SyncQueue[T]) WaitTillEmpty

func (q *SyncQueue[T]) WaitTillEmpty(timeout time.Duration) error

Blocks until the queue becomes empty. To disable timeout, set it to <= 0. Returns errs.StatusTimeout if timeout exceeded, nil otherwise.

func (*SyncQueue[T]) WaitTillNotEmpty

func (q *SyncQueue[T]) WaitTillNotEmpty(timeout time.Duration) error

Blocks until the queue contains at least one element. To disable timeout, set it to <= 0. Returns errs.StatusTimeout if timeout exceeded, nil otherwise.

type SyncStack added in v1.2.0

type SyncStack[T any, S Stack[T]] struct {
	// contains filtered or unexported fields
}

Wrapper that makes S thread-Safe. It just wraps Stack methods of S using mutex, so it's not a some kind of stack on it's own. (although it inherently implements Stack interface)

func NewSyncStack added in v1.2.0

func NewSyncStack[T any, S Stack[T]](s S) *SyncStack[T, S]

func (*SyncStack[T, S]) IsEmpty added in v1.2.0

func (s *SyncStack[T, S]) IsEmpty() bool

func (*SyncStack[T, S]) Peek added in v1.2.0

func (s *SyncStack[T, S]) Peek() (T, bool)

Gets top element from S. Returns false if stack is empty.

func (*SyncStack[T, S]) Pop added in v1.2.0

func (s *SyncStack[T, S]) Pop() (T, bool)

Removes top element from S. Returns false if stack is empty.

func (*SyncStack[T, S]) PopBatch added in v1.2.0

func (s *SyncStack[T, S]) PopBatch(n int64) ([]T, bool)

Removes n top elements from the stack. Returns false if stack is empty.

func (*SyncStack[T, S]) Push added in v1.2.0

func (s *SyncStack[T, S]) Push(v T) bool

Pushes new element on top of S. Returns false if S is overflowed (only if it can be overflowed).

func (*SyncStack[T, S]) PushBatch added in v1.2.0

func (s *SyncStack[T, S]) PushBatch(v ...T) bool

Pushes new elements on top of the stack. Returns false if stack is overflowed (only if it can be overflowed).

func (*SyncStack[T, S]) Size added in v1.2.0

func (s *SyncStack[T, S]) Size() int64

Returns amount of elements in S.

func (*SyncStack[T, S]) ToSlice added in v1.2.0

func (s *SyncStack[T, S]) ToSlice() []T

type Task

type Task interface {
	// Executes the task's logic.
	Process()
}

Represents an executable task that can be processed by a worker pool.

type WorkerPool

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

Manages a pool of goroutine workers that process tasks concurrently.

func NewWorkerPool

func NewWorkerPool(ctx context.Context, opt *WorkerPoolOptions) *WorkerPool

Creates a new worker pool with the given parent context and options. If opt is nil, it will be created using default values of WorkerPoolOptions fields.

func (*WorkerPool) Cancel

func (wp *WorkerPool) Cancel() error

Gracefully shuts down the worker pool. The worker pool will finish all its tasks before stopping. Once canceled, the worker pool cannot be started again.

func (*WorkerPool) IsCanceled

func (wp *WorkerPool) IsCanceled() bool

Returns true if the worker pool has been canceled.

func (*WorkerPool) Push

func (wp *WorkerPool) Push(t Task) error

Adds a new task to the worker pool queue. Returns error if worker pool is canceled.

func (*WorkerPool) Start

func (wp *WorkerPool) Start(workerCount int)

Starts the worker pool with the specified number of worker goroutines. This method is idempotent - subsequent calls will have no effect.

type WorkerPoolOptions

type WorkerPoolOptions struct {
	// Default: 1. If <= 0, will be set to default.
	BatchSize int
	// Maximum time to wait for tasks to complete on shutdown.
	// Default: 1s. If <= 0, will be set to default.
	StopTimeout time.Duration
}

Holds configuration options for a worker pool.

Jump to

Keyboard shortcuts

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