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

下面显示的是英文原文。

Payload

A table with a payload column is in payload mode: the claim lends the column's bytes to the codec, and the handler takes the decoded message.

#[field(payload)] marks the column, by hand .payload(..) with PayloadRow, which names the column's type and lends its bytes. A claim reads the row, and the delivery lends those bytes to the codec as they are, with no copy. The codec is the mount's: JSON by default, or any codec of the core the router or the scope names. The bytes are the service's own: binary and empty payloads reach the handler byte for byte, and a publish through a route writes them as the codec encoded them.

A row whose bytes do not decode into the handler's type is settled by the subscription's decode policy before the handler runs. A handler that takes a row instead of a message mounts only on a table in row mode.

Example​

A signup queue in payload mode: the handler takes the decoded Signup, and the acknowledged row keeps the bytes it was decoded from.

#[derive(Inbox, sqlx::FromRow)]
#[inbox(table = "event_jobs")]
struct EventJob {
#[field(id, generated)]
id: i64,
#[field(processed_at)]
processed_at: Option<DateTime<Utc>>,
#[field(locked_until)]
locked_until: Option<DateTime<Utc>>,
// The message bytes, decoded by the codec of the mount: JSON unless it names another.
#[field(payload)]
payload: Vec<u8>,
}

#[subscriber(InboxQueue::<EventJob>::new("signups"))]
async fn welcome(signup: &Signup) -> HandlerOutcome {
tracing::info!(email = %signup.email, "welcoming");
HandlerOutcome::ack()
}