Documentation
¶
Overview ¶
Package step has the durable steps a flow function calls: Run, Sleep, SleepUntil, WaitForEvent, Invoke and Send.
How replay works: the engine calls the function from the top on every request, with the results of finished steps (the memo). A step that is in the memo returns its stored result at once. A step that is new is run (for Run) or reported to the engine (the others), and then the SDK stops the function with a special panic that client.Serve() recovers.
So do not swallow that panic. If you recover() around step calls, call Repanic first:
defer func() {
if r := recover(); r != nil {
step.Repanic(r) // lets the SDK stop the function
// ... your own handling
}
}()
Step IDs must be stable between calls. The same ID used again (in a loop) is fine: repeats get ":1", ":2", ... added before hashing. Inside group.Parallel, give each step its own ID.
Do not call steps from goroutines you start yourself; use group.Parallel.
Index ¶
- Variables
- func IdempotencyKey(ctx context.Context) string
- func Invoke[T any](ctx context.Context, id string, opts InvokeOpts) (T, error)
- func IsControlPanic(r any) bool
- func Repanic(r any)
- func Run[T any](ctx context.Context, id string, fn func(ctx context.Context) (T, error)) (T, error)
- func Send(ctx context.Context, id string, events ...core.Event) ([]string, error)
- func Sleep(ctx context.Context, id string, d time.Duration) error
- func SleepUntil(ctx context.Context, id string, t time.Time) error
- func WaitForEvent[T any](ctx context.Context, id string, opts WaitForEventOpts) (T, error)
- type InvokeOpts
- type StepError
- type WaitForEventOpts
Constants ¶
This section is empty.
Variables ¶
var ErrEventNotReceived = errors.New("flow: event not received before the timeout")
ErrEventNotReceived is returned by WaitForEvent when the wait timed out.
var ErrNotInFunction = core.ErrNotInFunction
ErrNotInFunction is returned by step calls made outside a flow function or inside another step's callback.
Functions ¶
func IdempotencyKey ¶
IdempotencyKey returns "<runId>:<hashed step id>" inside a Run callback. It is the same on every attempt, so pass it to outside APIs that take one. It is empty outside a Run callback.
func Invoke ¶
Invoke calls another flow function and returns its output. If the child fails, Invoke returns a *StepError.
func IsControlPanic ¶
IsControlPanic reports whether a recovered value is the SDK's control panic.
func Repanic ¶
func Repanic(r any)
Repanic panics again with r if r is the SDK's control panic. Call it first in any recover() that can catch step calls.
func Run ¶
Run runs fn once, keeps its result, and returns the kept result on replay. If fn returns an error, the step fails and the engine retries it (wrap the error with flow.NoRetryError or flow.RetryAtError to change that). When the step is out of retries, Run returns a *StepError.
func Send ¶
Send sends one or more events from the function, once (not again on replay). It returns the event IDs.
func SleepUntil ¶
SleepUntil pauses the function until t.
func WaitForEvent ¶
WaitForEvent waits for an event and returns it. T is the event type, for example flow.GenericEvent[MyData]. On timeout it returns ErrEventNotReceived.
Types ¶
type InvokeOpts ¶
type InvokeOpts struct {
// FunctionID is the full ID "<appId>-<functionId>". It can be in another
// app, in any language.
FunctionID string
// Data becomes the child's event data.
Data any
// User becomes the child's event user field.
User any
// Timeout stops waiting after this long; the step then fails with
// InvokeTimeout. Zero means no timeout.
Timeout time.Duration
}
InvokeOpts says which function to call.
type StepError ¶
StepError is returned by a step whose stored result is an error: the step ran out of retries, or an invoked function failed. Check for it with errors.As to run compensation steps.
type WaitForEventOpts ¶
type WaitForEventOpts struct {
// Event is the name of the event to wait for.
Event string
// Timeout is how long to wait. Required.
Timeout time.Duration
// Match is a field path in both events, for example "data.userId".
Match *string
// If is an expression over event (the trigger) and async (the new event).
If *string
}
WaitForEventOpts says which event to wait for.