Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Execute ¶
func Execute[R any](fn func() (*R, error), cancelFunc context.CancelFunc, timeout time.Duration) (*R, error)
Execute initiates the execution of the provided operation and waits for its completion up to the given timeout duration. If the operation completes within the timeout, the function returns the result of the operation or any error that occurred. If the timeout elapses before the operation completes, the function returns a TimeoutError and the operation is canceled using the provided cancelFunc.
Possible errors returned: - TimeoutError: Indicates that the operation did not complete within the specified timeout duration. - CancelledOperationError: Indicates that the operation was canceled before it could complete. - Other generic errors: Represents any other error that might occur during the operation.
It's important for callers to handle these specific error cases, especially if there's a need to distinguish between a genuine operation failure and a timeout or cancellation.
func IsTimeoutError ¶
Types ¶
type AsyncTask ¶
type AsyncTask[R any] struct { // contains filtered or unexported fields }
AsyncTask represents an asynchronous task that can be executed in the background. It provides mechanisms to start, wait for completion, and cancel the task. Additionally, it allows for retrying the task a specified number of times in case of failure.
func NewAsyncTask ¶
func NewAsyncTask[R any](fn func() (*R, error), cancelFunc context.CancelFunc, retries int) *AsyncTask[R]
NewAsyncTask creates a new instance of AsyncTask.
func (*AsyncTask[R]) Cancel ¶
func (t *AsyncTask[R]) Cancel()
Cancel attempts to cancel the execution of the task. It invokes the provided cancellation function and marks the task as canceled.
func (*AsyncTask[R]) ExecuteAsync ¶
func (t *AsyncTask[R]) ExecuteAsync()
ExecuteAsync initiates the execution of the task in a separate goroutine. If the task fails, it will be retried up to the specified number of times. Once the task completes or fails after all retries, the result or error is stored internally.
type CancelledOperationError ¶
type CancelledOperationError struct{}
CancelledOperationError represents an error that occurs when an operation is canceled before it completes. This can be due to user intervention or a system decision. It's useful in scenarios where long-running tasks can be canceled by external triggers or user input, such as canceling an async task or an HTTP request.
func NewCancelledOperationError ¶
func NewCancelledOperationError() *CancelledOperationError
func (CancelledOperationError) Error ¶
func (e CancelledOperationError) Error() string
type Future ¶
type Future[T any] interface { Wait() (*T, error) TimeWait(timeout time.Duration) (*T, error) Cancel() bool Complete(t *T) bool Fail(err error) bool }
Future is an interface that represents a value or an error that will be available in the future. It provides mechanisms to wait for the value, set the value or error, and cancel the operation.
type FutureImpl ¶
type FutureImpl[T any] struct { // contains filtered or unexported fields }
FutureImpl is an implementation of the Future interface. It uses condition variables to allow waiting for the value or error to be set.
func NewFuture ¶
func NewFuture[T any]() *FutureImpl[T]
func (*FutureImpl[T]) Cancel ¶
func (f *FutureImpl[T]) Cancel() bool
Cancel attempts to cancel the FutureImpl. It sets an internal cancellation error and marks the FutureImpl as completed. It then wakes up all goroutines waiting on the FutureImpl. It returns false if the FutureImpl is already completed.
func (*FutureImpl[T]) Complete ¶
func (f *FutureImpl[T]) Complete(t *T) bool
Complete sets the value for the FutureImpl and marks it as completed. It then wakes up all goroutines waiting on the FutureImpl. It returns false if the FutureImpl is already completed.
func (*FutureImpl[T]) Fail ¶
func (f *FutureImpl[T]) Fail(err error) bool
Fail sets an error for the FutureImpl and marks it as completed. It then wakes up all goroutines waiting on the FutureImpl. It returns false if the FutureImpl is already completed.
func (*FutureImpl[T]) TimeWait ¶
func (f *FutureImpl[T]) TimeWait(timeout time.Duration) (*T, error)
TimeWait blocks the calling goroutine for the specified timeout duration or until the FutureImpl completes, whichever comes first. If the FutureImpl completes within the timeout, it returns the value or any error that occurred during the operation. If the timeout elapses before the FutureImpl completes, it returns a TimeoutError.
Possible errors returned: - TimeoutError: Indicates that the operation did not complete within the specified timeout duration. - CancelledOperationError: Indicates that the operation was canceled before it could complete. - Other generic errors: Represents any other error that might occur during the operation.
It's important for callers to handle these specific error cases, especially if there's a need to distinguish between a genuine operation failure and a timeout or cancellation.
func (*FutureImpl[T]) Wait ¶
func (f *FutureImpl[T]) Wait() (*T, error)
Wait blocks until the FutureImpl completes. It then returns the value or error stored in the FutureImpl.
Possible errors returned: - TimeoutError: Indicates that the operation did not complete within the specified timeout duration. - CancelledOperationError: Indicates that the operation was canceled before it could complete. - Other generic errors: Represents any other error that might occur during the operation.
type TimeoutError ¶
TimeoutError is used to indicate that a specific operation has exceeded the allocated time to complete. It provides information about the duration after which the timeout occurred.It's particularly useful in scenarios where you have operations that can block for an extended period, and you want to ensure they complete within a given timeframe. For example, waiting for a response from a remote service or waiting for a Future to complete.
func NewTimeoutError ¶
func NewTimeoutError(duration time.Duration) *TimeoutError
func (TimeoutError) Error ¶
func (e TimeoutError) Error() string