future

package
v2.11.1 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package future Most of the code here has been copied from (https://github.com/Workiva/go-datastructures) with a slight modification

Index

Constants

This section is empty.

Variables

View Source
var ErrTimeout = func(duration time.Duration) error { return fmt.Errorf(`timeout after %f seconds`, duration.Seconds()) }

ErrTimeout is returned when the future has timed out

Functions

This section is empty.

Types

type Future

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

Future represents an object that can be used to perform asynchronous tasks. The constructor of the future will complete it, and listeners will block on Result until a result is received. This is different from a channel in that the future is only completed once, and anyone listening on the future will get the result, regardless of the number of listeners.

func New

func New(task Task, timeout time.Duration) *Future

New creates an instance of Future that will time out when the timeout is hit

func NewWithContext

func NewWithContext(ctx context.Context, task Task) *Future

NewWithContext creates an instance of Future with a context. The future will time out when the given context is canceled before the response is received

func (*Future) HasResult

func (f *Future) HasResult() bool

HasResult will return true iff the result exists

func (*Future) Result

func (f *Future) Result() *Result

Result will immediately fetch the result if it exists or wait on the result until it is ready.

type Result

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

Result defines the future result. It holds both the success and error result

func (*Result) Failure

func (x *Result) Failure() error

Failure returns the future error result

func (*Result) Success

func (x *Result) Success() proto.Message

Success returns the future success result

type Task

type Task <-chan proto.Message

Task defines the successful outcome of a long-running task

Jump to

Keyboard shortcuts

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