txcore

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package txcore is the framework-agnostic engine behind the txctx (Fiber v2) and txctxv3 (Fiber v3) middlewares. It owns the request-scoped transaction holder, the lazy ConnPool wrapper, and the commit/rollback orchestration.

The public packages (txctx, txctxv3) are thin Fiber adapters: they extract a context.Context from the framework's request type and delegate everything here.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolPtr

func BoolPtr(v bool) *bool

BoolPtr is a helper for setting Config.LazyTx inline.

func Inject

func Inject(parent context.Context, h *Holder) context.Context

Inject returns a new context carrying h.

func RegisterWithManager added in v0.7.0

func RegisterWithManager(mgr CloserRegistrar, phase int, timeout time.Duration)

RegisterWithManager registers a PhasePostDrain closer that blocks until all active transaction holders (including their compensation callbacks) finish. This prevents gscore's PhaseDB from closing the *sql.DB while OnRollback or OnCommit callbacks are still issuing queries.

Calling this also activates per-holder WaitGroup tracking: every subsequent NewHolder call contributes to the counter, and Commit/Rollback decrements it.

phase must be gscore.PhasePostDrain (value 2).

txcore.RegisterWithManager(mgr, gscore.PhasePostDrain, 35*time.Second)

Types

type CloserRegistrar added in v0.7.0

type CloserRegistrar = gscore.CloserRegistrar

CloserRegistrar is the subset of gscore.Manager used by RegisterWithManager. It is a type alias for gscore.CloserRegistrar; *gscore.Manager satisfies it directly.

type Config

type Config struct {
	Timeout         time.Duration
	LazyTx          *bool
	CompensationCtx time.Duration
	OnCallbackError func(error)
}

Config holds the shared middleware options for both the v2 and v3 Fiber adapters. LazyTx is *bool so the zero value can be distinguished from an explicit false; when nil, the default is true (lazy mode).

func (Config) WithDefaults

func (cfg Config) WithDefaults() Config

WithDefaults returns cfg with zero-valued fields filled in.

type Holder

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

Holder is the request-scoped transaction state.

func FromCtx

func FromCtx(ctx context.Context) *Holder

FromCtx returns the Holder stored in ctx, or nil.

func MustFromCtx

func MustFromCtx(ctx context.Context) *Holder

MustFromCtx is FromCtx but panics if no holder is present.

func NewHolder

func NewHolder(db *gorm.DB, compensCtx time.Duration, lazy bool, onErr func(error)) *Holder

NewHolder constructs a Holder. The caller (middleware) is responsible for placing it in a context via Inject. When RegisterWithManager has been called, the holder automatically registers with the global WaitGroup and Commit / Rollback will decrement it after compensation callbacks finish.

func (*Holder) AppendOnCommit

func (h *Holder) AppendOnCommit(fn func(*gorm.DB) error)

AppendOnCommit registers fn to run only if the transaction commits successfully.

func (*Holder) AppendOnRollback

func (h *Holder) AppendOnRollback(fn func(*gorm.DB) error)

AppendOnRollback registers fn to run if the transaction rolls back.

func (*Holder) BaseDB

func (h *Holder) BaseDB() *gorm.DB

func (*Holder) Begin

func (h *Holder) Begin(ctx context.Context)

Begin opens the underlying *sql.Tx exactly once. Subsequent calls are no-ops.

func (*Holder) Commit

func (h *Holder) Commit() (commitErr error, postCommitErr error)

Commit returns (commitErr, postCommitErr). commitErr is set if tx.Commit itself failed — caller should treat as rollback. postCommitErr is set if an OnCommit callback failed after a successful commit — caller MUST NOT rollback (the tx is already durable).

func (*Holder) DB

func (h *Holder) DB(ctx context.Context) *gorm.DB

DB returns the request-scoped *gorm.DB. With lazy mode, the first write transparently opens BEGIN; reads stay outside any transaction.

func (*Holder) OnCommitLen

func (h *Holder) OnCommitLen() int

func (*Holder) OnRollbackLen

func (h *Holder) OnRollbackLen() int

func (*Holder) Outside

func (h *Holder) Outside(ctx context.Context) *gorm.DB

Outside returns a *gorm.DB whose context is decoupled from the request cancellation but preserves request values.

func (*Holder) Rollback

func (h *Holder) Rollback()

Rollback rolls back the transaction (if any) and runs all OnRollback callbacks with a fresh context whose deadline is compensCtx.

func (*Holder) Started

func (h *Holder) Started() bool

func (*Holder) Tx

func (h *Holder) Tx() *gorm.DB

Jump to

Keyboard shortcuts

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