This is a kind of workflows library inspired by:
The idea is to provide an easy way to implement complex data flow processes, included splits and joins. To achieve that, this lib uses these elements that are described below.
type Job func(context.Context, Postman)
Each Job implements a unique task in the Flow. It comunitcates with the rest of Jobs (parents and childs), by receiving and sending Param messages:
type Param struct {
Err error
Name string
Value interface{}
}
To receive/send messages from/to its parents/children, each Job receives a Postman implementation:
type Postman interface {
Receive(ctx context.Context) (*Param, error)
Send(ctx context.Context, p Param) bool
}
Neither of the two methods is a blocker. I mean that Receive method will return a nil param if there is not a message to be read from parents, and the Send method will return false if the message has not been sent. It can occur if the children are not still ready to receive it. But you are free to make them locker by wrapping them in an infinite loop until the message is read/sent.
A Worker wrappers a Job to be indcluded and managed by a Flow. It is in charge of:
- Ensuring that the Job is up and running.
- Kill the Job when the flow is also killed.
- Connecting the Job with its parents and children.
- Provide messages from parents through the Postman.Receive method.
- Filter messages from parents if a filter has been provided. The filter is provided as an option to the Worker constructor.
type InFilterFunc func(Param) bool
- Sending output messages from the Job to its children through the Postman.Send method
The Flow performs the flow role. It is provided with a list of workers. When it starts, all of its workers also start. And when it finishes, all of its worker finishes.
These are the steps to flow to build a flow:
- Coding the Job functions
- Wrapping the Jobs into Workers
- Defining the Flow
- Adding the Workers to the Flow
- Starting the Flow
- If the Flow is not and endless one, kill it when it finishes.
See Sorting flow example and tests to figure out how to code your own flows in a easy way. It's really easy to do thanks to Go concurrency features
I've coded it only for fun. Please read the LICENSE. I hope you enjoy using it as much as I enjoyed building it. All ideas are welcome. Please let me know what you would add or change.
- Adding more examples