Callback is a function that is returned by a Task. Callbacks are called in the same order that tasks are submitted.
type Callback func()
Stream is used to execute a stream of tasks concurrently while maintaining the order of the results.
To use a stream, you submit some number of `Task`s, each of which return a callback. Each task will be executed concurrently in the stream's associated Pool, and the callbacks will be executed sequentially in the order the tasks were submitted.
Once all your tasks have been submitted, Wait() must be called to clean up running goroutines and propagate any panics.
In the case of panic during execution of a task or a callback, all other tasks and callbacks will still execute. The panic will be propagated to the caller when Wait() is called.
A Stream is efficient, but not zero cost. It should not be used for very short tasks. Startup and teardown adds an overhead of a couple of microseconds, and the overhead for each task is roughly 500ns. It should be good enough for any task that requires a network call.
type Stream struct {
// contains filtered or unexported fields
}
▹ Example
func New() *Stream
New creates a new Stream with default settings.
func (s *Stream) Go(f Task)
Go schedules a task to be run in the stream's pool. All submitted tasks will be executed concurrently in worker goroutines. Then, the callbacks returned by the tasks will be executed in the order that the tasks were submitted. All callbacks will be executed by the same goroutine, so no synchronization is necessary between callbacks. If all goroutines in the stream's pool are busy, a call to Go() will block until the task can be started.
func (s *Stream) Wait()
Wait signals to the stream that all tasks have been submitted. Wait will not return until all tasks and callbacks have been run.
func (s *Stream) WithMaxGoroutines(n int) *Stream
Task is a task that is submitted to the stream. Submitted tasks will be executed concurrently. It returns a callback that will be called after the task has completed.
type Task func() Callback