async

package module
Version: v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2021 License: MIT Imports: 6 Imported by: 5

README


Async provides synchronization and asynchronous computation utilities for Go.

The implemented patterns were taken from Scala and Java.

Overview

  • Future - A placeholder object for a value that may not yet exist.
  • Promise - While futures are defined as a type of read-only placeholder object created for a result which doesn’t yet exist, a promise can be thought of as a writable, single-assignment container, which completes a future.
  • Reentrant Lock - Mutex that allows goroutines to enter into the lock on a resource more than once.
  • Optimistic Lock - Mutex that allows optimistic reading. Could be retried or switched to RLock in case of failure. Significantly improves performance in case of frequent reads and short writes. See benchmarks.

Go 1.18 Generic prototypes

  • Task - A data type for controlling possibly lazy and asynchronous computations.

Examples

Can be found in the examples directory/tests.

License

Licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoroutineID

func GoroutineID() (uint, error)

GoroutineID returns the current goroutine id.

Types

type Future

type Future interface {

	// Map creates a new Future by applying a function to the successful result of this Future.
	Map(func(interface{}) (interface{}, error)) Future

	// FlatMap creates a new Future by applying a function to the successful result of
	// this Future.
	FlatMap(func(interface{}) (Future, error)) Future

	// Get blocks until the Future is completed and returns either a result or an error.
	Get() (interface{}, error)

	// Recover handles any error that this Future might contain using a resolver function.
	Recover(func() (interface{}, error)) Future

	// RecoverWith handles any error that this Future might contain using another Future.
	RecoverWith(Future) Future
	// contains filtered or unexported methods
}

Future represents a value which may or may not currently be available, but will be available at some point, or an error if that value could not be made available.

func FutureFirstCompletedOf

func FutureFirstCompletedOf(futures ...Future) Future

FutureFirstCompletedOf asynchronously returns a new Future to the result of the first Future in the list that is completed. This means no matter if it is completed as a success or as a failure.

func FutureSeq

func FutureSeq(futures []Future) Future

FutureSeq reduces many Futures into a single Future

func FutureTimer

func FutureTimer(d time.Duration) Future

FutureTimer returns Future that will have been resolved after given duration useful for FutureFirstCompletedOf for timeout purposes

func NewFuture

func NewFuture() Future

NewFuture returns a new Future.

type FutureImpl

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

FutureImpl implements the Future interface.

func (*FutureImpl) FlatMap

func (fut *FutureImpl) FlatMap(f func(interface{}) (Future, error)) Future

FlatMap creates a new Future by applying a function to the successful result of this Future and returns the result of the function as a new Future.

func (*FutureImpl) Get

func (fut *FutureImpl) Get() (interface{}, error)

Get blocks until the Future is completed and returns either a result or an error.

func (*FutureImpl) Map

func (fut *FutureImpl) Map(f func(interface{}) (interface{}, error)) Future

Map creates a new Future by applying a function to the successful result of this Future and returns the result of the function as a new Future.

func (*FutureImpl) Recover

func (fut *FutureImpl) Recover(f func() (interface{}, error)) Future

Recover handles any error that this Future might contain using a given resolver function. Returns the result as a new Future.

func (*FutureImpl) RecoverWith

func (fut *FutureImpl) RecoverWith(rf Future) Future

RecoverWith handles any error that this Future might contain using another Future. Returns the result as a new Future.

type OptimisticLock

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

OptimisticLock allows optimistic reading. Implements the Locker interface and is not reentrant.

func NewOptimisticLock

func NewOptimisticLock() *OptimisticLock

NewOptimisticLock returns a new OptimisticLock.

func (*OptimisticLock) Lock

func (o *OptimisticLock) Lock()

Lock locks the resource for write.

func (*OptimisticLock) OptLock

func (o *OptimisticLock) OptLock() int64

OptLock returns the stamp to be verified on OptUnlock.

func (*OptimisticLock) OptUnlock

func (o *OptimisticLock) OptUnlock(stamp int64) bool

OptUnlock returns true if the lock has not been acquired in write mode since obtaining a given stamp. Retry or switch to RLock in case of failure.

func (*OptimisticLock) RLock

func (o *OptimisticLock) RLock()

RLock locks the resource for read.

func (*OptimisticLock) RUnlock

func (o *OptimisticLock) RUnlock()

RUnlock unlocks the resource after read.

func (*OptimisticLock) Unlock

func (o *OptimisticLock) Unlock()

Unlock unlocks the resource after write.

type Promise

type Promise interface {

	// Success completes the underlying Future with a value.
	Success(interface{})

	// Failure fails the underlying Future with an error.
	Failure(error)

	// Future returns the underlying Future.
	Future() Future
}

Promise represents a writable, single-assignment container, which completes a Future.

func NewPromise

func NewPromise() Promise

NewPromise returns a new PromiseImpl.

type PromiseImpl

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

PromiseImpl implements the Promise interface.

func (*PromiseImpl) Failure

func (p *PromiseImpl) Failure(e error)

Failure fails the underlying Future with a given error.

func (*PromiseImpl) Future

func (p *PromiseImpl) Future() Future

Future returns the underlying Future.

func (*PromiseImpl) Success

func (p *PromiseImpl) Success(value interface{})

Success completes the underlying Future with a given value.

type ReentrantLock

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

ReentrantLock allows goroutines to enter the lock more than once.

func NewReentrantLock

func NewReentrantLock() *ReentrantLock

NewReentrantLock returns a new ReentrantLock.

func (*ReentrantLock) Lock

func (r *ReentrantLock) Lock()

Lock locks the resource. Panics if the GoroutineID call returns an error.

func (*ReentrantLock) Unlock

func (r *ReentrantLock) Unlock()

Unlock unlocks the resource. Panics on trying to unlock the unlocked lock.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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