Documentation ¶
Overview ¶
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.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrFutureCanceled = errors.New("future canceled")
ErrFutureCanceled signals that futures in canceled by a call to `f.Cancel()`
Functions ¶
This section is empty.
Types ¶
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.
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.
func NewSelectable ¶ added in v1.0.19
func NewSelectable() *Selectable
NewSelectable returns new selectable future.
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.