tx

package
v0.9.6 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package tx 是事务边界的接口面,零驱动依赖。

业务包只 import 本包:service 层用 Transactor.Do 划定事务边界, 事务句柄由实现方(appkit/pgtx)经 With 藏进 context,业务代码永远见不到它。 contract 边界用 HasTx/Strip 实现「事务内禁跨模块调用」的运行时守卫与 ctx 防火墙。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasTx

func HasTx(ctx context.Context) bool

HasTx 报告 ctx 是否处于事务中。contract 边界以此实现运行时守卫。

func ReadsAllTenants added in v0.9.0

func ReadsAllTenants(ctx context.Context) bool

ReadsAllTenants 报告 ctx 是否带「读全部租户」标记。由 Transactor 实现方 在开启事务时读取;业务代码不需要它。

func Strip

func Strip(ctx context.Context) context.Context

Strip 返回一个不携带事务句柄的 ctx(其余值、deadline、取消传播保留)。

func Value

func Value(ctx context.Context) any

Value 由数据层实现方调用:取出当前事务句柄(无事务时为 nil)。

func With

func With(ctx context.Context, handle any) context.Context

With 由 Transactor 实现方调用:把事务句柄藏进 ctx。业务代码不得调用。

func WithReadAllTenants added in v0.9.0

func WithReadAllTenants(ctx context.Context) context.Context

WithReadAllTenants 标记:由此 ctx 开启的事务以「读全部租户」模式运行—— 租户域的 RLS 对 SELECT 放开全部行,写入仍只能落在当前租户(写永远要 显式目标,"写全部"不存在)。用途是跨租户的管理面读路径:运营看全部 商户的订单、全局搜索、总览看板。

这是进程内的 ctx 值,不进 callctx 白名单:contract 防火墙会剥掉它, 事件 meta 不带它——「读全部」不跨边界传播,下游域不会因上游持有该 权限而连带放开。调用方须先过权限门(reg.Require 跨租户码)再打标记, 且标记必须在最外层 Do 之前打:嵌套事务内切模式会让 SET LOCAL 延续到 外层事务结束,实现方对此报错拒绝(见 pgtx)。

Types

type Transactor

type Transactor interface {
	Do(ctx context.Context, fn func(ctx context.Context) error) error
}

Transactor 划定一个事务边界:fn 内经 repository 接口的读写都在同一事务中, fn 返回错误或 panic 则回滚。嵌套调用 Do 由实现方以 savepoint 语义支持。

Jump to

Keyboard shortcuts

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