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 ¶
- func BoolPtr(v bool) *bool
- func Inject(parent context.Context, h *Holder) context.Context
- func RegisterWithManager(mgr CloserRegistrar, phase int, timeout time.Duration)
- type CloserRegistrar
- type Config
- type Holder
- func (h *Holder) AppendOnCommit(fn func(*gorm.DB) error)
- func (h *Holder) AppendOnRollback(fn func(*gorm.DB) error)
- func (h *Holder) BaseDB() *gorm.DB
- func (h *Holder) Begin(ctx context.Context)
- func (h *Holder) Commit() (commitErr error, postCommitErr error)
- func (h *Holder) DB(ctx context.Context) *gorm.DB
- func (h *Holder) OnCommitLen() int
- func (h *Holder) OnRollbackLen() int
- func (h *Holder) Outside(ctx context.Context) *gorm.DB
- func (h *Holder) Rollback()
- func (h *Holder) Started() bool
- func (h *Holder) Tx() *gorm.DB
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 MustFromCtx ¶
MustFromCtx is FromCtx but panics if no holder is present.
func NewHolder ¶
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 ¶
AppendOnCommit registers fn to run only if the transaction commits successfully.
func (*Holder) AppendOnRollback ¶
AppendOnRollback registers fn to run if the transaction rolls back.
func (*Holder) Begin ¶
Begin opens the underlying *sql.Tx exactly once. Subsequent calls are no-ops.
func (*Holder) Commit ¶
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 ¶
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 (*Holder) OnRollbackLen ¶
func (*Holder) Outside ¶
Outside returns a *gorm.DB whose context is decoupled from the request cancellation but preserves request values.