task

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2025 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsRetryableError

func IsRetryableError(err error) bool

IsRetryableError checks if the error is retryable.

func NewDeadlineError

func NewDeadlineError(deadline time.Duration, lastErr error) error

NewDeadlineRetryError creates DeadlineRetryError instance.

func NewMaxRetryError

func NewMaxRetryError(maxAttempts int, lastErr error) error

NewMaxRetryError creates MaxRetryError instance.

func NewRetryableError

func NewRetryableError(err error) error

NewRetryableError wraps an error and makes it retryable.

Types

type Backoff

type Backoff interface {
	Next() time.Duration
}

type DeadlineRetryError

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

DeadlineRetryError is returned when RetryWithDeadline could not complete successfully within given deadline.

func (*DeadlineRetryError) Cause

func (err *DeadlineRetryError) Cause() error

Cause returns the last error before the deadline is exceeded.

func (*DeadlineRetryError) Error

func (err *DeadlineRetryError) Error() string

Error returns the error message.

type Group

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

Group is used to wait for a group of tasks to finish.

It will stop all the tasks on the first task failure, and the Wait() method will return only the first encountered error.

func NewGroup

func NewGroup() *Group

NewGroup creates new task group instance.

func (*Group) Cancel

func (g *Group) Cancel()

Cancel cancels all the tasks.

func (*Group) Go

func (g *Group) Go(tasks ...TaskFunc)

Go runs tasks in the group.

Every task is run in new goroutine. When a task returns an error, all the tasks in the group are canceled.

Typically one should schedule tasks with the Group.Go() method and then wait for all of them to finish by using the Group.Wait() method.

func (*Group) Wait

func (g *Group) Wait(ctx context.Context) error

Wait until all tasks are stopped. Returns the first encountered error if any. If the context is done all tasks are canceled and the context error is returned.

type MaxRetryExceedError

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

MaxRetryExceedError is returned when RetryUntil could not complete successfully for a given maxAttempts retries.

func (*MaxRetryExceedError) Cause

func (err *MaxRetryExceedError) Cause() error

Cause returns the last error before the max retry attempts is exceeded.

func (*MaxRetryExceedError) Error

func (err *MaxRetryExceedError) Error() string

Error returns the error message.

type RetryableError

type RetryableError interface {
	error
	IsRetryable() bool
}

RetryableError error signify that the task can be retried.

type TaskFunc

type TaskFunc func(ctx context.Context) error

TaskFunc specify the functions that are run by the task group.

func NewTaskFunc

func NewTaskFunc(fn TaskFunc, decorators ...TaskFuncDecorator) TaskFunc

NewTaskFunc creates a TaskFunc decorated with the provided decorators.

func Retry

func Retry(retryInterval time.Duration, retryFunc TaskFunc) TaskFunc

Retry retries a task until it returns no error or the returned error is non retriable. An error is retriable when it implements the RetryableError interface and its IsRetryable method returns true. The retryInterval specify how much time to wait between every retry.

func RetryUntil

func RetryUntil(maxAttempts int, retryInterval time.Duration, retryFunc TaskFunc) TaskFunc

RetryUntil retries a task maxAttempts times until it returns no error or the returned error is non retriable. An error is retriable when it implements the RetryableError interface and its IsRetryable method returns true. The retryInterval specify how much time to wait between every retry. If the task do not complete for maxAttempts retries, RetryUntil will return MaxRetryExceedError.

NOTE: when the cancel channel is closed, RetryUntil will not return an error, even if the retryFunc had failed couple of times so far.

func RetryWithBackoff

func RetryWithBackoff(maxAttempts int, backoff Backoff, retryFunc TaskFunc) TaskFunc

RetryWithBackoff retries a task maxAttempts times with exponential backoff until it returns no error or the returned error is non retriable.

An error is retriable when it implements the RetryableError interface and its IsRetryable method returns true.

If the task do not complete for maxAttempts retries, RetryWithBackoff will return MaxRetryExceedError.

NOTE: when the cancel channel is closed, RetryWithBackoff will not return an error, even if the retryFunc had failed couple of times so far.

If maxAttempts is -1, it will retry infinitely.

func RetryWithDeadline

func RetryWithDeadline(
	timeoutDeadline time.Duration,
	retryInterval time.Duration,
	retryFunc TaskFunc,
) TaskFunc

RetryWithDeadline retries a task until it returns no error, or the returned error is non retriable, or timeoutDeadline is exceeded. An error is retriable when it implements the RetryableError interface and its IsRetryable method returns true. The retryInterval specify how much time to wait between every retry. If the task do not complete and timeoutDeadline is exceeded, RetryWithDeadline will return DeadlineRetryError.

NOTE: when the cancel channel is closed, RetryWithDeadline will not return an error, even if the retryFunc had failed couple of times so far.

func Task

func Task(t TaskInterface) TaskFunc

Task adapts a TaskInterface to TaskFunc required by the task.Group to run tasks.

type TaskFuncDecorator

type TaskFuncDecorator func(fn TaskFunc) TaskFunc

type TaskInterface

type TaskInterface interface {
	Run(ctx context.Context) error
}

Jump to

Keyboard shortcuts

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