saga

package
v1.16.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2026 License: Apache-2.0 Imports: 2 Imported by: 3

README

pkg/server/saga

Intention

pkg/server/saga provides a deliberately small in-process saga runner for multi-step server workflows that need best-effort cleanup on failure.

Its purpose is to give handlers a common rollback pattern for synchronous operations that perform several state-changing steps, such as quota allocation followed by resource creation. Instead of open-coding partial rollback in every handler, callers describe an ordered list of forward actions and optional compensation actions.

This package is intentionally a first-cut implementation. The goal is to get server workflows onto a shared best-effort cleanup model, not to provide durable orchestration, reliable recovery, or distributed saga semantics.

Invariants And Guard Rails

  • This package is for synchronous in-process rollback coordination, not for durable workflow orchestration.
  • A saga handler defines an ordered list of actions. Run() executes them in order on the forward path.
  • If an action fails, previously completed actions are compensated in reverse order when a compensation function is defined.
  • The original action failure is the error returned to the caller, even if a later compensation step also fails.
  • Actions and compensations are typically bound receivers so saga steps can share state accumulated during the workflow.
  • Compensation is optional per action. Callers must be explicit about which state changes can and cannot be unwound.

Caveats

  • Cleanup is best-effort only. There is no durable saga log, resumability, retry policy, or asynchronous recovery.
  • If a compensation step fails, the package logs the compensation failure and returns the original action error. Recovery may therefore be incomplete and require manual cleanup.
  • The implementation is deliberately minimal. It does not attempt to classify transient versus terminal errors or make policy decisions about retries.
  • This package does not make a distributed operation atomic. It only provides a structured local pattern for attempting rollback after partial failure.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, handler Handler) error

Run implements the saga algorithm.

Types

type Action

type Action struct {
	// contains filtered or unexported fields
}

Action is a single step in a saga.

func NewAction

func NewAction(name string, action, compensate ActionFunc) Action

NewAction creates a new action.

type ActionFunc

type ActionFunc func(ctx context.Context) error

ActionFunc is a generic action/compensation function. They will typically be bound receivers so that saga steps can share state between themselves.

type Handler

type Handler interface {
	Actions() []Action
}

Handler implements a saga, a set of steps to achieve a desired outcome and a set of steps to undo any state changes on failure of an action.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL