Skip to main content

Unlock

Trait Unlock 

Source
pub trait Unlock<DB: Database>: InboxRow {
    // Required method
    fn unlock(
        conn: &mut DB::Connection,
        key: &str,
    ) -> impl Future<Output = Result<bool, Error>> + Send;
}
Expand description

Releases the advisory lock on a delivery’s key: the unlock event of the advisory lock form, which a table names as its own beside Lock.

A settlement calls it after its statement, on the connection that holds the delivery, and the connection goes back to the pool once it answered true. An unlock that answers false or fails closes the connection instead, which ends the session and its locks. A delivery dropped unsettled runs it before its connection closes, and shutdown runs it for every delivery in work.

§Examples

use ruststream_sqlx::{Inbox, Lock, Unlock};
use sqlx::{PgConnection, Postgres};

#[derive(Inbox, sqlx::FromRow)]
#[inbox(table = "jobs", advisory_lock = "jobs-{id}", custom(lock, unlock))]
pub struct Job {
    #[field(id)]
    id: i64,
    #[field(payload)]
    payload: Vec<u8>,
}

// MSSQL releases with `sp_releaseapplock`; Postgres's session lock stands in for it here.
impl Unlock<Postgres> for Job {
    async fn unlock(conn: &mut PgConnection, key: &str) -> Result<bool, sqlx::Error> {
        sqlx::query_scalar("SELECT pg_advisory_unlock(hashtext($1))")
            .bind(key)
            .fetch_one(conn)
            .await
    }
}

impl Lock<Postgres> for Job {
    async fn lock(conn: &mut PgConnection, key: &str) -> Result<bool, sqlx::Error> {
        sqlx::query_scalar("SELECT pg_try_advisory_lock(hashtext($1))")
            .bind(key)
            .fetch_one(conn)
            .await
    }
}

Required Methods§

Source

fn unlock( conn: &mut DB::Connection, key: &str, ) -> impl Future<Output = Result<bool, Error>> + Send

Releases the advisory lock on key the session of conn holds: true when the session held it.

§Errors

The database’s error; the connection closes, which ends the session and its locks.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§