ltx

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 8 Imported by: 0

README

ltx

Go Reference

Lightweight logical transactions.

Overview

A logical transactions is a sequence of operations that can be undone if something goes wrong. This is useful for managing resources that need to be cleaned up in case of failure, such as temporary files, third party mutations or other side effects.

The ltx API mimics database/sql transactions with BeginTx, Commit, and Rollback and adds Cleanup from testing to register callbacks that run on rollback.

Install

go get github.com/olahol/ltx

Usage

tx, err := ltx.BeginTx(ctx)
if err != nil {
    return err
}
defer tx.Rollback() // safe to call even after commit

resource, err := createResource()
if err != nil {
    return err
}
tx.Cleanup(func(ctx context.Context) error {
    return deleteResource(resource)
})

err = registerService(resource)
if err != nil {
    return err // rollback deletes the resource
}
tx.Cleanup(func(ctx context.Context) error {
    return deregisterService(resource)
})

tx.Commit()

License

MIT

Documentation

Overview

Package ltx provides logical transactions.

Index

Constants

This section is empty.

Variables

View Source
var ErrFinished = errors.New("ltx: transaction has already finished")

ErrFinished is returned when operating on a finished transaction.

Functions

This section is empty.

Types

type Tx

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

Tx is a logical transaction.

func BeginTx

func BeginTx(ctx context.Context, opts ...TxOption) (*Tx, error)

BeginTx starts a logical transaction.

func (*Tx) Cleanup

func (tx *Tx) Cleanup(fn func(context.Context) error)

Cleanup is like Compensate but does not return a cancel function or error.

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit ends the transaction, making any call to Rollback a noop. If the transaction is already finished, Commit returns ErrFinished.

func (*Tx) Compensate

func (tx *Tx) Compensate(fn func(context.Context) error) (cancel func(), err error)

Compensate adds a compensation function to be called on rollback and returns a cancel function to deactivate the compensation and ErrFinished if the transaction is already finished. If the compensation is canceled, it will not be called during rollback. Calls after the transaction is finished (committed or rolled back) are a noop.

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback aborts the transaction and executes compensation functions in reverse order unless parallel cleanup is enabled then compensations are run concurrently. If the transaction is already finished, Rollback is a noop and returns nil.

type TxOption

type TxOption func(*Tx) error

TxOption configures a transaction.

func WithParallelCleanup

func WithParallelCleanup() TxOption

WithParallelCleanup runs rollback compensations concurrently.

func WithRollbackIgnoresCancel

func WithRollbackIgnoresCancel() TxOption

WithRollbackIgnoresCancel ensures compensations run even if ctx is canceled.

func WithRollbackTimeout

func WithRollbackTimeout(d time.Duration) TxOption

WithRollbackTimeout sets the context timeout for compensations during rollback.

Jump to

Keyboard shortcuts

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