Flow-based programming is the base of SDK. It is a set of asynchronous modules which can be combinated in workflow. Each module implement a function that has N inputs and M outputs and executes asynchronously (N, M >= 0). In our implementation inputs are untyped. So each module has to check type of received message.
Module has to implement the interface Module
:
type Module interface {
io.Closer
Name() string
Start(ctx context.Context)
Input(name string) (*Input, error)
Output(name string) (*Output, error)
AttachTo(outputName string, input *Input) error
}
Interface contains following methods:
Start
- starts asynchronous waiting of messages in inputs and initialize module state.Close
- gracefully stops all module activities (inherited fromio.Closer
interface).Name
- returns name of module which will be used in workflow construction.Input
- returns input by its name.Output
- returns output by its name.AttachTo
- connects output with name to passed input of another module.
All communication between modules is implemented via inputs/outputs. Input is the structure contains channel with any
as data. It also has name which will be its identity in module's scope.
type Input struct {
data chan any
name string
}
It has following methods:
Close
- closes channel of inputPush
- sends message to channelListen
- waits new messageName
- returns input name
Output is the set of inputs which connected to it. When module send message to output it iterates over all connected inputs and pushes message to them. Output also has name which identifies it.
type Output struct {
connectedInputs []*Input
name string
mx sync.RWMutex
}
It has following methods:
ConnectedInputs
- returns all connected inputsPush
- pushes message to all connected inputsAttach
- adds input to connected inputs arrayName
- returns output name
SDK has helper function Connect
:
func Connect(outputModule, inputModule Module, outputName, inputName string) error
The function receives outputModule
and inputModule
: modules which will be connected. Also it receives input and output names in that modules which will be connected.
Modules can be united in workflow. Workflow is set of module which connected in certain seqeunce. To create Workflow
you can call function NewWorkflow
:
func NewWorkflow(modules ...Module) *Workflow
Workflow
has following functions:
Add
- adds module with name returning from itsName
methodAddWithName
- adds module with name passed to itGet
- returns module by name which module was created withConnect
- connects modules with certain names by names of its input and outputStart
- starts all modules in workflow
SDK has some modules which can be used during workflow creation.
gRPC module where realized default client and server. Detailed docs can be found here.
Cron module implements cron scheduler. Detailed docs can be found here.