Documentation
¶
Index ¶
- Constants
- Variables
- func ApplyChan[T any](input <-chan T, fn func(in T), opts ...ApplyOption)
- func ApplySlice[T any](input []T, fn func(in T), opts ...ApplyOption)
- func Execute(cbs ...func() error) []error
- func ExecuteOpts(cbs []func() error, opts ...ExecuteOption) []error
- func MapChan[Input any, Output any](input <-chan Input, fn func(in Input) (Output, error), opts ...MapOption) (<-chan Output, <-chan error)
- func MapSlice[Input any, Output any](input []Input, fn func(in Input) (Output, error), opts ...MapOption) ([]Output, []error)
- func MapSliceOrdered[Input any, Output any](input []Input, fn func(in Input) (Output, error), opts ...MapOption) ([]Output, []error)
- type ApplyOption
- type ConcurrencyLimiter
- type ExecuteOption
- type MapOption
- type PanicError
Constants ¶
const (
ApplyDefaultConcurrency = 10
)
const (
ExecuteDefaultConcurrency = 10
)
const (
MapDefaultConcurrency = 10
)
Variables ¶
var ErrMapSkip = errors.New("skip")
ErrMapSkip is a special error that can be returned from Map function to skip the item.
Functions ¶
func ApplyChan ¶
func ApplyChan[T any](input <-chan T, fn func(in T), opts ...ApplyOption)
ApplyChan executes `fn` on each element of `input` channel in multiple threads. Options:
WithApplyConcurrency(int) - limits the number of parallel threads. Default: ApplyDefaultConcurrency
To stop processing, close `input` channel.
A panic in `fn` does not kill the process: it is recovered in the worker, ApplyChan stops reading new items (so the panic surfaces even if `input` is never closed), waits for already-started workers, and re-raises the first panic in the calling goroutine as *PanicError.
func ApplySlice ¶
func ApplySlice[T any](input []T, fn func(in T), opts ...ApplyOption)
ApplySlice does the same as ApplyChan, but works with slice instead of a channel.
A panic in `fn` does not kill the process: after the first recovered panic no new items are started, already-started workers finish, and the first panic is re-raised in the calling goroutine as *PanicError.
func Execute ¶
Execute executes multiple callback functions `cbs` in parallel.
A panic in a callback does not kill the process: it is recovered in the worker (all callbacks are started immediately, so the others still run), and the first panic is re-raised in the calling goroutine as *PanicError after all workers finish.
func ExecuteOpts ¶
func ExecuteOpts(cbs []func() error, opts ...ExecuteOption) []error
ExecuteOpts executes slice of callback functions `cbs` with custom options.
A panic in a callback does not kill the process: after the first recovered panic no new callbacks are started, already-started ones finish, and the first panic is re-raised in the calling goroutine as *PanicError.
func MapChan ¶
func MapChan[Input any, Output any](input <-chan Input, fn func(in Input) (Output, error), opts ...MapOption) (<-chan Output, <-chan error)
MapChan executes `fn` on each element of `input` channel in several threads.
A panic in `fn` does not kill the process: it is recovered in the worker and delivered to the returned errors channel as *PanicError. MapChan is non-blocking, so the panic can not be re-raised in the calling goroutine.
func MapSlice ¶
func MapSlice[Input any, Output any](input []Input, fn func(in Input) (Output, error), opts ...MapOption) ([]Output, []error)
MapSlice does the same as MapChan, but works with slices instead of channels in input and output.
Unlike MapChan, MapSlice blocks until the workers finish, so the first panic recovered in a worker is re-raised in the calling goroutine as *PanicError. After the first panic no new items are started.
Types ¶
type ApplyOption ¶
type ApplyOption interface {
// contains filtered or unexported methods
}
func WithApplyConcurrency ¶
func WithApplyConcurrency(concurrency int) ApplyOption
type ConcurrencyLimiter ¶
type ConcurrencyLimiter interface {
// Acquire acquires a slot in the concurrency limiter.
// Blocks until a slot is available.
Acquire()
// Release releases a slot in the concurrency limiter.
Release()
}
ConcurrencyLimiter is a helper that can limit amount of concurrently processed requests. See concurrency_limiter_test.go for usage example.
func NewConcurrencyLimiter ¶
func NewConcurrencyLimiter(concurrency int) ConcurrencyLimiter
type ExecuteOption ¶
type ExecuteOption interface {
// contains filtered or unexported methods
}
func WithExecuteConcurrency ¶
func WithExecuteConcurrency(concurrency int) ExecuteOption
type MapOption ¶
type MapOption interface {
// contains filtered or unexported methods
}
func WithMapConcurrency ¶
func WithMapStopOnFirstError ¶
func WithMapStopOnFirstError() MapOption
type PanicError ¶ added in v0.1.2
PanicError wraps a panic recovered in a worker goroutine.
A panic in a goroutine spawned by this package can not be recovered by the caller: recover() only works in the goroutine where the panic happened, and an unrecovered panic in any goroutine kills the whole process. To prevent that, every worker goroutine recovers panics itself and the package propagates them to the caller:
- blocking functions (ApplyChan, ApplySlice, Execute, ExecuteOpts, MapSlice, MapSliceOrdered) re-raise the first recovered panic in the calling goroutine after the workers finish, so the caller's own defer/recover (e.g. a gRPC recovery interceptor) can handle it just like a panic in synchronous code. After the first panic no new items or callbacks are started; workers that are already running finish first. ApplyChan also stops reading from its input channel, so the panic is re-raised even if the channel is never closed;
- MapChan is non-blocking, so recovered panics are delivered to the returned errors channel as *PanicError values and processing continues.
Value holds the original value passed to panic(), Stack holds the stack trace of the worker goroutine captured at the moment of recovery.
func (*PanicError) Error ¶ added in v0.1.2
func (e *PanicError) Error() string
func (*PanicError) Unwrap ¶ added in v0.1.2
func (e *PanicError) Unwrap() error
Unwrap returns the panic value if it is an error, so errors.Is and errors.As see through PanicError.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
apply
command
|
|
|
concurrency_limiter
command
|
|
|
execute
command
|
|
|
map
command
|