Documentation ¶
Index ¶
- func Go(ctx context.Context, f func(context.Context) error) <-chan error
- func GoTimed(ctx context.Context, tag string, timeout time.Duration, ...) error
- func Schedule(ctx context.Context, period time.Duration, startImmediately bool, ...) (quit chan bool)
- func ScheduleWithCondition(ctx context.Context, period time.Duration, startImmediately bool, ...) (quit chan bool)
- func ScheduleWithSkip(ctx context.Context, period time.Duration, startImmediately bool, ...) (quit chan bool)
- func StartGoRoutine(ctx context.Context, goFunction interface{}, parameters ...interface{}) error
- func WaitChannels(chs ...<-chan struct{}) <-chan struct{}
- func WaitFunction(myfunc func()) <-chan struct{}
- func WaitOrTimeout(timeout time.Duration, chs ...<-chan struct{})
- func WaitShutdownSignal()
- type ExecutionGroup
- type TimeOutError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Go ¶
Go starts f in a goroutine and recovers any panics that occur. Go returns a channel on which any errors from f, or errors recovered from a panic, are returned. The error channel is closed when the goroutine is finished. Any panics are also logged.
The error channel Go returns is buffered, so it can be ignored without causing a goroutine leak:
_ = Go(ctx, logTag, func(ictx context.Context) error { return foo(ictx, "first argument", "second argument") })
func GoTimed ¶
func GoTimed(ctx context.Context, tag string, timeout time.Duration, function func(ctx context.Context)) error
GoTimed will execute a function with a timeout. nil is returned if the function finishes in time, otherwise an error is returned. tag is for debug purpose, it will be included in the error message
func Schedule ¶
func Schedule(ctx context.Context, period time.Duration, startImmediately bool, what interface{}, whatParameters ...interface{}) (quit chan bool)
Schedule starts the passed-in function with the interval of period set startImmediately to true to run what func right now. if it is set to false, the what func will only start after period
func ScheduleWithCondition ¶
func ScheduleWithCondition(ctx context.Context, period time.Duration, startImmediately bool, startCondition func() bool, what interface{}, whatParameters ...interface{}) (quit chan bool)
ScheduleWithCondition starts the passed-in function with the interval of period if condition check passed set startImmediately to true to run what func right now. if it is set to false, the what func will only start after period
func ScheduleWithSkip ¶
func ScheduleWithSkip(ctx context.Context, period time.Duration, startImmediately bool, startCondition func() bool, what interface{}, whatParameters ...interface{}) (quit chan bool)
ScheduleWithSkip starts the passed-in function with the interval of period if condition check passed this schedule will skip the current ticker if the previous one is not finished
func StartGoRoutine ¶
StartGoRoutine starts the goroutine with a passed-in function, and prints out a log when it panics.
func WaitChannels ¶
func WaitChannels(chs ...<-chan struct{}) <-chan struct{}
WaitChannels returns a channel which will be closed when receiving from all input channels.
func WaitFunction ¶
func WaitFunction(myfunc func()) <-chan struct{}
WaitFunction makes a function to run in another go routine, and returns a channel that closes only after the function finish execution.
func WaitOrTimeout ¶
WaitOrTimeout waits for all channels or timeout.
func WaitShutdownSignal ¶
func WaitShutdownSignal()
WaitShutdownSignal waits for shutdown signal, and if so, then waits for all channels finish or timeout.
Types ¶
type ExecutionGroup ¶
ExecutionGroup is a timed version of typical use cases for WaitGroup It ensures all the sub-goroutines will get a context deadline exceed notification when timeout, which can stop the expired jobs immediately
func NewExecutionGroup ¶
func NewExecutionGroup(ctx context.Context, timeout time.Duration) *ExecutionGroup
NewExecutionGroup returns a new instance
func (*ExecutionGroup) Go ¶
func (eg *ExecutionGroup) Go(tag string, runnable func(ctx context.Context))
Go will execute the function in another go routine. tag is used for tracking purpose in case of timeout
func (*ExecutionGroup) WaitForDone ¶
func (eg *ExecutionGroup) WaitForDone() error
WaitForDone will block until all goroutines in the execution group finish, or at most for the given timeout interval. It timeout occurs, it will return an error indicating which tag was failed.
func (*ExecutionGroup) WaitForDoneWithTimeout ¶
func (eg *ExecutionGroup) WaitForDoneWithTimeout(timeout time.Duration) *TimeOutError
WaitForDone will block at most given timeout. It timeout occurs, it will return an error indicating which tag was failed.
type TimeOutError ¶
type TimeOutError struct {
// contains filtered or unexported fields
}
TimeOutError is for timeout
func (TimeOutError) Tag ¶
func (err TimeOutError) Tag() string
Tag returns the first tag that has timeout
func (TimeOutError) Tags ¶
func (err TimeOutError) Tags() []string
Tags returns all the tags that have timeout