Documentation
¶
Overview ¶
Package contract 实现跨模块契约调用的边界语义。
无论对端是进程内实现还是远程 client,每次契约方法调用都必须经过 Call: 由合约仓库生成的 wrapper/client 在方法体内调用它。Call 保证两种部署形态下 语义一致(ServiceWeaver 教训的反向落地——不隐藏网络边界,而是让进程内调用 也表现得像一次可失败的远程调用):
- 运行时守卫:事务内发起契约调用直接失败(apperr.CodeTxBoundary)。 静态分析对这条规则只能提高绕过成本,运行时守卫在任何测试里都会暴露。
- ctx 防火墙:剥离事务句柄与请求作用域值,只保留取消/超时传播、 OpenTelemetry span 与 callctx.Meta 白名单——与跨网络传播的信息一致。
- 协作式超时:每次调用有独立 deadline(默认 5s),通过 ctx 传给进程内 或远程实现;发起时 ctx 已取消/超时的调用直接失败(CodeUnavailable), 不进入实现——跨网络时这种调用本来就发不出去。已经启动的同步 fn 不会被 goroutine+select 强杀;若忽略 ctx,Call 可能在 deadline 后才返回,写操作 的调用方必须按成功或未知结果用幂等/查询收敛。
- 错误规范化:任何错误折叠为 *apperr.Error,错误身份 = 错误码。
每次调用还自动产出一条 span 与一条 appkit.contract.call.duration 观测 (标签 system/method/outcome,失败时附错误码)——契约边界是模块间唯一的 交互面,这里的延迟与错误率就是模块间的 SLI,不该等到出事才补埋点。
Index ¶
- Constants
- func Call[T any](ctx context.Context, system, method string, timeout time.Duration, ...) (T, error)
- func Firewall(ctx context.Context) context.Context
- func NewSecureHTTPClient(base string, opts SecureClientOptions) (*http.Client, error)
- type SecureClientOptions
- type ServiceCredential
- type ServiceCredentialProvider
- type ServiceCredentialProviderFunc
- type ServiceScope
Constants ¶
const DefaultTimeout = 5 * time.Second
DefaultTimeout 是单次契约调用的默认超时。
const HeaderServiceAuthorization = "X-Service-Authorization"
HeaderServiceAuthorization carries a service credential, never a user token.
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 ¶
Firewall 返回剥离了全部 ctx 值(含事务句柄)的 ctx,保留取消/超时、 当前 span 与 callctx.Meta。
保留的这三样恰好是跨网络调用里本来也会传的东西(deadline、traceparent、 request/tenant 头),一件不多——所以进程内调用拿不到任何比远程调用更多的 上下文,两种部署形态下代码行为一致。
func NewSecureHTTPClient ¶ added in v0.9.1
func NewSecureHTTPClient(base string, opts SecureClientOptions) (*http.Client, error)
NewSecureHTTPClient constructs an HTTPS-only client bound to base's origin. Use it through a generated NewSecureClient so contract.Call also supplies the transaction guard, timeout and error normalization. Configuration and provider code are trusted application code, not a sandbox. Do not mutate the returned client's fields to replace its security transport or redirect policy.
Types ¶
type SecureClientOptions ¶ added in v0.9.1
type SecureClientOptions struct {
Audience string
Credentials ServiceCredentialProvider
HTTPClient *http.Client
}
SecureClientOptions configures authenticated, origin-bound contract HTTP. Audience and Credentials are mandatory. HTTPClient may customize timeouts and a standard *http.Transport (including trusted root certificates). Custom round trippers, TLS dialers/protocol handlers, insecure TLS and cookie jars are rejected. Requests with trailers are rejected rather than forwarding hidden credentials or silently dropping a caller's integrity trailer. The supplied client/transport configuration is copied. Redirects are always refused; a supplied CheckRedirect callback is not used.
type ServiceCredential ¶ added in v0.9.1
ServiceCredential contains a bare service JWT and its actual signed expiry. Providers must not return a user token or invent a later expiry for a token.
type ServiceCredentialProvider ¶ added in v0.9.1
type ServiceCredentialProvider interface {
ServiceCredential(context.Context, ServiceScope) (ServiceCredential, error)
}
ServiceCredentialProvider obtains a fresh, explicitly authorized service credential for each HTTP attempt. Implementations must honor cancellation and be safe for concurrent calls. The transport does not cache credentials.
type ServiceCredentialProviderFunc ¶ added in v0.9.1
type ServiceCredentialProviderFunc func(context.Context, ServiceScope) (ServiceCredential, error)
ServiceCredentialProviderFunc adapts an explicit provider function.
func (ServiceCredentialProviderFunc) ServiceCredential ¶ added in v0.9.1
func (f ServiceCredentialProviderFunc) ServiceCredential(ctx context.Context, scope ServiceScope) (ServiceCredential, error)
type ServiceScope ¶ added in v0.9.1
ServiceScope is an explicit delegation request, not proof of authorization. The provider must authorize its audience and every nonempty scope before signing. Tenant and partition originate in the contract context whitelist; user actors, merchant IDs and incoming HTTP credentials are not forwarded.