Ниже показан английский оригинал.
Ordering
On a subscription with message ordering, messages that share an ordering key reach a subscriber in the order they were published.
An ordering key is a string a publish carries, up to 1 KB.
A subscription orders deliveries only when it was created with message ordering on, a setting that cannot change afterwards.
A subscription create_with_topic creates has it on; one created elsewhere keeps its own setting.
The order holds for messages of one key published to one region, so publishers of one key use the same locational endpoint.
When a message of a key is redelivered, the messages published after it under that key come again too, acknowledged ones included.
With a dead-letter policy on the subscription, the order may not hold.
A publish names its key in one of three places, and the first one present wins.
The ordering_key step, or a transform, writes it into the message's settings.
The partition-key header is the portable spelling.
The policy's default key applies where neither named one.
A delivery reports the key it arrived under in the partition-key header.
Example
Three events of one order, published under its key, arrive in that order. The test runs the same body in process and against the emulator.
- Macros
- Manual
/// The subscription `create_with_topic` creates has message ordering on, so the events of one
/// order arrive in the order they were published.
#[subscriber(
GooglePubSub::new("ordering-order-events-workers").create_with_topic("ordering-order-events")
)]
async fn track(event: &OrderEvent, ctx: &mut Context<'_>) -> HandlerOutcome {
let key = ctx
.headers()
.get_str(PARTITION_KEY_HEADER)
.unwrap_or_default();
tracing::info!(event.order_id, event.step, key, "order event");
HandlerOutcome::ack()
}
/// The subscription `create_with_topic` creates has message ordering on, so the events of one
/// order arrive in the order they were published.
struct Track;
impl Handle<OrderEvent> for Track {
fn handle(
&self,
event: &OrderEvent,
_outs: &(),
ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
let key = ctx
.headers()
.get_str(PARTITION_KEY_HEADER)
.unwrap_or_default();
tracing::info!(event.order_id, event.step, key, "order event");
ready(Ok(()))
}
}
fn order_events() -> GooglePubSub {
GooglePubSub::new("ordering-order-events-workers").create_with_topic("ordering-order-events")
}