Documentation
¶
Overview ¶
Package parallelisation defines a module for `Concurrency`
Index ¶
- func CloseAll(cs ...io.Closer) error
- func CloseAllFunc(cs ...CloseFunc) error
- func CloseAllWithContext(ctx context.Context, cs ...io.Closer) error
- func DetermineContextError(ctx context.Context) error
- func Parallelise(argList interface{}, action func(arg interface{}) (interface{}, error), ...) (results interface{}, err error)
- func RunActionWithParallelCheck(ctx context.Context, action func(ctx context.Context) error, ...) error
- func RunActionWithTimeout(blockingAction func(stop chan bool) error, timeout time.Duration) (err error)
- func RunActionWithTimeoutAndCancelStore(ctx context.Context, timeout time.Duration, store *CancelFunctionStore, ...) error
- func RunActionWithTimeoutAndContext(ctx context.Context, timeout time.Duration, ...) error
- func SafeSchedule(ctx context.Context, period time.Duration, offset time.Duration, ...)
- func SafeScheduleAfter(ctx context.Context, offset time.Duration, f func(context.Context, time.Time))
- func Schedule(ctx context.Context, period time.Duration, offset time.Duration, ...)
- func ScheduleAfter(ctx context.Context, offset time.Duration, f func(time.Time))
- func SleepWithContext(ctx context.Context, delay time.Duration)
- func SleepWithInterruption(stop chan bool, delay time.Duration)
- func WaitUntil(ctx context.Context, evalCondition func(ctx2 context.Context) (bool, error), ...) error
- func WaitWithContext(ctx context.Context, wg IWaiter) (err error)
- func WaitWithContextAndError(ctx context.Context, wg IErrorWaiter) (err error)
- func WorkerPool[InputType, ResultType any](ctx context.Context, numWorkers int, jobs []InputType, ...) (results []ResultType, err error)
- type CancelFunctionStore
- type CloseFunc
- type CloseFunctionStore
- type CloserStore
- type IErrorWaiter
- type IWaiter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloseAll ¶ added in v1.106.0
CloseAll calls concurrently Close on all io.Closer implementations passed as arguments and returns the first error encountered
func CloseAllFunc ¶ added in v1.106.0
CloseAllFunc calls concurrently all Close functions passed as arguments and returns the first error encountered
func CloseAllWithContext ¶ added in v1.106.0
CloseAllWithContext is similar to CloseAll but can be controlled using a context.
func DetermineContextError ¶
DetermineContextError determines what the context error is if any.
func Parallelise ¶
func Parallelise(argList interface{}, action func(arg interface{}) (interface{}, error), resultType reflect.Type) (results interface{}, err error)
Parallelise parallelises an action over as many goroutines as specified by the argList and retrieves all the results when all the goroutines are done.
func RunActionWithParallelCheck ¶
func RunActionWithParallelCheck(ctx context.Context, action func(ctx context.Context) error, checkAction func(ctx context.Context) bool, checkPeriod time.Duration) error
RunActionWithParallelCheck runs an action with a check in parallel The function performing the check should return true if the check was favourable; false otherwise. If the check did not have the expected result and the whole function would be cancelled.
func RunActionWithTimeout ¶
func RunActionWithTimeout(blockingAction func(stop chan bool) error, timeout time.Duration) (err error)
RunActionWithTimeout runs an action with timeout
func RunActionWithTimeoutAndCancelStore ¶
func RunActionWithTimeoutAndCancelStore(ctx context.Context, timeout time.Duration, store *CancelFunctionStore, blockingAction func(context.Context) error) error
RunActionWithTimeoutAndCancelStore runs an action with timeout The cancel store is used just to register the cancel function so that it can be called on Cancel.
func RunActionWithTimeoutAndContext ¶
func RunActionWithTimeoutAndContext(ctx context.Context, timeout time.Duration, blockingAction func(context.Context) error) error
RunActionWithTimeoutAndContext runs an action with timeout blockingAction's context will be cancelled on exit.
func SafeSchedule ¶ added in v1.40.0
func SafeSchedule(ctx context.Context, period time.Duration, offset time.Duration, f func(context.Context, time.Time))
SafeSchedule calls function `f` regularly with a `period` and an `offset`, similarly to Schedule but with context control.
func SafeScheduleAfter ¶ added in v1.40.0
func SafeScheduleAfter(ctx context.Context, offset time.Duration, f func(context.Context, time.Time))
SafeScheduleAfter calls once function `f` after `offset` similarly to ScheduleAfter but stops the function is controlled by the context
func ScheduleAfter ¶ added in v1.1.0
ScheduleAfter calls once function `f` after `offset`
func SleepWithContext ¶
SleepWithContext performs an interruptable sleep Similar to time.Sleep() but also responding to context cancellation instead of blocking for the whole length of time.
func SleepWithInterruption ¶
SleepWithInterruption performs an interruptable sleep Similar to time.Sleep() but also interrupting when requested instead of blocking for the whole length of time.
func WaitUntil ¶ added in v1.100.0
func WaitUntil(ctx context.Context, evalCondition func(ctx2 context.Context) (bool, error), pauseBetweenEvaluations time.Duration) error
WaitUntil waits for a condition evaluated by evalCondition to be verified
func WaitWithContext ¶ added in v1.101.0
func WaitWithContextAndError ¶ added in v1.101.0
func WaitWithContextAndError(ctx context.Context, wg IErrorWaiter) (err error)
func WorkerPool ¶ added in v1.108.0
func WorkerPool[InputType, ResultType any](ctx context.Context, numWorkers int, jobs []InputType, f func(context.Context, InputType) (ResultType, bool, error)) (results []ResultType, err error)
WorkerPool parallelises an action using a worker pool of the size provided by numWorkers and retrieves all the results when all the actions have completed. It is similar to Parallelise but it uses generics instead of reflection and allows you to control the pool size
Types ¶
type CancelFunctionStore ¶
type CancelFunctionStore struct {
// contains filtered or unexported fields
}
func NewCancelFunctionsStore ¶
func NewCancelFunctionsStore() *CancelFunctionStore
NewCancelFunctionsStore creates a store for cancel functions.
func (*CancelFunctionStore) Cancel ¶
func (s *CancelFunctionStore) Cancel()
Cancel will execute the cancel functions in the store. Any errors will be ignored and Execute() is recommended if you need to know if a cancellation failed
func (*CancelFunctionStore) Len ¶
func (s *CancelFunctionStore) Len() int
func (*CancelFunctionStore) RegisterCancelFunction ¶
func (s *CancelFunctionStore) RegisterCancelFunction(cancel ...context.CancelFunc)
func (*CancelFunctionStore) RegisterFunction ¶ added in v1.106.0
func (s *CancelFunctionStore) RegisterFunction(function ...T)
type CloseFunctionStore ¶ added in v1.106.0
type CloseFunctionStore struct {
// contains filtered or unexported fields
}
func NewCloseFunctionStoreStore ¶ added in v1.106.0
func NewCloseFunctionStoreStore(stopOnFirstError bool) *CloseFunctionStore
NewCloseFunctionStoreStore returns a store closing functions which will all be called concurrently on Close(). The first error received will be returned.
func (*CloseFunctionStore) Close ¶ added in v1.106.0
func (s *CloseFunctionStore) Close() error
func (*CloseFunctionStore) Len ¶ added in v1.106.0
func (s *CloseFunctionStore) Len() int
func (*CloseFunctionStore) RegisterCloseFunction ¶ added in v1.106.0
func (s *CloseFunctionStore) RegisterCloseFunction(closerObj ...CloseFunc)
func (*CloseFunctionStore) RegisterFunction ¶ added in v1.106.0
func (s *CloseFunctionStore) RegisterFunction(function ...T)
type CloserStore ¶ added in v1.106.0
type CloserStore struct {
// contains filtered or unexported fields
}
func NewCloserStore ¶ added in v1.106.0
func NewCloserStore(stopOnFirstError bool) *CloserStore
NewCloserStore returns a store of io.Closer object which will all be closed concurrently on Close(). The first error received will be returned
func (*CloserStore) Close ¶ added in v1.106.0
func (s *CloserStore) Close() error
func (*CloserStore) Len ¶ added in v1.106.0
func (s *CloserStore) Len() int
func (*CloserStore) RegisterCloser ¶ added in v1.106.0
func (s *CloserStore) RegisterCloser(closerObj ...io.Closer)
func (*CloserStore) RegisterFunction ¶ added in v1.106.0
func (s *CloserStore) RegisterFunction(function ...T)
type IErrorWaiter ¶ added in v1.101.0
type IErrorWaiter interface {
Wait() error
}
IErrorWaiter can be used to wait on errgroups and similar types where Wait() returns an error This is used to support use in the WaitWithContextAndError function to wait but listen to contexts