light_future

package module
v0.0.0-...-a60db72 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2021 License: MIT Imports: 9 Imported by: 0

README

light-future

A lightweight future, allows users to better abstract concurrency logic.

Go Report Card Build status

Intro

Using Future tends to be more readable, asynchronous behavior is abstracted under the Future, and code tends to be more elegant

Example


result, err := NewFuture(ctx, func(ctx context.Context) (interface{}, error) {
    b := a
    return b, nil
}).
Send().
Await().
Result()

Benchmark

Compare using future and pass the result by the channel, you can compare this cost to determine whether to use

goos: linux
goarch: amd64
pkg: github.com/ragpanda/light-future
cpu: Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
BenchmarkFuture-8                        	   10000	    104739 ns/op	   45246 B/op	    1006 allocs/op
BenchmarkGoroutineUsingChannelReturn-8   	   14524	     82598 ns/op	   15830 B/op	     398 allocs/op
PASS
ok  	github.com/ragpanda/light-future	3.098s

Documentation

Index

Constants

View Source
const DefaultTimeout = 5 * time.Second

Variables

This section is empty.

Functions

This section is empty.

Types

type Future

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

func Merge

func Merge(ctx context.Context, fList ...*Future) *Future

func NewFuture

func NewFuture(ctx context.Context, runnable FutureFunc) *Future

func (*Future) Await

func (self *Future) Await() *FutureResult

func (*Future) Cancel

func (self *Future) Cancel() *Future

func (*Future) Send

func (self *Future) Send() *Future

func (*Future) Timeout

func (self *Future) Timeout(t time.Duration) *Future

func (*Future) Use

func (self *Future) Use(pool Pool) *Future

type FutureErrorAbstract

type FutureErrorAbstract interface {
	Error() string
}

type FutureFunc

type FutureFunc func(context.Context) (interface{}, error)

type FutureResult

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

func (*FutureResult) Error

func (self *FutureResult) Error() error

func (*FutureResult) Result

func (self *FutureResult) Result() (interface{}, error)

func (*FutureResult) ResultWithFill

func (self *FutureResult) ResultWithFill(r interface{}) error

r is &struct

type GoroutineInfanitePool

type GoroutineInfanitePool struct{}

func (*GoroutineInfanitePool) Exec

func (*GoroutineInfanitePool) Exec(exec func())

type MultipleFutureError

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

func (*MultipleFutureError) Error

func (self *MultipleFutureError) Error() string

type Pool

type Pool interface {
	Exec(func())
}

Jump to

Keyboard shortcuts

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