Skip to main content

LeaseRow

Trait LeaseRow 

Source
pub trait LeaseRow: InboxRow {
    type Lease: QueueTime;
}
Expand description

A queue row taken by lease: its struct has a #[field(locked_until)] field.

A claim writes the lease’s expiry into locked_until and commits at once, so the lease, not a transaction, holds the row while the handler runs. The expiry it wrote is the delivery’s ownership token: a settlement takes effect only while the row still holds it. While the handler runs, the subscription extends the lease each half lease, and each extension’s expiry becomes the token. The crate implements it for every table in the lease form, a struct with the field or a description that sets InboxSpec::lease, and a subscription of such a table can set its own lease (InboxQueue::lease).

§Examples

use std::time::Duration;

use chrono::{DateTime, Utc};
use ruststream_sqlx::prelude::*;
use serde::Deserialize;
use sqlx::PgPool;

// `locked_until` makes `Report` a lease row: a claim writes the lease's expiry there and commits.
#[derive(Inbox, sqlx::FromRow)]
#[inbox(table = "report_jobs")]
pub struct Report {
    #[field(id, generated)]
    id: i64,
    #[field(locked_until)]
    locked_until: Option<DateTime<Utc>>,
    #[field(payload)]
    payload: Vec<u8>,
}

#[derive(Deserialize)]
struct Request {
    month: u32,
}

// A lease row's subscription sets its own lease; the handler may run for minutes without a
// transaction open.
#[subscriber(InboxQueue::<Report>::new("reports").lease(Duration::from_secs(300)))]
async fn render(request: &Request) -> HandlerOutcome {
    tracing::info!(month = request.month, "rendering");
    HandlerOutcome::ack()
}

pub fn app(pool: PgPool) -> RustStream {
    RustStream::new(AppInfo::new("reports", "0.1.0")).with_broker(SqlxBroker::new(pool), |b| {
        b.include(render);
    })
}

Required Associated Types§

Source

type Lease: QueueTime

The time locked_until holds: the lease’s expiry, and the delivery’s ownership token.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<Row, Settings> LeaseRow for Row
where Row: InboxRow + InboxTable<Table = InboxSpec<Settings>>, Settings: Declaration, Settings::Form: LeaseAxis,

Source§

type Lease = <<Settings as Declaration>::Form as LeaseAxis>::Time