compensation

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package compensation provides saga compensation execution.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompensationError

type CompensationError struct {
	ActivityID string
	FuncName   string
	Err        error
}

CompensationError wraps an error that occurred during compensation execution.

func (*CompensationError) Error

func (e *CompensationError) Error() string

func (*CompensationError) Unwrap

func (e *CompensationError) Unwrap() error

type CompensationFunc

type CompensationFunc func(ctx context.Context, arg []byte) error

CompensationFunc is a function that can be executed as compensation. The argument is JSON-encoded data that was passed when registering.

type Entry

type Entry struct {
	ActivityID   string
	FunctionName string
	Argument     any // Will be JSON-encoded
}

Entry represents a compensation entry to be registered.

type Executor

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

Executor handles saga compensation execution.

func NewExecutor

func NewExecutor(s storage.Storage, registry *Registry) *Executor

NewExecutor creates a new compensation executor.

func (*Executor) AddCompensation

func (e *Executor) AddCompensation(ctx context.Context, instanceID string, entry Entry) error

AddCompensation registers a compensation action for an activity. The compensation will be executed in reverse order (LIFO) if the workflow fails.

func (*Executor) ExecuteCompensations

func (e *Executor) ExecuteCompensations(ctx context.Context, instanceID string) error

ExecuteCompensations runs all pending compensations for a workflow in LIFO order. Returns the first error encountered, but continues executing remaining compensations.

type Registry

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

Registry holds registered compensation functions by name.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new compensation function registry.

func (*Registry) Get

func (r *Registry) Get(name string) (CompensationFunc, bool)

Get retrieves a compensation function by name.

func (*Registry) Register

func (r *Registry) Register(name string, fn CompensationFunc)

Register adds a compensation function to the registry.

type TypedCompensation

type TypedCompensation[T any] struct {
	Name string
	Fn   func(ctx context.Context, arg T) error
}

TypedCompensation wraps a typed compensation function for registry.

func NewTypedCompensation

func NewTypedCompensation[T any](name string, fn func(ctx context.Context, arg T) error) *TypedCompensation[T]

NewTypedCompensation creates a typed compensation wrapper.

func (*TypedCompensation[T]) AsFunc

func (tc *TypedCompensation[T]) AsFunc() CompensationFunc

AsFunc returns the raw compensation function for direct use.

func (*TypedCompensation[T]) Register

func (tc *TypedCompensation[T]) Register(r *Registry)

Register registers the typed compensation function in the registry.

Jump to

Keyboard shortcuts

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