pub enum BranchResult {
Success,
Failure,
Ongoing,
Unknown,
}Expand description
分支被调用后的结论。这四态的区分是整个系统的命门。
Variants§
Success
HTTP 200 —— 成功
Failure
HTTP 409 —— 业务明确要求回滚。只有这个才触发补偿
Ongoing
HTTP 425 —— 还在处理中,别当失败
Unknown
网络错误、超时、5xx —— 结果未知
绝不能当成失败:超时的时候对方可能已经成功了,贸然补偿会造成不一致。 正确做法是重试,直到拿到 Success 或 Failure。
Implementations§
Source§impl BranchResult
impl BranchResult
Sourcepub fn from_http(status: u16, body: &str) -> Self
pub fn from_http(status: u16, body: &str) -> Self
从 HTTP 状态码 + 响应体判定。响应体里的 dtm_result 字段优先于状态码,
这样业务方用 200 返回 {"dtm_result":"FAILURE"} 也能表达失败。
Sourcepub fn from_grpc(code: i32) -> Self
pub fn from_grpc(code: i32) -> Self
从 gRPC 状态码判定。取值是 gRPC 规范里的标准编号,跟 DTM 的
dtmgrpc 对齐,这样两边的业务服务可以互换。
§这个映射为什么是这几个码
HTTP 那边靠 409/425 表达「明确失败」和「还在处理」,gRPC 没有这两个码, 得从 16 个标准码里各挑一个不会被基础设施误用的:
| gRPC 码 | 语义 | 对应 HTTP |
|---|---|---|
OK(0) | 成功 | 200 |
ABORTED(10) | 业务明确要求回滚 | 409 |
FAILED_PRECONDITION(9) | 还在处理,别当失败 | 425 |
| 其它全部 | 结果未知,重试 | 5xx / 超时 |
关键在最后一行。UNAVAILABLE(14)、DEADLINE_EXCEEDED(4)、INTERNAL(13)
这些都算未知而不是失败 —— 它们恰恰是网络抖动和超时会产生的码,
而超时的时候对方可能已经成功了。这跟 HTTP 侧「超时不等于失败」是同一条命门。
特别注意 CANCELLED(1) 和 DEADLINE_EXCEEDED(4):调用方自己取消/超时
产生的码,绝不能当成业务失败 —— 那是我们这边放弃了,不是对方拒绝了。
Trait Implementations§
Source§impl Clone for BranchResult
impl Clone for BranchResult
Source§fn clone(&self) -> BranchResult
fn clone(&self) -> BranchResult
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreimpl Copy for BranchResult
Source§impl Debug for BranchResult
impl Debug for BranchResult
impl Eq for BranchResult
Source§impl PartialEq for BranchResult
impl PartialEq for BranchResult
impl StructuralPartialEq for BranchResult
Auto Trait Implementations§
impl Freeze for BranchResult
impl RefUnwindSafe for BranchResult
impl Send for BranchResult
impl Sync for BranchResult
impl Unpin for BranchResult
impl UnsafeUnpin for BranchResult
impl UnwindSafe for BranchResult
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more