Documentation
¶
Overview ¶
Package flx provides generic stream processing with dynamic concurrency control, explicit context-aware transforms, and reusable retry, timeout, and parallel execution helpers.
Module path: github.com/ezra-sullivan/flx
Index ¶
- Variables
- func DoWithRetry(fn func() error, opts ...RetryOption) error
- func DoWithRetryCtx(ctx context.Context, fn func(context.Context, int) error, opts ...RetryOption) error
- func DoWithTimeout(fn func() error, timeout time.Duration, opts ...TimeoutOption) error
- func DoWithTimeoutCtx(fn func(context.Context) error, timeout time.Duration, opts ...TimeoutOption) error
- func Parallel(fns ...func())
- func ParallelErr(fns ...func() error) error
- func ParallelWithErrorStrategy(strategy ErrorStrategy, fns ...func()) error
- func Reduce[T, R any](s Stream[T], fn func(<-chan T) (R, error)) (R, error)
- func SendContext[T any](ctx context.Context, pipe chan<- T, item T) bool
- type ConcurrencyController
- type DynamicSemaphore
- type ErrorStrategy
- type Option
- func WithDynamicWorkers(controller *ConcurrencyController) Option
- func WithErrorStrategy(strategy ErrorStrategy) Option
- func WithForcedDynamicWorkers(controller *ConcurrencyController) Option
- func WithInterruptibleWorkers(controller *ConcurrencyController) Option
- func WithUnlimitedWorkers() Option
- func WithWorkers(workers int) Option
- type RetryOption
- type Stream
- func Chunk[T any](s Stream[T], n int) Stream[[]T]
- func Concat[T any](s Stream[T], others ...Stream[T]) Stream[T]
- func DistinctBy[T any, K comparable](s Stream[T], fn func(T) K) Stream[T]
- func FlatMap[T, U any](s Stream[T], fn func(T, chan<- U), opts ...Option) Stream[U]
- func FlatMapContext[T, U any](ctx context.Context, s Stream[T], fn func(context.Context, T, chan<- U), ...) Stream[U]
- func FlatMapContextErr[T, U any](ctx context.Context, s Stream[T], fn func(context.Context, T, chan<- U) error, ...) Stream[U]
- func FlatMapErr[T, U any](s Stream[T], fn func(T, chan<- U) error, opts ...Option) Stream[U]
- func From[T any](generate func(chan<- T)) Stream[T]
- func FromChan[T any](source <-chan T) Stream[T]
- func GroupBy[T any, K comparable](s Stream[T], fn func(T) K) Stream[[]T]
- func Map[T, U any](s Stream[T], fn func(T) U, opts ...Option) Stream[U]
- func MapContext[T, U any](ctx context.Context, s Stream[T], fn func(context.Context, T) U, ...) Stream[U]
- func MapContextErr[T, U any](ctx context.Context, s Stream[T], fn func(context.Context, T) (U, error), ...) Stream[U]
- func MapErr[T, U any](s Stream[T], fn func(T) (U, error), opts ...Option) Stream[U]
- func Values[T any](items ...T) Stream[T]
- func (s Stream[T]) AllMatch(predicate func(T) bool) bool
- func (s Stream[T]) AllMatchErr(predicate func(T) bool) (bool, error)
- func (s Stream[T]) AnyMatch(predicate func(T) bool) bool
- func (s Stream[T]) AnyMatchErr(predicate func(T) bool) (bool, error)
- func (s Stream[T]) Buffer(n int) Stream[T]
- func (s Stream[T]) Collect() []T
- func (s Stream[T]) CollectErr() ([]T, error)
- func (s Stream[T]) Concat(others ...Stream[T]) Stream[T]
- func (s Stream[T]) Count() (count int)
- func (s Stream[T]) CountErr() (int, error)
- func (s Stream[T]) Done()
- func (s Stream[T]) DoneErr() error
- func (s Stream[T]) Err() error
- func (s Stream[T]) Filter(fn func(T) bool, opts ...Option) Stream[T]
- func (s Stream[T]) First() (T, bool)
- func (s Stream[T]) FirstErr() (T, bool, error)
- func (s Stream[T]) ForAll(fn func(<-chan T))
- func (s Stream[T]) ForAllErr(fn func(<-chan T)) error
- func (s Stream[T]) ForEach(fn func(T))
- func (s Stream[T]) ForEachErr(fn func(T)) error
- func (s Stream[T]) Head(n int64) Stream[T]
- func (s Stream[T]) Last() (T, bool)
- func (s Stream[T]) LastErr() (T, bool, error)
- func (s Stream[T]) Max(less func(T, T) bool) (T, bool)
- func (s Stream[T]) MaxErr(less func(T, T) bool) (T, bool, error)
- func (s Stream[T]) Min(less func(T, T) bool) (T, bool)
- func (s Stream[T]) MinErr(less func(T, T) bool) (T, bool, error)
- func (s Stream[T]) NoneMatch(predicate func(T) bool) bool
- func (s Stream[T]) NoneMatchErr(predicate func(T) bool) (bool, error)
- func (s Stream[T]) Parallel(fn func(T), opts ...Option)
- func (s Stream[T]) ParallelErr(fn func(T) error, opts ...Option) error
- func (s Stream[T]) Reverse() Stream[T]
- func (s Stream[T]) Skip(n int64) Stream[T]
- func (s Stream[T]) Sort(less func(T, T) bool) Stream[T]
- func (s Stream[T]) Tail(n int64) Stream[T]
- type TimeoutOption
- type WorkerError
Constants ¶
This section is empty.
Variables ¶
var ( ErrNilController = errors.New("flx: nil concurrency controller") ErrInterruptibleWorkersRequireContextTransform = errors.New("flx: WithInterruptibleWorkers/WithForcedDynamicWorkers requires MapContext/FlatMapContext") )
var ( ErrInvalidRetryTimes = errors.New("flx: retry times must be greater than 0") ErrNegativeRetryInterval = errors.New("flx: retry interval must not be negative") ErrNegativeRetryTimeout = errors.New("flx: retry timeout must not be negative") ErrNegativeAttemptTimeout = errors.New("flx: attempt timeout must not be negative") ErrAttemptTimeoutRequiresRetryCtx = errors.New("flx: WithAttemptTimeout requires DoWithRetryCtx") ErrRetryAttemptTimeout = errors.New("flx: retry attempt timeout") )
var ( ErrCanceled = context.Canceled ErrTimeout = context.DeadlineExceeded ErrNilContext = errors.New("flx: nil context") ErrNegativeTimeout = errors.New("flx: timeout must not be negative") )
var ErrInvalidErrorStrategy = errors.New("flx: invalid error strategy")
var ErrWorkerLimitReduced = errors.New("flx: worker canceled because concurrency limit was reduced")
Functions ¶
func DoWithRetry ¶
func DoWithRetry(fn func() error, opts ...RetryOption) error
func DoWithRetryCtx ¶
func DoWithTimeout ¶
func DoWithTimeout(fn func() error, timeout time.Duration, opts ...TimeoutOption) error
func DoWithTimeoutCtx ¶
func ParallelErr ¶
func ParallelWithErrorStrategy ¶
func ParallelWithErrorStrategy(strategy ErrorStrategy, fns ...func()) error
Types ¶
type ConcurrencyController ¶
type ConcurrencyController struct {
// contains filtered or unexported fields
}
func NewConcurrencyController ¶
func NewConcurrencyController(workers int) *ConcurrencyController
func (*ConcurrencyController) ActiveWorkers ¶
func (c *ConcurrencyController) ActiveWorkers() int
func (*ConcurrencyController) SetWorkers ¶
func (c *ConcurrencyController) SetWorkers(n int)
func (*ConcurrencyController) Workers ¶
func (c *ConcurrencyController) Workers() int
type DynamicSemaphore ¶
type DynamicSemaphore struct {
// contains filtered or unexported fields
}
func NewDynamicSemaphore ¶
func NewDynamicSemaphore(n int) *DynamicSemaphore
func (*DynamicSemaphore) Acquire ¶
func (s *DynamicSemaphore) Acquire()
func (*DynamicSemaphore) AcquireCtx ¶
func (s *DynamicSemaphore) AcquireCtx(ctx context.Context) error
func (*DynamicSemaphore) Cap ¶
func (s *DynamicSemaphore) Cap() int
func (*DynamicSemaphore) Current ¶
func (s *DynamicSemaphore) Current() int
func (*DynamicSemaphore) Release ¶
func (s *DynamicSemaphore) Release()
func (*DynamicSemaphore) Resize ¶
func (s *DynamicSemaphore) Resize(n int)
type ErrorStrategy ¶
type ErrorStrategy uint8
const ( ErrorStrategyFailFast ErrorStrategy = iota ErrorStrategyCollect ErrorStrategyLogAndContinue )
func (ErrorStrategy) String ¶
func (s ErrorStrategy) String() string
type Option ¶
type Option func(*opOptions)
func WithDynamicWorkers ¶
func WithDynamicWorkers(controller *ConcurrencyController) Option
WithDynamicWorkers enables graceful dynamic resizing for the current operation. Shrinking does not interrupt workers that already hold a slot.
func WithErrorStrategy ¶
func WithErrorStrategy(strategy ErrorStrategy) Option
WithErrorStrategy configures how worker panic/error is handled for the current operation.
func WithForcedDynamicWorkers ¶
func WithForcedDynamicWorkers(controller *ConcurrencyController) Option
WithForcedDynamicWorkers enables forced dynamic resizing for the current operation. Shrinking cancels excess workers via context and only works with MapContext/FlatMapContext.
func WithInterruptibleWorkers ¶
func WithInterruptibleWorkers(controller *ConcurrencyController) Option
WithInterruptibleWorkers is kept as a compatibility alias for WithForcedDynamicWorkers.
func WithUnlimitedWorkers ¶
func WithUnlimitedWorkers() Option
WithUnlimitedWorkers spawns one worker per item for the current operation.
func WithWorkers ¶
WithWorkers limits the current operation to a fixed number of workers.
type RetryOption ¶
type RetryOption func(*retryOptions)
func WithAttemptTimeout ¶
func WithAttemptTimeout(timeout time.Duration) RetryOption
func WithIgnoreErrors ¶
func WithIgnoreErrors(ignoreErrors []error) RetryOption
func WithInterval ¶
func WithInterval(interval time.Duration) RetryOption
func WithRetry ¶
func WithRetry(times int) RetryOption
func WithTimeout ¶
func WithTimeout(timeout time.Duration) RetryOption
type Stream ¶
type Stream[T any] struct { // contains filtered or unexported fields }
func DistinctBy ¶
func DistinctBy[T any, K comparable](s Stream[T], fn func(T) K) Stream[T]
func FlatMapContext ¶
func FlatMapContextErr ¶
func FlatMapErr ¶
func MapContext ¶
func MapContextErr ¶
func (Stream[T]) CollectErr ¶
func (Stream[T]) ForEachErr ¶
func (Stream[T]) NoneMatchErr ¶
func (Stream[T]) ParallelErr ¶
type TimeoutOption ¶
func WithContext ¶
func WithContext(ctx context.Context) TimeoutOption
type WorkerError ¶
type WorkerError struct {
Err error
}
func (*WorkerError) Error ¶
func (e *WorkerError) Error() string
func (*WorkerError) Unwrap ¶
func (e *WorkerError) Unwrap() error