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

    View Source
    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.

          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

              func (f *Future) HasResult() bool

                HasResult will return true iff the result exists

                type Selectable

                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

                  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

                    func (f *Selectable) Cancel()

                      Cancel is alias for SetError(ErrFutureCanceled)

                      func (*Selectable) Fill

                      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

                        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

                          func (f *Selectable) SetError(e error)

                            SetError is alias for Fill(nil, e)

                            func (*Selectable) SetValue

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

                              SetValue is alias for Fill(v, nil)

                              func (*Selectable) WaitChan

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

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