pub struct QueueRuntime { /* private fields */ }Expand description
队列运行时
管理 consumer lifecycle:启动一个消费循环任务,监听 CancellationToken 优雅退出。
§设计
- 不持有
JoinHandle:消费任务由调用方持有,本结构仅提供启动入口 - ack 策略:handler 返回 Ok 时自动 ack;Err 时不 ack(消息留在 in_flight)
- 退出策略:监听
token.cancelled(),当前正在处理的消息会等待完成
§用法
ⓘ
use sz_rust_core::runtime::queue::{QueueRuntime, QueueRuntimeConfig, QueueConsumer};
use sz_orm_queue::{InMemoryQueue, MessageQueue, Message};
use async_trait::async_trait;
use std::sync::Arc;
use tokio_util::sync::CancellationToken;
struct MyConsumer;
#[async_trait]
impl QueueConsumer for MyConsumer {
async fn handle(&self, msg: &Message) -> Result<(), QueueConsumerError> {
println!("got: {:?}", msg.payload);
Ok(())
}
}
let queue = Arc::new(InMemoryQueue::new(1000));
let runtime = QueueRuntime::new(
QueueRuntimeConfig::new("orders"),
queue,
);
let token = CancellationToken::new();
let handle = runtime.start(Arc::new(MyConsumer), token.clone());
// ... 业务运行 ...
token.cancel();
let _ = handle.await;Implementations§
Source§impl QueueRuntime
impl QueueRuntime
Sourcepub fn new(config: QueueRuntimeConfig, queue: Arc<dyn MessageQueue>) -> Self
pub fn new(config: QueueRuntimeConfig, queue: Arc<dyn MessageQueue>) -> Self
创建队列运行时
Sourcepub fn start<C>(
&self,
consumer: Arc<C>,
token: CancellationToken,
) -> JoinHandle<()> ⓘwhere
C: QueueConsumer + 'static,
pub fn start<C>(
&self,
consumer: Arc<C>,
token: CancellationToken,
) -> JoinHandle<()> ⓘwhere
C: QueueConsumer + 'static,
启动消费循环(返回 JoinHandle,调用方持有以控制 lifecycle)
§行为
- 每
poll_interval_ms毫秒调用queue.consume(topic)拉取消息 - 收到消息后调用
consumer.handle(&msg) - handler 返回 Ok → 自动
queue.ack(msg.id) - handler 返回 Err → 跳过 ack(消息留在 in_flight)
- 监听
token.cancelled(),收到信号后停止拉取新消息
Sourcepub fn config(&self) -> &QueueRuntimeConfig
pub fn config(&self) -> &QueueRuntimeConfig
获取配置
Auto Trait Implementations§
impl !RefUnwindSafe for QueueRuntime
impl !UnwindSafe for QueueRuntime
impl Freeze for QueueRuntime
impl Send for QueueRuntime
impl Sync for QueueRuntime
impl Unpin for QueueRuntime
impl UnsafeUnpin for QueueRuntime
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.