futures

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2021 License: Apache-2.0 Imports: 3 Imported by: 3

Documentation

Overview

This module implements a simple Async Futures for golang Usage: f := NewAsyncFuture(childCtx, func(ctx2 context.Context) (interface{}, error) { can do large async / non blocking work

	return ...
}

f.Ready() // can be checked for completion f.Get() .. will block till the given sub-routine returns

Index

Constants

This section is empty.

Variables

View Source
var ErrAsyncFutureCanceled = fmt.Errorf("async future was canceled")

ErrAsyncFutureCanceled is returned when the async future is cancelled by invoking the cancel function on the context

Functions

This section is empty.

Types

type AsyncFuture

type AsyncFuture struct {
	sync.Mutex
	// contains filtered or unexported fields
}

An asynchronously completing future

func NewAsyncFuture

func NewAsyncFuture(ctx context.Context, closure func(context.Context) (interface{}, error)) *AsyncFuture

Creates a new Async future, that will call the method `closure` and return the results from the execution of this method

func (*AsyncFuture) Get

func (f *AsyncFuture) Get(ctx context.Context) (interface{}, error)

Returns results (interface{} or an error) OR blocks till the results are available. If context is cancelled while waiting for results, an ErrAsyncFutureCanceled is returned

func (*AsyncFuture) Ready

func (f *AsyncFuture) Ready() bool

returns whether the future is completed

type Future

type Future interface {
	// Returns true if the Future is ready and either a value or error is available. Once Ready returns True, Get should return immediately
	Ready() bool
	// Get is a potentially blocking call, that returns the asynchronously computed value or an error
	// If Get is called before Ready() returns True, then it will block till the future has been completed
	Get(ctx context.Context) (interface{}, error)
}

Provides a Future API for asynchronous completion of tasks

type SyncFuture

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

This is a synchronous future, where the values are available immediately on construction. This is used to maintain a synonymous API with both Async and Sync tasks

func NewSyncFuture

func NewSyncFuture(val interface{}, err error) *SyncFuture

Creates a new "completed" future that matches the async computation api

func (*SyncFuture) Get

func (s *SyncFuture) Get(_ context.Context) (interface{}, error)

returns the previously provided value / error

func (SyncFuture) Ready

func (s SyncFuture) Ready() bool

Always returns true

Jump to

Keyboard shortcuts

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