syncutil

package
v0.0.0-...-b8313d7 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTaskPanic = fmt.Errorf("task panic occurred")

Functions

func Each

func Each(ctx context.Context, n int, actor func(context.Context, int))

Each starts n goroutines and runs actor inside it. It returns when all actors return.

func Every

func Every(ctx context.Context, n int, actor func(context.Context, int) error) error

Every starts n goroutines and runs actor inside each. If some actor returns error it stops processing and cancel other actions by canceling their context argument. It returns first error occured.

Types

type GoerFn

type GoerFn func(context.Context, func()) error

GoerFn represents function that starts a goroutine which executes a given task.

type Multitask

type Multitask struct {
	// ContinueOnError disables cancellation of sub context passed to
	// action callbackwhen it is not possible to start all N goroutines to
	// prepare an action.
	ContinueOnError bool

	// Goer starts a goroutine which executes a given task.
	// It is useful when client using some pool of goroutines.
	//
	// If nil, then default `go` is used and context is ignored.
	//
	// Non-nil error from Goer means that some resources are temporarily
	// unavailable and given task will not be executed.
	Goer GoerFn
}

Multitask helps to run N tasks in parallel.

func (Multitask) Do

func (m Multitask) Do(ctx context.Context, n int, actor func(context.Context, int) bool) (err error)

Do executes actor function N times probably in parallel. It blocks until all actions are done or become canceled.

type TaskGroup

type TaskGroup struct {
	// N is a maximum number of tasks TaskGroup can allow to execute.
	// If N is zero then TaskGroup with value 1 is used by default.
	N int

	// Goer starts a goroutine which executes a given task.
	// It is useful when client using some pool of goroutines.
	//
	// If nil, then default `go` is used and context is ignored.
	//
	// Non-nil error from Goer means that some resources are temporarily
	// unavailable and given task will not be executed.
	//
	// Note that for goroutine pool implementations it is required for pool to
	// have at least capacity of N goroutines. In other way deadlock may occur.
	Goer GoerFn
	// contains filtered or unexported fields
}

TaskGroup helps to control execution flow of repeatable tasks. It is intended to execute at most N tasks at one time.

func (*TaskGroup) Cancel

func (t *TaskGroup) Cancel()

Cancel cancels context of all currently running tasks. Further Do() calls will not be blocked on waiting for exit of previous tasks.

func (*TaskGroup) Do

func (t *TaskGroup) Do(ctx context.Context, n int, task func(context.Context, int) error) []<-chan error

Do executes given function task in separate goroutine n minus <currently running tasks number> times. It returns slice of n channels which fulfillment means the end of appropriate task execution.

That is, for m already running tasks Do(n, n < m) will return n channels referring to a previously spawned task goroutines.

All currenlty executing tasks can be signaled to cancel by calling TaskGroup's Cancel() method.

nolint:gocognit

type TaskRunner

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

TaskRunner runs only one task. Caller can subscribe to current task or in case if no task is running initiate a new one via Do method.

Check MAILX-1585 for details.

func (*TaskRunner) Cancel

func (t *TaskRunner) Cancel()

func (*TaskRunner) Do

func (t *TaskRunner) Do(ctx context.Context, task func(context.Context) error) <-chan error

Do returns channel from which the result of the current task will be returned.

In case if task is not running, it creates one.

type Throttle

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

Throttle helps to run a function only a once per given time period.

func NewThrottle

func NewThrottle(p time.Duration) *Throttle

NewThrottle creates new Throttle with given period.

func (*Throttle) Next

func (t *Throttle) Next() (ok bool)

Do executes fn if Throttle's last execution time is far enough in the past.

func (*Throttle) Reset

func (t *Throttle) Reset()

Reset resets the throttle timeout such that next Next() will return true.

func (*Throttle) Set

func (t *Throttle) Set(p time.Time)

Set sets throttle point such that Next() will return true only after given moment p.

Jump to

Keyboard shortcuts

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