跳到主要内容
本页尚未翻译

下面显示的是英文原文。

Prelude

One glob, ruststream_gcp_pubsub::prelude, brings the core prelude, the broker, the descriptor, the publish policy and the ordering step.

use ruststream_gcp_pubsub::prelude::*; is the one import a Pub/Sub service writes. It carries the core's own prelude, so #[subscriber], RustStream, HandlerOutcome and Out come with it. It adds PubSubBroker, the descriptor GooglePubSub and the live PubSubPublisher.

The publish policy comes under the name every broker crate uses: Publish is PubSubPublish. So a mount site reads the same on every broker. Two broker preludes in one file collide on this name; the prefixed type at the crate root tells them apart.

A handler body bounds its slots with capability traits, such as Out<impl Publisher>, and names no broker type. The one exception is a body that names an ordering key on one publish. The ordering_key step needs PubSubOrdering in scope and the slot bounded Out<impl Publisher<Options = PubSubPublishOptions>>, both from this prelude. The constants PARTITION_KEY_HEADER and DELIVERY_ATTEMPT_HEADER stay at the crate root.

Example​

The descriptor, the broker and the reply policy all come from the one glob.

/// The descriptor, the broker and the policy all come from the one glob.
#[subscriber(GooglePubSub::new("orders-workers").create_with_topic("orders"), reply)]
async fn receipt(order: &Order) -> Receipt {
Receipt { order_id: order.id }
}

#[ruststream::app]
fn app() -> impl App {
RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(
PubSubBroker::new("my-project"),
|b| {
b.include(receipt).out_reply(Publish::default());
},
)
}