conc

package
v0.0.0-...-1593278 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2023 License: Apache-2.0 Imports: 8 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AwaitAll

func AwaitAll[T future](futures ...T) error

Await for multiple futures, Return nil if no future returns error, or return the first error in these futures.

func WarmupPool

func WarmupPool[T any](pool *Pool[T], warmup func())

WarmupPool do warm up logic for each goroutine in pool

Types

type Future

type Future[T any] struct {
	// contains filtered or unexported fields
}

Future is a result type of async-await style. It contains the result (or error) of an async task. Trying to obtain the result (or error) blocks until the async task completes.

func Go

func Go[T any](fn func() (T, error)) *Future[T]

Go spawns a goroutine to execute fn, returns a future that contains the result of fn. NOTE: use Pool if you need limited goroutines.

func (*Future[T]) Await

func (future *Future[T]) Await() (T, error)

Return the result and error of the async task.

func (*Future[T]) Err

func (future *Future[T]) Err() error

Return the error of the async task, nil if no error.

func (*Future[T]) Inner

func (future *Future[T]) Inner() <-chan struct{}

Return a read-only channel, which will be closed if the async task completes. Use this if you need to wait the async task in a select statement.

func (*Future[T]) OK

func (future *Future[T]) OK() bool

False if error occurred, true otherwise.

func (*Future[T]) Value

func (future *Future[T]) Value() T

Return the result of the async task, nil if no result or error occurred.

type Pool

type Pool[T any] struct {
	// contains filtered or unexported fields
}

A goroutine pool

func NewDefaultPool

func NewDefaultPool[T any]() *Pool[T]

NewDefaultPool returns a pool with cap of the number of logical CPU, and pre-alloced goroutines.

func NewPool

func NewPool[T any](cap int, opts ...PoolOption) *Pool[T]

NewPool returns a goroutine pool. cap: the number of workers. This panic if provide any invalid option.

func (*Pool[T]) Cap

func (pool *Pool[T]) Cap() int

The number of workers

func (*Pool[T]) Free

func (pool *Pool[T]) Free() int

Free returns the number of free workers

func (*Pool[T]) Release

func (pool *Pool[T]) Release()

func (*Pool[T]) Running

func (pool *Pool[T]) Running() int

The number of running workers

func (*Pool[T]) Submit

func (pool *Pool[T]) Submit(method func() (T, error)) *Future[T]

Submit a task into the pool, executes it asynchronously. This will block if the pool has finite workers and no idle worker. NOTE: As now golang doesn't support the member method being generic, we use Future[any]

type PoolOption

type PoolOption func(opt *poolOption)

PoolOption options function to setup pool.

func WithConcealPanic

func WithConcealPanic(v bool) PoolOption

func WithDisablePurge

func WithDisablePurge(v bool) PoolOption

func WithExpiryDuration

func WithExpiryDuration(d time.Duration) PoolOption

func WithNonBlocking

func WithNonBlocking(v bool) PoolOption

func WithPreAlloc

func WithPreAlloc(v bool) PoolOption

Jump to

Keyboard shortcuts

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