Documentation
¶
Overview ¶
Package async provide different kind of interface and implementation to manipulate a bit more safely the go routing.
First things provided by this package is an implementation of the pattern Async/Await you can find in Javascript It should be used when you need to run multiple asynchronous task and wait for each of them to finish. Example:
func doneAsync() int {
// asynchronous task
time.Sleep(3 * time.Second)
return 1
}
func synchronousTask() {
next := async.Async(func() interface{} {
return doneAsync()
})
// do some other stuff
// then wait for the end of the asynchronous task and get back the result
result := next.Await()
// do something with the result
}
It is useful to use this implementation when you want to paralyze quickly some short function like paralyzing multiple HTTP request. You definitely won't use this implementation if you want to create a cron or a long task. Instead you should implement the interface SimpleTask or Task for doing that.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JoinAll ¶
func JoinAll(ctx context.Context, timeout time.Duration, taskRunners []TaskRunner)
JoinAll is waiting for context to be canceled A task that is ended and should stop the whole application, must have called the master cancelFunc shared by every TaskRunner which will closed the master context.
func LaunchRunner ¶
func LaunchRunner(ctx context.Context, cancelFunc context.CancelFunc, t TaskRunner)
LaunchRunner is executing in a go-routing the TaskRunner that handles a unique task
Types ¶
type SimpleTask ¶
type SimpleTask interface {
// Implements fmt.Stringer for logging purpose (able to identify the execute or in the logs)
fmt.Stringer
// Execute is the method called once or periodically to execute the task
// ctx is the parent context, you usually use it to listen if the context is done.
// cancel will be used to cancel the ctx and to propagate the information to others Task that they have to stop since it will close the channel
// Use it if you are going to implement a task that is critical to your application and if it stops then it should stop your whole application.
// A good example is the signalListener. You want to stop your application in case a sigterm is sent to the application. So in that particular case, the signalListener will call the cancelFunc
Execute(ctx context.Context, cancelFunc context.CancelFunc) error
}
SimpleTask is the simple way to define an asynchronous task. It will be used by the runner. Depending of what you want, the runner will can only once the method Execute or periodically.
func NewSignalListener ¶
func NewSignalListener(signals ...os.Signal) SimpleTask
type Task ¶
type Task interface {
SimpleTask
// Initialize is called by the runner before the Task is running
Initialize() error
// Finalize is called by the runner when it ends (clean-up, wait children, ...)
Finalize() error
}
Task is a more complete way to define an asynchronous task. Use it when you want a finer control of each steps when the task will run.
type TaskRunner ¶
type TaskRunner interface {
fmt.Stringer
Start(ctx context.Context, cancelFunc context.CancelFunc) error
// Done returns the channel used to wait for the task job to be finalized
Done() <-chan struct{}
}
TaskRunner is an interface that defines a wrapper of the task that would help to execute it. Even if this interface is public, you usually don't have to implement it yourself, you just have to implement a Task or a SimpleTask
func NewCron ¶
func NewCron(task interface{}, interval time.Duration) (TaskRunner, error)
NewCron is returning a TaskRunner that will execute the task periodically task can be a SimpleTask or a Task. It returns an error if it's something different
func NewTaskRunner ¶
func NewTaskRunner(task interface{}) (TaskRunner, error)