Skip to main content

BranchResult

Enum BranchResult 

Source
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

Source

pub fn from_http(status: u16, body: &str) -> Self

从 HTTP 状态码 + 响应体判定。响应体里的 dtm_result 字段优先于状态码, 这样业务方用 200 返回 {"dtm_result":"FAILURE"} 也能表达失败。

Source

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

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Copy for BranchResult

Source§

impl Debug for BranchResult

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for BranchResult

Source§

impl PartialEq for BranchResult

Source§

fn eq(&self, other: &BranchResult) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for BranchResult

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.