Version: v1.0.53 Latest Latest

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

Go to latest
Published: Mar 29, 2021 License: Apache-2.0 Imports: 5 Imported by: 134



Package futures is useful for broadcasting an identical message to a multitude of listeners as opposed to channels which will choose a listener at random if multiple listeners are listening to the same channel. The future will also cache the result so any future interest will be immediately returned to the consumer.



This section is empty.


View Source
var ErrFutureCanceled = errors.New("future canceled")

ErrFutureCanceled signals that futures in canceled by a call to `f.Cancel()`


This section is empty.


type Completer

type Completer <-chan interface{}

Completer is a channel that the future expects to receive a result on. The future only receives on this channel.

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 getresult 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(completer Completer, timeout time.Duration) *Future

New is the constructor to generate a new future. Pass the completed item to the toComplete channel and any listeners will get notified. If timeout is hit before toComplete is called, any listeners will get passed an error.

func (*Future) GetResult

func (f *Future) GetResult() (interface{}, error)

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

func (*Future) HasResult added in v1.0.30

func (f *Future) HasResult() bool

HasResult will return true iff the result exists

type Selectable added in v1.0.19

type Selectable struct {
	// contains filtered or unexported fields

Selectable is a future with channel exposed for external `select`. Many simultaneous listeners may wait for result either with `f.Value()` or by selecting/fetching from `f.WaitChan()`, which is closed when future fulfilled. Selectable contains sync.Mutex, so it is not movable/copyable.

func NewSelectable added in v1.0.19

func NewSelectable() *Selectable

NewSelectable returns new selectable future. Note: this method is for backward compatibility. You may allocate it directly on stack or embedding into larger structure

func (*Selectable) Cancel added in v1.0.19

func (f *Selectable) Cancel()

Cancel is alias for SetError(ErrFutureCanceled)

func (*Selectable) Fill added in v1.0.19

func (f *Selectable) Fill(v interface{}, e error) error

Fill sets value for future, if it were not already fullfilled Returns error, if it were already set to future.

func (*Selectable) GetResult added in v1.0.19

func (f *Selectable) GetResult() (interface{}, error)

GetResult waits for future to be fullfilled and returns value or error, whatever is set first

func (*Selectable) SetError added in v1.0.19

func (f *Selectable) SetError(e error)

SetError is alias for Fill(nil, e)

func (*Selectable) SetValue added in v1.0.19

func (f *Selectable) SetValue(v interface{}) error

SetValue is alias for Fill(v, nil)

func (*Selectable) WaitChan added in v1.0.19

func (f *Selectable) WaitChan() <-chan struct{}

WaitChan returns channel, which is closed when future is fullfilled.

Jump to

Keyboard shortcuts

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