Documentation
¶
Index ¶
- Variables
- func WithContextCall(op func(context.Context))
- func WithContextCallErr(op func(context.Context) error) error
- func WithContextCallOk(op func(context.Context) bool) bool
- func WithContextDo[T any](op func(context.Context) T) T
- func WithContextDoErr[T any](op func(context.Context) (T, error)) (out T, err error)
- func WithContextDoOk[T any](op func(context.Context) (T, bool)) (T, bool)
- func WithContextTimeoutCall(dur time.Duration, op func(context.Context))
- func WrapWithContextCall(ctx context.Context, op func(context.Context)) func()
- func WrapWithContextDo[T any](ctx context.Context, op func(context.Context) T) func() T
- func WrapWithContextDoErr[T any](ctx context.Context, op func(context.Context) (T, error)) func() (T, error)
- func WrapWithContextDoOk[T any](ctx context.Context, op func(context.Context) (T, bool)) func() (T, bool)
- type Constructors
- func (Constructors) ContextChannelWorker() Worker
- func (Constructors) ConvertOperationToWorker() Converter[Operation, Worker]
- func (Constructors) ConvertWorkerToOperation(eh fn.Handler[error]) Converter[Worker, Operation]
- func (Constructors) ErrorChannelWorker(ch <-chan error) Worker
- func (Constructors) OperationHandler() Handler[Operation]
- func (Constructors) Signal() (func(), Worker)
- func (Constructors) WorkerHandler() Handler[Worker]
- type Converter
- type Future
- func CheckedFuture[T any](op func() (T, bool)) Future[T]
- func MakeFuture[T any](fn func() (T, error)) Future[T]
- func NewFuture[T any](fn func(ctx context.Context) (T, error)) Future[T]
- func PtrFuture[T any](fn func() *T) Future[T]
- func StaticFuture[T any](val T, err error) Future[T]
- func ValueFuture[T any](val T) Future[T]
- func WrapFuture[T any](f fn.Future[T]) Future[T]
- func (pf Future[T]) After(ts time.Time) Future[T]
- func (pf Future[T]) Check(ctx context.Context) (T, bool)
- func (pf Future[T]) Delay(d time.Duration) Future[T]
- func (pf Future[T]) Filter(fl func(T) bool) Future[T]
- func (pf Future[T]) Force() fn.Future[T]
- func (pf Future[T]) Future(ctx context.Context, ob fn.Handler[error]) fn.Future[T]
- func (pf Future[T]) If(cond bool) Future[T]
- func (pf Future[T]) Ignore(ctx context.Context) fn.Future[T]
- func (pf Future[T]) Jitter(jf func() time.Duration) Future[T]
- func (pf Future[T]) Join(next Future[T]) Future[T]
- func (pf Future[T]) Limit(in int) Future[T]
- func (pf Future[T]) Lock() Future[T]
- func (pf Future[T]) Must(ctx context.Context) fn.Future[T]
- func (pf Future[T]) Once() Future[T]
- func (pf Future[T]) PostHook(op func()) Future[T]
- func (pf Future[T]) PreHook(op Operation) Future[T]
- func (pf Future[T]) Read(ctx context.Context) (T, error)
- func (pf Future[T]) Resolve() T
- func (pf Future[T]) Retry(n int) Future[T]
- func (pf Future[T]) TTL(dur time.Duration) Future[T]
- func (pf Future[T]) Wait() (T, error)
- func (pf Future[T]) When(cond func() bool) Future[T]
- func (pf Future[T]) WithCancel() (Future[T], context.CancelFunc)
- func (pf Future[T]) WithErrorCheck(ef fn.Future[error]) Future[T]
- func (pf Future[T]) WithErrorFilter(ef erc.Filter) Future[T]
- func (pf Future[T]) WithLock(mtx *sync.Mutex) Future[T]
- func (pf Future[T]) WithLocker(mtx sync.Locker) Future[T]
- func (pf Future[T]) WithRecover() Future[T]
- func (pf Future[T]) WithoutErrors(errs ...error) Future[T]
- type Handler
- func (pf Handler[T]) After(ts time.Time) Handler[T]
- func (pf Handler[T]) Capture() fn.Handler[T]
- func (pf Handler[T]) Check(ctx context.Context, in T) bool
- func (pf Handler[T]) Delay(dur time.Duration) Handler[T]
- func (pf Handler[T]) Filter(fl func(T) bool) Handler[T]
- func (pf Handler[T]) Force(in T)
- func (pf Handler[T]) Handler(ctx context.Context, oe fn.Handler[error]) fn.Handler[T]
- func (pf Handler[T]) If(c bool) Handler[T]
- func (pf Handler[T]) Ignore(ctx context.Context, in T)
- func (pf Handler[T]) Jitter(jf func() time.Duration) Handler[T]
- func (pf Handler[T]) Job(in T) func(context.Context) error
- func (pf Handler[T]) Join(pfs ...Handler[T]) Handler[T]
- func (pf Handler[T]) Limit(n int) Handler[T]
- func (pf Handler[T]) Lock() Handler[T]
- func (pf Handler[T]) Must(ctx context.Context, in T)
- func (pf Handler[T]) Once() Handler[T]
- func (pf Handler[T]) PostHook(op func()) Handler[T]
- func (pf Handler[T]) PreHook(op Operation) Handler[T]
- func (pf Handler[T]) Read(ctx context.Context, in T) error
- func (pf Handler[T]) TTL(dur time.Duration) Handler[T]
- func (pf Handler[T]) Wait(in T) error
- func (pf Handler[T]) When(c func() bool) Handler[T]
- func (pf Handler[T]) WithCancel() (Handler[T], context.CancelFunc)
- func (pf Handler[T]) WithErrorFilter(ef erc.Filter) Handler[T]
- func (pf Handler[T]) WithLock(mtx *sync.Mutex) Handler[T]
- func (pf Handler[T]) WithLocker(mtx sync.Locker) Handler[T]
- func (pf Handler[T]) WithRecover() Handler[T]
- func (pf Handler[T]) WithoutErrors(errs ...error) Handler[T]
- type Operation
- func (wf Operation) Add(ctx context.Context, wg *WaitGroup)
- func (wf Operation) After(ts time.Time) Operation
- func (wf Operation) Background(ctx context.Context)
- func (wf Operation) Delay(dur time.Duration) Operation
- func (wf Operation) Go() Operation
- func (wf Operation) Group(n int) Operation
- func (wf Operation) If(cond bool) Operation
- func (wf Operation) Interval(dur time.Duration) Operation
- func (wf Operation) Jitter(dur func() time.Duration) Operation
- func (wf Operation) Job() func(context.Context) error
- func (wf Operation) Join(ops ...Operation) Operation
- func (wf Operation) Launch(ctx context.Context) Operation
- func (wf Operation) Limit(in int) Operation
- func (wf Operation) Lock() Operation
- func (wf Operation) Once() Operation
- func (wf Operation) PostHook(hook func()) Operation
- func (wf Operation) PreHook(hook Operation) Operation
- func (wf Operation) Run(ctx context.Context)
- func (wf Operation) Signal(ctx context.Context) <-chan struct{}
- func (wf Operation) StartGroup(ctx context.Context, n int) Operation
- func (wf Operation) TTL(dur time.Duration) Operation
- func (wf Operation) Wait()
- func (wf Operation) When(cond func() bool) Operation
- func (wf Operation) While() Operation
- func (wf Operation) WithCancel() (Operation, context.CancelFunc)
- func (wf Operation) WithContextHook(hook func(context.Context) context.Context) Operation
- func (wf Operation) WithErrorHook(ef fn.Handler[error]) Worker
- func (wf Operation) WithLock(mtx *sync.Mutex) Operation
- func (wf Operation) WithRecover() Worker
- func (wf Operation) Worker() Worker
- type WaitGroup
- func (wg *WaitGroup) Add(num int)
- func (wg *WaitGroup) Done()
- func (wg *WaitGroup) Group(n int, op Operation) Operation
- func (wg *WaitGroup) Inc()
- func (wg *WaitGroup) IsDone() bool
- func (wg *WaitGroup) Launch(ctx context.Context, op Operation)
- func (wg *WaitGroup) Num() int
- func (wg *WaitGroup) Operation() Operation
- func (wg *WaitGroup) StartGroup(ctx context.Context, n int, op Operation) Operation
- func (wg *WaitGroup) Wait(ctx context.Context)
- func (wg *WaitGroup) Worker() Worker
- type Worker
- func (wf Worker) After(ts time.Time) Worker
- func (wf Worker) Background(ctx context.Context, ob fn.Handler[error]) Operation
- func (wf Worker) Check(ctx context.Context) bool
- func (wf Worker) Delay(dur time.Duration) Worker
- func (wf Worker) Group(n int) Worker
- func (wf Worker) If(cond bool) Worker
- func (wf Worker) Ignore() Operation
- func (wf Worker) Interval(dur time.Duration) Worker
- func (wf Worker) Jitter(jf func() time.Duration) Worker
- func (wf Worker) Job() func(context.Context) error
- func (wf Worker) Join(wfs ...Worker) Worker
- func (wf Worker) Launch(ctx context.Context) Worker
- func (wf Worker) Limit(n int) Worker
- func (wf Worker) Lock() Worker
- func (wf Worker) Must() Operation
- func (wf Worker) Once() Worker
- func (wf Worker) Operation(ob fn.Handler[error]) Operation
- func (wf Worker) PostHook(post func()) Worker
- func (wf Worker) PreHook(pre Operation) Worker
- func (wf Worker) Retry(n int) Worker
- func (wf Worker) Run(ctx context.Context) error
- func (wf Worker) Signal(ctx context.Context) <-chan error
- func (wf Worker) StartGroup(ctx context.Context, n int) Worker
- func (wf Worker) TTL(dur time.Duration) Worker
- func (wf Worker) Wait() error
- func (wf Worker) When(cond func() bool) Worker
- func (wf Worker) While() Worker
- func (wf Worker) WithCancel() (Worker, context.CancelFunc)
- func (wf Worker) WithContextHook(hook func(context.Context) context.Context) Worker
- func (wf Worker) WithErrorFilter(ef erc.Filter) Worker
- func (wf Worker) WithErrorHook(eh fn.Handler[error]) Worker
- func (wf Worker) WithLock(mtx sync.Locker) Worker
- func (wf Worker) WithRecover() Worker
- func (wf Worker) WithoutErrors(errs ...error) Worker
Constants ¶
This section is empty.
Variables ¶
var MAKE = Constructors{}
MAKE provides namespaced access to the constructors provided by the Constructors type.
Functions ¶
func WithContextCall ¶ added in v0.14.0
WithContextCall runs the function, which is the same type as a fnx.Operation, with a new context that is canceled after the function exits.
func WithContextCallErr ¶ added in v0.14.0
WithContextCallErr runs the function, which is the same type as a fnx.Worker, with a new context that is canceled after the function exits.
func WithContextCallOk ¶ added in v0.14.0
WithContextCallOk runs the function, with a new context that is canceled after the function exits.
func WithContextDo ¶ added in v0.14.0
WithContextDo runs a function with a new context that is canceled after the function returns.
func WithContextDoErr ¶ added in v0.14.0
WithContextDoErr runs a function, which is the same type as fnx..Future, with a new context that is canceled after the function returns.
func WithContextDoOk ¶ added in v0.14.0
WithContextDoOk runs a function with a new context that is canceled after the function returns.
func WithContextTimeoutCall ¶ added in v0.14.0
WithContextTimeoutCall runs the function, which is the same type as fnx.Operation, with a new context that expires after the specified duration.
func WrapWithContextCall ¶ added in v0.14.0
WrapWithContextCall wraps a context-accepting function into a no-argument function by capturing the provided context.
func WrapWithContextDo ¶ added in v0.14.0
WrapWithContextDo wraps a context-accepting function that returns a value into a no-argument function by capturing the provided context.
func WrapWithContextDoErr ¶ added in v0.14.0
func WrapWithContextDoErr[T any](ctx context.Context, op func(context.Context) (T, error)) func() (T, error)
WrapWithContextDoErr wraps a context-accepting function that returns a value and error into a no-argument function by capturing the provided context.
Types ¶
type Constructors ¶ added in v0.14.0
type Constructors struct{}
The Constructors type serves to namespace constructors of common operations and specializations of generic functions provided by this package.
func (Constructors) ContextChannelWorker ¶ added in v0.14.0
func (Constructors) ContextChannelWorker() Worker
ContextChannelWorker creates a worker function that wraps a context and will--when called--block until the context is done, returning the context's cancellation error. Unless provided with a custom context that can be canceled but does not return an error (which would break many common assumptions regarding contexts,) this worker will always return an error.
func (Constructors) ConvertOperationToWorker ¶ added in v0.14.0
func (Constructors) ConvertOperationToWorker() Converter[Operation, Worker]
ConvertOperationToWorker provides a converter function to produce Worker functions from Operation functions. The errors produced by the worker functions--if any--are the recovered panics from the inner operation.
func (Constructors) ConvertWorkerToOperation ¶ added in v0.14.0
ConvertWorkerToOperation converts Worker functions to Operation function, capturing their errors with the provided error handler.
func (Constructors) ErrorChannelWorker ¶ added in v0.14.0
func (Constructors) ErrorChannelWorker(ch <-chan error) Worker
ErrorChannelWorker constructs a worker from an error channel. The resulting worker blocks until an error is produced in the error channel, the error channel is closed, or the worker's context is canceled. If the channel is closed, the worker will return a nil error, and if the context is canceled, the worker will return a context error. In all other cases the work will propagate the error (or nil) received from the channel.
You can call the resulting worker function more than once.
func (Constructors) OperationHandler ¶ added in v0.14.0
func (Constructors) OperationHandler() Handler[Operation]
OperationHandler constructs a Handler function for running Worker functions. Use with iterators to build worker pools.
func (Constructors) Signal ¶ added in v0.14.0
func (Constructors) Signal() (func(), Worker)
Signal is a wrapper around the common pattern where signal channels are closed to pass termination and blocking notifications between go routines. The constructor returns two functions: a closer operation--func()--and a Worker that waits for the closer to be triggered.
The closer is safe to call multiple times.
func (Constructors) WorkerHandler ¶ added in v0.14.0
func (Constructors) WorkerHandler() Handler[Worker]
WorkerHandler constructs a Handler function for running Worker functions. Use with iterators to build worker pools.
The WorkerHandler provides no panic protection.
type Converter ¶
Converter is a function type that converts T objects int objects of type O.
func MakeConverter ¶
MakeConverter builds a Transform function out of an equivalent function that doesn't take a context or return an error.
func MakeConverterErr ¶
MakeConverterErr constructs a Transform function from an analogous function that does not take a context.
func MakeCovnerterOk ¶
MakeCovnerterOk builds a Transform function from a function that converts between types T and O, but that returns a boolean/check value. When the converter function returns false the transform function returns a ers.ErrCurrentOpSkip error.
func (Converter[T, O]) Convert ¶
Convert uses the converter function to transform a value from one type (T) to another (O).
func (Converter[T, O]) Lock ¶
Lock returns a Transform function that's executed the root function inside of the sope of a mutex.
func (Converter[T, O]) Wait ¶
Wait calls the transform function passing a context that cannot expire.
func (Converter[T, O]) WithLock ¶
WithLock returns a Transform function inside of the scope of the provided mutex.
func (Converter[T, O]) WithRecover ¶
WithRecover returns a Transform function that catches a panic, converts the panic object to an error if needed, and aggregates that with the Transform function's error.
type Future ¶
Future is a function type that describes a fairly common function signature, and provides a collection of methods for improved ergonomics.
func CheckedFuture ¶
CheckedFuture wraps a function object that uses the second ("OK") value to indicate that no more values will be produced. Errors returned from the resulting produce are always either the context cancellation error or io.EOF.
func MakeFuture ¶
MakeFuture constructs a future that wraps a similar function that does not take a context.
func NewFuture ¶
NewFuture returns a future as a convenience function to avoid the extra cast when creating new function objects.
func PtrFuture ¶
PtrFuture uses a function that returns a pointer to a value and converts that into a future that de-references and returns non-nil values of the pointer, and returns EOF for nil values of the pointer.
func StaticFuture ¶
StaticFuture returns a future function that always returns the provided values.
func ValueFuture ¶
ValueFuture returns a future function that always returns the provided value, and a nill error.
func WrapFuture ¶
WrapFuture creates a future for the fn.Future function. The underlying Future's panics are converted to errors.
func (Future[T]) After ¶
After will return a Future that will block until the provided time is in the past, and then execute normally.
func (Future[T]) Check ¶
Check converts the error into a boolean, with true indicating success and false indicating (but not propagating it.).
func (Future[T]) Delay ¶
Delay wraps a Future in a function that will always wait for the specified duration before running.
If the value is negative, then there is always zero delay.
func (Future[T]) Filter ¶
Filter creates a function that passes the output of the future to the filter function, which, if it returns true. is returned to the caller, otherwise the Future returns the zero value of type T and ers.ErrCurrentOpSkip error (e.g. continue), which iterators and other future-consuming functions can respect.
func (Future[T]) Force ¶
Force combines the semantics of Must and Wait as a future: when the future is resolved, the future executes with a context that never expires and panics in the case of an error.
func (Future[T]) Future ¶
Future creates a future function using the context provided and error observer to collect the error.
func (Future[T]) If ¶
If returns a future that will execute the root future only if the cond value is true. Otherwise, If will return the zero value for T and a nil error.
func (Future[T]) Ignore ¶
Ignore creates a future that runs the future and returns the value, ignoring the error.
func (Future[T]) Jitter ¶
Jitter wraps a Future that runs the jitter function (jf) once before every execution of the resulting function, and waits for the resulting duration before running the Future.
If the function produces a negative duration, there is no delay.
func (Future[T]) Join ¶
Join runs the first future until it returns an io.EOF error, and then returns the results of the second future. A future will be re-run until it returns a terminating error (e.g. io.EOF, ers.ErrContainerClosed, or ers.ErrCurrentOpAbort). If the first future returns any other error, that error is returned to the caller and the second future never runs.
When the second function returns a terminating error, or any other error, all successive calls return io.EOF.
func (Future[T]) Limit ¶
Limit runs the future a specified number of times, and caches the result of the last execution and returns that value for any subsequent executions.
func (Future[T]) Lock ¶
Lock creates a future that runs the root mutex as per normal, but under the protection of a mutex so that there's only one execution of the future at a time.
func (Future[T]) Must ¶
Must returns a future that resolves the future returning the constructed value and panicing if the future errors.
func (Future[T]) Once ¶
Once returns a future that only executes ones, and caches the return values, so that subsequent calls to the output future will return the same values.
func (Future[T]) PostHook ¶
PostHook appends a function to the execution of the future. If the function panics it is converted to an error and aggregated with the error of the future.
Useful for calling context.CancelFunc, closers, or incrementing counters as necessary.
func (Future[T]) PreHook ¶
PreHook configures an operation function to run before the returned future. If the pre-hook panics, it is converted to an error which is aggregated with the (potential) error from the future, and returned with the future's output.
func (Future[T]) Resolve ¶
func (pf Future[T]) Resolve() T
Resolve calls the function returned by Force resolving the future. This ignores all errors and provides a the underlying future with a context that will never be canceled.
func (Future[T]) Retry ¶
Retry constructs a worker function that takes runs the underlying future until the error value is nil, or it encounters a terminating error (io.EOF, ers.ErrAbortCurrentOp, or context cancellation.) In all cases, unless the error value is nil (e.g. the retry succeeds)
Context cancellation errors are returned to the caller, other terminating errors are not, with any other errors encountered during retries. ers.ErrCurrentOpSkip is always ignored and not aggregated. All errors are discarded if the retry operation succeeds in the provided number of retries.
Except for ers.ErrCurrentOpSkip, which is ignored, all other errors are aggregated and returned to the caller only if the retry fails. It's possible to return a nil error and a zero value, if the future only returned ers.ErrCurrentOpSkip values.
func (Future[T]) TTL ¶
TTL runs the future only one time per specified interval. The interval must me greater than 0.
func (Future[T]) When ¶
When constructs a future that will call the cond upon every execution, and when true, will run and return the results of the root future. Otherwise When will return the zero value of T and a nil error.
func (Future[T]) WithCancel ¶
func (pf Future[T]) WithCancel() (Future[T], context.CancelFunc)
WithCancel creates a Future and a cancel function which will terminate the context that the root Future is running with. This context isn't canceled *unless* the cancel function is called (or the context passed to the Future is canceled.)
func (Future[T]) WithErrorCheck ¶
WithErrorCheck takes an error future, and checks it before executing the future function. If the error future returns an error (any error), the future propagates that error, rather than running the underying future. Useful for injecting an abort into an existing pipleine or chain.
func (Future[T]) WithErrorFilter ¶
WithErrorFilter passes the error of the root Future function with the erc.Filter.
func (Future[T]) WithLock ¶
WithLock uses the provided mutex to protect the execution of the future.
func (Future[T]) WithLocker ¶
WithLocker uses the provided mutex to protect the execution of the future.
func (Future[T]) WithRecover ¶
WithRecover returns a wrapped future with a panic handler that converts any panic to an error.
func (Future[T]) WithoutErrors ¶
WithoutErrors returns a Future function that wraps the root future and, after running the root future, and makes the error value of the future nil if the error returned is in the error list. The produced value in these cases is almost always the zero value for the type.
type Handler ¶
Handler are generic functions that take an argument (and a context) and return an error.
func FromHandler ¶
FromHandler up-converts a fn.Handler (e.g. a more simple handler function that doesn't return an error or take a context,) into a Handler. The underlying function runs with a panic handler (so fn.Handler panics are converted to errors.)
func JoinHandlers ¶
JoinHandlers takes a collection of Handler functions and merges them into a single chain, eliding any nil Handlers.
func MakeHandler ¶
MakeHandler converts a function with the Handler signature (minus the context) for easy conversion.
func NewHandler ¶
NewHandler returns a Handler Function. This is a convenience function to avoid the extra cast when creating new function objects.
func (Handler[T]) Capture ¶
Capture creates a handler function that like, Handler.Force, passes a background context and ignores the Handlers error.
func (Handler[T]) Check ¶
Check processes the input and returns true when the error is nil, and false when there was an error.
func (Handler[T]) Delay ¶
Delay wraps a Handler in a function that will always wait for the specified duration before running.
If the value is negative, then there is always zero delay.
func (Handler[T]) Filter ¶
Filter returns a wrapping Handler that takes a function a function that only calls the Handler when the filter function returns true, and returns ers.ErrCurrentOpSkip otherwise.
func (Handler[T]) Force ¶
func (pf Handler[T]) Force(in T)
Force processes the input, but discards the error and uses a background context.
func (Handler[T]) Handler ¶
Handler converts a Handler into an observer, handling the error with the error observer and using the provided context.
func (Handler[T]) If ¶
If runs the Handler function if, and only if the condition is true. Otherwise the function does not run and the Handler returns nil.
The resulting Handler can be used more than once.
func (Handler[T]) Jitter ¶
Jitter wraps a Handler that runs the jitter function (jf) once before every execution of the resulting function, and waits for the resulting duration before running the Handler.
If the function produces a negative duration, there is no delay.
func (Handler[T]) Job ¶ added in v0.14.0
Job converts a Handler into a panic-safe worker function suitable for use in worker pool implementations (e.g., wpa.RunWithPool). The returned function catches any panics that occur during execution and converts them to errors.
func (Handler[T]) Join ¶
Join combines a sequence of Handlers on the same input, calling each function in order, as long as there is no error and the context does not expire. Context expiration errors are not propagated.
func (Handler[T]) Must ¶
Must calls the underlying handler, but converts all errors returned by the handler into panics.
func (Handler[T]) Once ¶
Once make a Handler that can only run once. Subsequent calls to the Handler return the cached error of the original run.
func (Handler[T]) PostHook ¶
PostHook produces an amalgamated Handler that runs after the Handler completes. Panics are caught, converted to errors, and aggregated with the Handlers error. The hook operation is unconditionally called after the Handler function (except in the case of a Handler panic.)
func (Handler[T]) PreHook ¶
PreHook creates an amalgamated Handler that runs the operation before the root Handler. If the operation panics that panic is converted to an error and merged with the Handler's error. Use with Operation.Once() to create an "init" function that runs once before a Handler is called the first time.
func (Handler[T]) TTL ¶
TTL returns a Handler that runs once in the specified window, and returns the error from the last run in between this interval. While the executions of the underlying function happen in isolation, in between, the Handler is concurrently accessible.
func (Handler[T]) When ¶
When returns a Handler function that runs if the conditional function returns true, and does not run otherwise. The conditional function is evaluated every time the returned Handler is run.
func (Handler[T]) WithCancel ¶
func (pf Handler[T]) WithCancel() (Handler[T], context.CancelFunc)
WithCancel creates a Handler and a cancel function which will terminate the context that the root proccessor is running with. This context isn't canceled *unless* the cancel function is called (or the context passed to the Handler is canceled.)
func (Handler[T]) WithErrorFilter ¶
WithErrorFilter uses an erc.Filter to process the error respose from the Handler.
func (Handler[T]) WithLock ¶
WithLock wraps the Handler and ensures that the mutex is always held while the root Handler is called.
func (Handler[T]) WithLocker ¶
WithLocker wraps the Handler and ensures that the sync.Locker instance is always held while the root Handler is called.
func (Handler[T]) WithRecover ¶
WithRecover runs the producer, converted all panics into errors.
func (Handler[T]) WithoutErrors ¶
WithoutErrors returns a producer that will convert a non-nil error of the provided types to a nil error.
type Operation ¶
Operation is a type of function object that will block until an operation returns or the context is canceled.
func MakeOperation ¶
func MakeOperation(in func()) Operation
MakeOperation converts a function that takes no arguments into an Operation.
func WaitChannel ¶
WaitChannel converts a channel (typically, a `chan struct{}`) to a Operation. The Operation blocks till it's context is canceled or the channel is either closed or returns one item.
func WaitContext ¶
WaitContext wait's for the context to be canceled before returning. The Operation that's return also respects it's own context. Use this Operation and it's own context to wait for a context to be cacneled with a timeout, for instance.
func (Operation) Add ¶
Add starts a the operation in a goroutine incrementing and decrementing the WaitGroup as appropriate.
func (Operation) After ¶
After provides an operation that will only run if called after the specified clock time. When called after this time, the operation blocks until that time passes (or the context is canceled.)
func (Operation) Background ¶
Background launches the operation in a go routine. There is no panic-safety provided.
func (Operation) Delay ¶
Delay wraps a Operation in a function that will always wait for the specified duration before running.
If the value is negative, then there is always zero delay.
func (Operation) Go ¶
Go provides access to the Go method (e.g. starting this operation in a go routine.) as a method that can be used as an operation itself.
func (Operation) Group ¶
Group makes an operation that runs n copies of the underlying worker, in different go routines. Work does not start until the resulting worker is called.
func (Operation) If ¶
If provides a static version of the When that only runs if the condition is true, and is otherwise a noop.
func (Operation) Interval ¶
Interval runs the operation with a timer that resets to the provided duration. The operation runs immediately, and then the time is reset to the specified interval after the base operation is completed. Which is to say that the runtime of the operation itself is effectively added to the interval.
func (Operation) Jitter ¶
Jitter wraps a Operation that runs the jitter function (jf) once before every execution of the resulting function, and waits for the resulting duration before running the Operation operation.
If the function produces a negative duration, there is no delay.
func (Operation) Job ¶ added in v0.14.0
Job converts an Operation into a panic-safe worker function suitable for use in worker pool implementations (e.g., wpa.RunWithPool). The returned function catches any panics that occur during execution and converts them to errors.
func (Operation) Join ¶
Join combines a sequence of operations, calling the Operations in order as long as the context does not expire. If the context expires, the combined operation aborts early.
func (Operation) Launch ¶
Launch starts the operation in a background go routine and returns an operation which blocks until it's context is canceled or the underlying operation returns.
func (Operation) Limit ¶
Limit returns an operation that will only run the specified number of times. The resulting operation is safe for concurrent use, but operations can run concurrently. All limits less that 1 are treated as 1.
func (Operation) Lock ¶
Lock constructs a mutex that ensure that the underlying operation (when called through the output operation,) only runs within the scope of the lock.
func (Operation) Once ¶
Once produces an operation that will only execute the root operation once, no matter how many times it's called.
func (Operation) PostHook ¶
PostHook unconditionally runs the post-hook operation after the operation returns, as a defer. Use the hook to run cleanup operations. The Operation returned from this method runs both the original hook, and the hook function.
func (Operation) PreHook ¶
PreHook unconditionally runs the hook operation before the underlying operation. Use Operaiton.Once() operations for the hook to initialize resources for use by the operation, or without Once to provide reset semantics. The Operation returned from this method runs both the original hook, and the hook function.
func (Operation) Signal ¶
Signal starts the operation in a go routine, and provides a signal channel which will be closed when the operation is complete.
func (Operation) StartGroup ¶
StartGroup runs n operations, incrementing the WaitGroup to account for the job. Callers must wait on the WaitGroup independently.
func (Operation) TTL ¶
TTL runs an operation, and if the operation is called before the specified duration, the operation is a noop.
func (Operation) Wait ¶
func (wf Operation) Wait()
Wait runs the operation with a background context.
func (Operation) WithCancel ¶
func (wf Operation) WithCancel() (Operation, context.CancelFunc)
WithCancel creates a Operation and a cancel function which will terminate the context that the root Operation is running with. This context isn't canceled *unless* the cancel function is called (or the context passed to the Operation is canceled.)
func (Operation) WithContextHook ¶
WithContextHook returns an wrapped operation where the provided context hook intercepts the context that the worker function is called with. This makes a kind of rudamentary middleware possible.
func (Operation) WithErrorHook ¶
WithErrorHook prodces a Worker function that runs the operation--potentially catching a panic and converting it to an error--and then passes that error to the error handler, before returning the original error. These errors are either context cancilations or error that were converted from panics.
func (Operation) WithLock ¶
WithLock ensures that the underlying operation, when called through the output operation, will holed the mutex while running.
func (Operation) WithRecover ¶
WithRecover converts the Operation into a Worker function that catchers panics and returns them as errors using fun.Check.
type WaitGroup ¶
type WaitGroup struct {
// contains filtered or unexported fields
}
WaitGroup works like sync.WaitGroup, except that the Wait method takes a context (and can be passed as a fnx.Operation). The implementation is exceptionally simple. The only constraint, like sync.WaitGroup, is that you can never modify the value of the internal counter such that it is negative, event transiently. The implementation does not require background resources aside from Wait, which creates a single goroutine that lives for the entire time that Wait is running, but no other background resources are created. Multiple copies of Wait can be safely called at once, and the WaitGroup is reusable more than once.
This implementation is about 50% slower than sync.WaitGroup after informal testing. It provides a little extra flexiblity and introspection, with similar semantics, that may be worth the additional performance hit.
func (*WaitGroup) Add ¶
Add modifies the internal counter. Raises an ErrInvariantViolation error if any modification causes the internal coutner to be less than 0.
func (*WaitGroup) Group ¶
Group returns an operation that, when executed, starts <n> copies of the operation and blocks until all have finished.
func (*WaitGroup) Launch ¶
Launch increments the WaitGroup and starts the operation in a go routine.
func (*WaitGroup) StartGroup ¶
StartGroup starts <n> copies of the operation in separate threads and returns an operation that waits on the wait group.
func (*WaitGroup) Wait ¶
Wait blocks until either the context is canceled or all items have completed.
Wait is passable or usable as a fnx.Operation.
In many cases, callers should not rely on the Wait operation returning after the context expires: If Done() calls are used in situations that respect a context cancellation, aborting the Wait on a context cancellation, particularly when Wait gets a context that has the same lifecycle as the operations its waiting on, the result is that worker routines will leak. Nevertheless, in some situations, when workers may take a long time to respond to a context cancellation, being able to set a second deadline on Waiting may be useful.
Consider using `fnx.Operation(wg.Wait).Block()` if you want blocking semantics with the other features of this WaitGroup implementation.
type Worker ¶
Worker represents a basic function used in worker pools and other similar situations.
func MakeWorker ¶
MakeWorker converts a non-context worker function into a worker for compatibility with tooling.
func (Worker) After ¶
After returns a Worker that blocks until the timestamp provided is in the past. Additional calls to this worker will run immediately. If the timestamp is in the past the resulting worker will run immediately.
func (Worker) Background ¶
Background starts the worker function in a go routine, passing the error to the provided observer function.
func (Worker) Check ¶
Check runs the worker and returns true (ok) if there was no error, and false otherwise.
func (Worker) Delay ¶
Delay wraps a Worker in a function that will always wait for the specified duration before running.
If the value is negative, then there is always zero delay.
func (Worker) Group ¶
Group makes a worker that runs n copies of the underlying worker, in different go routines and aggregates their output. Work does not start until the resulting worker is called.
func (Worker) If ¶
If returns a Worker function that runs only if the condition is true. The error is always nil if the condition is false. If-ed functions may be called more than once, and will run multiple times potentiall.y.
func (Worker) Ignore ¶
Ignore converts the worker into a Operation that discards the error produced by the worker.
func (Worker) Interval ¶
Interval runs the worker with a timer that resets to the provided duration. The worker runs immediately, and then the time is reset to the specified interval after the base worker has. Which is to say that the runtime of the worker's operation is effectively added to the interval.
The interval worker will run until the context is canceled or the worker returns an error.
func (Worker) Jitter ¶
Jitter wraps a Worker that runs the jitter function (jf) once before every execution of the resulting function, and waits for the resulting duration before running the Worker.
If the function produces a negative duration, there is no delay.
func (Worker) Job ¶ added in v0.14.0
Job converts a Worker into a panic-safe worker function suitable for use in worker pool implementations (e.g., wpa.RunWithPool). The returned function catches any panics that occur during execution and converts them to errors.
func (Worker) Join ¶
Join combines a sequence of workers, calling the workers in order, as long as there is no error and the context does not expire. Context expiration errors are not propagated. Does not skip nil workers.
func (Worker) Launch ¶
Launch runs the worker function in a go routine and returns a new fun.Worker which will block for the context to expire or the background worker to completes, which returns the error from the background request.
The underlying worker begins executing before future returns.
func (Worker) Limit ¶
Limit produces a worker than runs exactly n times. Each execution is isolated from every other, but once the limit is exceeded, the result of the *last* worker to execute is cached concurrent access to that value is possible.
func (Worker) Lock ¶
Lock produces a Worker that will be executed within the scope of a (managed) mutex.
func (Worker) Must ¶
Must converts a Worker function into a wait function; however, if the worker produces an error Must converts the error into a panic.
func (Worker) Once ¶
Once wraps the Worker in a function that will execute only once. The return value (error) is cached, and can be accessed many times without re-running the worker.
func (Worker) Operation ¶
Operation converts a worker function into a wait function, passing any error to the handler function.
func (Worker) PostHook ¶
PostHook runs hook operation after the worker function completes. The Worker function's panics are converted to errors and the hook will continue to run.
func (Worker) PreHook ¶
PreHook returns a Worker that runs an operatio unconditionally before running the underlying worker. If the hook function panics it is converted to an error and aggregated with the worker's error.
func (Worker) Retry ¶
Retry constructs a worker function that takes runs the underlying worker until the return value is nil, or it encounters a terminating error (io.EOF, ers.ErrAbortCurrentOp, or context cancellation.) Context cancellation errors are returned to the caller with any other errors encountered in previous retries, other terminating errors are not. All errors are discarded if the retry operation succeeds in the provided number of retries.
Except for ers.ErrCurrentOpSkip, which is ignored, all other errors are aggregated and returned to the caller only if the retry fails.
func (Worker) Signal ¶
Signal runs the worker function in a background goroutine and returns the error in an error channel, that returns when the worker function returns. If Signal is called with a canceled context the worker is still executed (with that context.)
A value, possibly nil, is always sent through the channel. Panics are not caught or handled.
func (Worker) StartGroup ¶
StartGroup starts n copies of the worker operation and returns a future/worker that returns the aggregated errors from all workers
The operation is fundamentally continue-on-error. To get abort-on-error semantics, use the Filter() method on the input worker, that cancels the context on when it sees an error.
func (Worker) TTL ¶
TTL produces a worker that will only run once during every specified duration, when called more than once. During the interval between calls, the previous error is returned. While each execution of the root worker is protected by a mutex, the resulting worker can be used in parallel during the intervals between calls.
func (Worker) When ¶
When wraps a Worker function that will only run if the condition function returns true. If the condition is false the worker does not execute. The condition function is called in between every operation.
When worker functions may be called more than once, and will run multiple times potentially.
func (Worker) While ¶
While runs the Worker in a continuous while loop, returning only if the underlying worker returns an error or if the context is cancled.
func (Worker) WithCancel ¶
func (wf Worker) WithCancel() (Worker, context.CancelFunc)
WithCancel creates a Worker and a cancel function which will terminate the context that the root Worker is running with. This context isn't canceled *unless* the cancel function is called (or the context passed to the Worker is canceled.)
func (Worker) WithContextHook ¶
WithContextHook wraps the worker function, and passes the context received by that function through the hook function before calling the underlying worker.
func (Worker) WithErrorFilter ¶
WithErrorFilter wraps the worker with a Worker that passes the output of the root Worker's error and returns the output of the filter.
The ers package provides a number of filter implementations but any function in the following form works:
func(error) error
func (Worker) WithErrorHook ¶
WithErrorHook runs the worker, potentially catching a panic and converting that to an error. The output of the worker function is passed to the error hook (regardless of the test's error status) before returning the original error. Panics in the error handler are not handled.
func (Worker) WithLock ¶
WithLock produces a Worker that will be executed within the scope of the provided mutex.
func (Worker) WithRecover ¶
WithRecover produces a worker function that converts the worker function's panics to errors.
func (Worker) WithoutErrors ¶
WithoutErrors returns a worker that will return nil if the error returned by the worker is one of the errors passed to WithoutErrors.