race

package
v1.79.0-mod Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package race contains a helper to "race" two functions, returning the first successful result. It also allows explicitly triggering the (possibly-waiting) second function when the first function returns an error or indicates that it should be retried.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Func

type Func[T any] func(context.Context) (T, error)

Func is the signature of a function to be called.

type Race

type Race[T any] struct {
	// contains filtered or unexported fields
}

Race allows running two functions concurrently and returning the first non-error result returned.

func New

func New[T any](d time.Duration, func1, func2 Func[T]) *Race[T]

New creates a new Race that, when Start is called, will immediately call func1 to obtain a result. After the timeout d or if triggered by an error response from func1, func2 will be called.

func (*Race[T]) Start

func (rh *Race[T]) Start(ctx context.Context) (T, error)

Start will start the "race" process, returning the first non-error result or the errors that occurred when calling func1 and/or func2.

Jump to

Keyboard shortcuts

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