async

package
v0.0.0-...-2b6943d Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoID

func GoID() int

GoID returns id of goroutine if exists, otherwise return 0

func Parallel

func Parallel(ctx context.Context, fn ...interface {
	Run(context.Context)
},
) (<-chan struct{}, func())

func Peer

func Peer[T any](c chans.Result[T], fn func(context.Context) (T, error)) peer[T]

func SetDefaultContext

func SetDefaultContext(ctx context.Context)

func SetGoPanicLogger

func SetGoPanicLogger(l interfaces.Logger)

SetGoPanicLogger for setting goroutine panic logger

func Task

func Task[T any](task parallelJob[T]) peer[T]

func TaskFunc

func TaskFunc[T any](fn func(context.Context) (T, error)) peer[T]

Types

type ErrorPolicy

type ErrorPolicy = int8

ErrorPolicy for what to do if you encounter an error

const (
	// ErrorPolicyStop will not continue with remaining goroutine
	ErrorPolicyStop ErrorPolicy = iota + 1
	// ErrorPolicyCountine will ignore error
	ErrorPolicyCountine
)

type GoGroup

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

GoGroup is a goroutine wrapper that limits the number of concurrent and generated

func NewGoGroup

func NewGoGroup(opts ...GroupOption) *GoGroup

NewGoGroup is used to create a GoGroup

func (*GoGroup) Cancel

func (gg *GoGroup) Cancel()

Cancel is used to try to stop goroutine

func (*GoGroup) Err

func (gg *GoGroup) Err() error

Err get first error of goroutines

func (*GoGroup) Go

func (gg *GoGroup) Go(f func(context.Context), opts ...Option) *Job

Go start a goroutine with strategy

func (*GoGroup) GoRaw

func (gg *GoGroup) GoRaw(f func(), opts ...Option) *Job

GoRaw start a goroutine ignore context

func (*GoGroup) GoWithErr

func (gg *GoGroup) GoWithErr(f func(context.Context) error, opts ...Option) *Job

GoWithErr start a goroutine with strategy and return error

func (*GoGroup) Wait

func (gg *GoGroup) Wait()

Wait for all job complete

type GroupOption

type GroupOption interface {
	// contains filtered or unexported methods
}

GroupOption is option pattern for NewGoGroup

func GroupContext

func GroupContext(ctx context.Context) GroupOption

GroupContext is used to set external context

func GroupErrorPolicy

func GroupErrorPolicy(policy ErrorPolicy) GroupOption

GroupErrorPolicy set what to do if you encounter an error

func MaxConcurrency

func MaxConcurrency(c uint64) GroupOption

MaxConcurrency is used to set max concurrency count

func MaxGoroutine

func MaxGoroutine(c uint64) GroupOption

MaxGoroutine is used to set max goroutine count

type Job

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

Job is a goroutine controller

func Go

func Go(f func(context.Context), opts ...Option) *Job

Go protected goroutine startup method

func GoRaw

func GoRaw(f func(), opts ...Option) *Job

GoRaw start a goroutine with panic protection only

func GoWithErr

func GoWithErr(f func(context.Context) error, opts ...Option) *Job

GoWithErr protected goroutine startup method with error return

func (*Job) Cancel

func (j *Job) Cancel()

Cancel is used to try to stop a goroutine

func (*Job) Done

func (j *Job) Done() <-chan struct{}

Done is then channel for job complete

func (*Job) Err

func (j *Job) Err() error

Err is any error during job

func (*Job) Stop

func (j *Job) Stop()

Stop is cancel and wait

func (*Job) Stopped

func (j *Job) Stopped() bool

Stopped is used to check if the job is stopped

func (*Job) Wait

func (j *Job) Wait()

Wait for goroutine complete

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option pattern

func AlwaysRestartOnFailure

func AlwaysRestartOnFailure() Option

AlwaysRestartOnFailure is used to restart the job after crash

func OnPost

func OnPost(f func()) Option

OnPost to set post-execution callbacks

func OnPre

func OnPre(f func()) Option

OnPre to set pre-execution callbacks

func RestartOnFailure

func RestartOnFailure(r RestartPolicy) Option

RestartOnFailure to set the policy to restart the goroutine after crash

func WithBlockChan

func WithBlockChan(c chan struct{}) Option

WithBlockChan for concurrent count control

func WithContext

func WithContext(ctx context.Context) Option

WithContext for custom context

func WithCount

func WithCount(c uint64) Option

WithCount for set repeat count of goroutine

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout for maximum execution time

func WithWaitGroup

func WithWaitGroup(wg *sync.WaitGroup) Option

WithWaitGroup for set external waitgroup

type RestartPolicy

type RestartPolicy struct {
	// If Times is 0, it will not restart
	Times int
	// Default restart delay time, if the call to DelayFunc fails this value is still used
	Delay time.Duration
	// If DelayFunc is set, DelayFunc is used first, otherwise Delay is used.
	// If the second return value is false, it will not restart
	DelayFunc func(int) (time.Duration, bool)
	// contains filtered or unexported fields
}

RestartPolicy number and interval of restarts

type Runner

type Runner func()

func (Runner) Run

func (f Runner) Run(ctx context.Context)

Jump to

Keyboard shortcuts

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