chain is a small Go workflow library built around composable Action
values. An action receives one value, returns the next value, and can be
connected to other actions through a Workflow.
The package focuses on in-process workflow control. It does not try to be a distributed scheduler or a full parallel DAG engine.
go get github.com/JSYoo5B/chainAn Action is the basic execution unit.
type Action[T any] interface {
Name() string
Run(ctx context.Context, input T) (output T, err error)
}Use NewSimpleAction when a function is enough:
double := chain.NewSimpleAction(
"double",
func(ctx context.Context, input int) (int, error) {
return input * 2, nil
},
)A Workflow connects actions and also implements Action, so workflows can be
nested inside other workflows.
workflow := chain.NewWorkflow("calculation", action1, action2, action3)
output, err := workflow.Run(context.Background(), input)By default, actions are connected through the Success direction in constructor
order. Failure and Abort terminate unless a custom RunPlan is set.
workflow.SetRunPlan(action1, chain.DefaultPlan(action2, errorHandler))A RunPlan maps a direction to the next action.
type RunPlan[T any] map[string]Action[T]Built-in directions are:
SuccessFailureAbort
BranchAction can add custom directions.
NewSimpleActioncreates an action from a function.NewSimpleBranchActioncreates a branch action from functions.AsBranchActionwraps an existing action and adds branching logic.
AsRetryableActionretries a main action and optionally runs rollback before the next attempt.SkipRollbackexplicitly disables rollback for a retryable action.AsBestEffortActionsuppresses a non-critical action error and optionally calls a fallback hook.
AsSequenceSliceActionruns an action over a slice sequentially.AsSequenceMapActionruns an action over a map sequentially.AsParallelSliceActionruns an action over a slice concurrently.AsParallelMapActionruns an action over a map concurrently.
Parallel collection actions preserve output positions or keys, but joined error order follows completion order.
AdaptActionruns an action against a field or sub-value inside a larger value.
This is useful when a workflow carries one aggregate state type, while some actions only operate on one part of that state.
Workflow.ValidateGraph checks the configured workflow graph before execution.
It rejects:
- directed cycles
- disconnected workflow graphs
Branching and merge points are allowed as long as the graph remains a connected DAG.
See: