contract

package
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: MIT Imports: 10 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 contract 实现跨模块契约调用的边界语义。

无论对端是进程内实现还是远程 client,每次契约方法调用都必须经过 Call: 由合约仓库生成的 wrapper/client 在方法体内调用它。Call 保证两种部署形态下 语义一致(ServiceWeaver 教训的反向落地——不隐藏网络边界,而是让进程内调用 也表现得像一次可失败的远程调用):

  1. 运行时守卫:事务内发起契约调用直接失败(apperr.CodeTxBoundary)。 静态分析对这条规则只能提高绕过成本,运行时守卫在任何测试里都会暴露。
  2. ctx 防火墙:剥离事务句柄与请求作用域值,只保留取消/超时传播、 OpenTelemetry span 与 callctx.Meta 白名单——与跨网络传播的信息一致。
  3. 超时:每次调用有独立超时(默认 5s),进程内实现同样受约束; 发起时 ctx 已取消/超时的调用直接失败(CodeUnavailable),不进入实现—— 跨网络时这种调用本来就发不出去。
  4. 错误规范化:任何错误折叠为 *apperr.Error,错误身份 = 错误码。

每次调用还自动产出一条 span 与一条 appkit.contract.call.duration 观测 (标签 system/method/outcome,失败时附错误码)——契约边界是模块间唯一的 交互面,这里的延迟与错误率就是模块间的 SLI,不该等到出事才补埋点。

Index

Constants

View Source
const DefaultTimeout = 5 * time.Second

DefaultTimeout 是单次契约调用的默认超时。

Variables

This section is empty.

Functions

func Call

func Call[T any](ctx context.Context, system, method string, timeout time.Duration, fn func(ctx context.Context) (T, error)) (T, error)

Call 执行一次契约调用。system/method 用于 span 命名与错误归属, 形如 Call(ctx, "ledger", "PostEntries", 0, func(ctx) (T, error) { ... })。 timeout 为 0 时取 DefaultTimeout。

func Firewall

func Firewall(ctx context.Context) context.Context

Firewall 返回剥离了全部 ctx 值(含事务句柄)的 ctx,保留取消/超时、 当前 span 与 callctx.Meta。

保留的这三样恰好是跨网络调用里本来也会传的东西(deadline、traceparent、 request/tenant 头),一件不多——所以进程内调用拿不到任何比远程调用更多的 上下文,两种部署形态下代码行为一致。

Types

This section is empty.

Jump to

Keyboard shortcuts

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