Skip to main content

Api

Struct Api 

Source
pub struct Api {
    pub store: Store,
    /* private fields */
}

Fields§

§store: Store

Implementations§

Source§

impl Api

Source

pub fn new(store: Store) -> Self

Source

pub fn with_inline_driver(self, d: Driver) -> Self

开启「提交后直接开推」。

§省掉的是那次抢占往返

老流程:提交方写完事务就返回,推进器再 lock_one_due 抢一次才能推。 那次抢占每笔事务都要付,在 Redis 上是一次 Lua 往返 —— 实测它就是 saga 落后 DTM 的主要原因(saga 只有一次客户端请求,摊不薄)。

新流程:建事务的那条写入里顺便把租约占在自己手上owner=自己next_cron_time=现在+租约),写成功就等于抢到了, 直接推。零额外往返。

§跟 DTM 的差别:我们不阻塞提交

DTM 是在 submit 请求里同步把事务推完,客户端要一直等。这里是 spawn 出去推,提交立刻返回 —— 省掉往返的同时保住了提交延迟。

§代价

租约一占就是 lease 秒。如果进程在「写完」和「推完」之间挂了, 这笔要等租约到期才会被别的实例接手,而不是下一个 tick。 这跟「推进器抢到之后崩了」是同一种情形,不是新引入的风险。

Source

pub fn new_gid(&self) -> String

时间戳 + 进程内计数。生产建议客户端直接用业务单号当 gid —— 那样天然幂等,重试不会变成两笔

Source

pub async fn submit( &self, gid: &str, trans_type: &str, steps: &[SagaStep], ) -> Result<()>

提交。

重复提交同一个 gid 必须成功而不是报错 —— 客户端网络抖动重试时 返回错误会让它以为没受理,然后换个 gid 再来一次,就成了两笔。

Source

pub async fn prepare( &self, gid: &str, trans_type: &str, actions: &[String], query_prepared: &str, grace_secs: Option<i64>, ) -> Result<()>

第一阶段。msg 建 prepared 事务 + 正向分支;tcc / xa 只建空事务。

Source

pub async fn register_branch(&self, r: &RegisterBranch) -> Result<()>

分支登记。必须先登记再做一阶段:反过来的话一阶段成功但登记失败, TC 就不知道有这个分支,回滚时不会处理它 —— TCC 是预留资源永久泄漏, XA 更糟,会留下一个永久持锁的 prepared 事务。

Source

pub async fn abort(&self, gid: &str) -> Result<()>

主动中止,触发逆序补偿

Source

pub async fn retry(&self, gid: &str) -> Result<()>

立刻重试:把下次调度时间提到现在,并清掉退避累积。

只是「排到队首」,不跳过任何安全检查 —— 分支该幂等还是要幂等。 终态事务不能重试(没意义,而且会让它重新变成活跃事务)。

Source

pub async fn query(&self, gid: &str) -> Result<TransView>

Source

pub async fn list_recent(&self, limit: i64) -> Vec<TransView>

Trait Implementations§

Source§

impl Clone for Api

Source§

fn clone(&self) -> Api

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

Auto Trait Implementations§

§

impl !RefUnwindSafe for Api

§

impl !UnwindSafe for Api

§

impl Freeze for Api

§

impl Send for Api

§

impl Sync for Api

§

impl Unpin for Api

§

impl UnsafeUnpin for Api

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more