compensation

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package compensation provides enhanced saga pattern support for workflow compensation.

Package compensation provides saga pattern support for workflow compensation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompensationBuilder

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

CompensationBuilder provides a fluent interface for building compensation handlers.

func NewCompensation

func NewCompensation(name string) *CompensationBuilder

NewCompensation creates a new compensation builder.

func (*CompensationBuilder) Build

Build returns the compensation function.

func (*CompensationBuilder) WithAction

WithAction sets the compensation action.

func (*CompensationBuilder) WithRetry

func (b *CompensationBuilder) WithRetry(maxAttempts int) *CompensationBuilder

WithRetry wraps the compensation with retry logic.

func (*CompensationBuilder) WithTimeout

WithTimeout wraps the compensation with a timeout.

type CompensationError

type CompensationError struct {
	Errors []error
}

CompensationError aggregates multiple compensation failures.

func (*CompensationError) Error

func (e *CompensationError) Error() string

Error implements the error interface.

func (*CompensationError) Unwrap

func (e *CompensationError) Unwrap() error

Unwrap returns the first error for errors.Is/As compatibility.

type CompensationExecutionRecord

type CompensationExecutionRecord struct {
	ActivityName string             `json:"activityName"`
	Status       CompensationStatus `json:"status"`
	ExecutedAt   time.Time          `json:"executedAt,omitempty"`
	Duration     time.Duration      `json:"duration,omitempty"`
	Error        string             `json:"error,omitempty"`
	Retries      int                `json:"retries,omitempty"`
}

CompensationExecutionRecord tracks compensation execution details.

type CompensationFunc

type CompensationFunc func(ctx context.Context) error

CompensationFunc is a function that compensates for a previous action.

type CompensationManager

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

CompensationManager manages workflow-level compensation transactions.

func NewCompensationManager

func NewCompensationManager(ctx workflow.Context) *CompensationManager

NewCompensationManager creates a new CompensationManager instance.

func (*CompensationManager) Clear

func (cm *CompensationManager) Clear()

Clear removes all registered compensations and records.

func (*CompensationManager) Compensate

func (cm *CompensationManager) Compensate(ctx workflow.Context) error

Compensate runs all registered compensations in reverse order.

func (*CompensationManager) CompensatePartial

func (cm *CompensationManager) CompensatePartial(ctx workflow.Context, activityNames []string) error

CompensatePartial runs compensation for specific activities only.

func (*CompensationManager) CompensationCount

func (cm *CompensationManager) CompensationCount() int

CompensationCount returns the number of registered compensations.

func (*CompensationManager) ExecutedCount

func (cm *CompensationManager) ExecutedCount() int

ExecutedCount returns the number of executed activities.

func (*CompensationManager) GetExecutedActivities

func (cm *CompensationManager) GetExecutedActivities() []string

GetExecutedActivities returns a copy of the executed activity names.

func (*CompensationManager) IsExecuted

func (cm *CompensationManager) IsExecuted(activityName string) bool

IsExecuted checks if an activity was executed.

func (*CompensationManager) RecordExecution

func (cm *CompensationManager) RecordExecution(activityName string)

RecordExecution marks an activity as successfully executed.

func (*CompensationManager) Records

Records returns the compensation execution records.

func (*CompensationManager) RegisterCompensation

func (cm *CompensationManager) RegisterCompensation(step CompensationStep)

RegisterCompensation adds a compensation step for an activity.

func (*CompensationManager) RegisterSimple

func (cm *CompensationManager) RegisterSimple(activityName string, fn WorkflowCompensationFunc, input interface{})

RegisterSimple adds a simple compensation with default settings.

type CompensationRecord

type CompensationRecord struct {
	Name       string        `json:"name"`
	Executed   bool          `json:"executed"`
	ExecutedAt time.Time     `json:"executedAt,omitempty"`
	Error      string        `json:"error,omitempty"`
	Duration   time.Duration `json:"duration,omitempty"`
}

CompensationRecord tracks a compensation action and its state.

func ExecuteSaga

func ExecuteSaga(ctx context.Context, steps []SagaStep) ([]CompensationRecord, error)

ExecuteSaga executes a series of steps with automatic compensation on failure.

type CompensationStatus

type CompensationStatus string

CompensationStatus represents the status of a compensation operation.

const (
	CompensationPending   CompensationStatus = "pending"
	CompensationRunning   CompensationStatus = "running"
	CompensationCompleted CompensationStatus = "completed"
	CompensationFailed    CompensationStatus = "failed"
	CompensationSkipped   CompensationStatus = "skipped"
)

type CompensationStep

type CompensationStep struct {
	ActivityName string
	CompensateFn WorkflowCompensationFunc
	Input        interface{}
	Timeout      time.Duration
	RetryPolicy  *temporal.RetryPolicy
	Idempotent   bool
	AllowSkip    bool // If true, failure doesn't stop other compensations
}

CompensationStep represents a single compensation operation.

type ResourceCleanup

type ResourceCleanup struct{}

ResourceCleanup provides common compensation handlers for resource cleanup.

func NewResourceCleanup

func NewResourceCleanup() *ResourceCleanup

NewResourceCleanup creates a new ResourceCleanup instance.

func (*ResourceCleanup) CancelOperation

func (r *ResourceCleanup) CancelOperation(operationType, operationID string, cancelFn func(ctx context.Context, id string) error) CompensationFunc

CancelOperation creates a compensation handler to cancel an operation.

func (*ResourceCleanup) DeleteResource

func (r *ResourceCleanup) DeleteResource(resourceType, resourceID string, deleteFn func(ctx context.Context, id string) error) CompensationFunc

DeleteResource creates a compensation handler to delete a resource.

func (*ResourceCleanup) RevertState

func (r *ResourceCleanup) RevertState(resourceType, resourceID string, previousState json.RawMessage, revertFn func(ctx context.Context, id string, state json.RawMessage) error) CompensationFunc

RevertState creates a compensation handler to revert state.

type SagaManager

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

SagaManager manages compensation transactions for workflows.

func NewSagaManager

func NewSagaManager() *SagaManager

NewSagaManager creates a new SagaManager instance.

func (*SagaManager) AddCompensation

func (s *SagaManager) AddCompensation(name string, fn CompensationFunc)

AddCompensation registers a compensation function to be executed on rollback. Compensations are executed in reverse order (LIFO).

func (*SagaManager) Clear

func (s *SagaManager) Clear()

Clear removes all registered compensations.

func (*SagaManager) Compensate

func (s *SagaManager) Compensate(ctx context.Context) error

Compensate executes all registered compensations in reverse order. It continues executing even if individual compensations fail.

func (*SagaManager) Count

func (s *SagaManager) Count() int

Count returns the number of registered compensations.

func (*SagaManager) Records

func (s *SagaManager) Records() []CompensationRecord

Records returns the compensation execution records.

type SagaStep

type SagaStep struct {
	Name         string
	Action       func(ctx context.Context) error
	Compensation CompensationFunc
}

SagaStep represents a single step in a saga with its action and compensation.

type TransactionalSaga

type TransactionalSaga struct {
	*SagaManager
	// contains filtered or unexported fields
}

TransactionalSaga extends SagaManager with transaction-like semantics.

func NewTransactionalSaga

func NewTransactionalSaga() *TransactionalSaga

NewTransactionalSaga creates a new transactional saga.

func (*TransactionalSaga) Commit

func (s *TransactionalSaga) Commit()

Commit marks the saga as committed, preventing automatic compensation.

func (*TransactionalSaga) IsCommitted

func (s *TransactionalSaga) IsCommitted() bool

IsCommitted returns true if the saga has been committed.

func (*TransactionalSaga) Rollback

func (s *TransactionalSaga) Rollback(ctx context.Context) error

Rollback executes compensations if not committed.

type WorkflowCompensationFunc

type WorkflowCompensationFunc func(ctx workflow.Context, input interface{}) error

CompensationFunc is a function executed via Temporal workflow context.

Directories

Path Synopsis
Package repository provides data access for compensation records.
Package repository provides data access for compensation records.
Package testing provides test utilities for compensation workflows.
Package testing provides test utilities for compensation workflows.

Jump to

Keyboard shortcuts

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