pub struct DatabaseClock;Expand description
The database’s own clock: #[inbox(clock = DatabaseClock)] makes the statements read it
(statement_timestamp() on Postgres, UTC_TIMESTAMP(6) on MySQL) instead of binding the
host’s time.
A table in the lease form reads the host’s clock: every settlement names the expiry its claim
wrote, so the crate computes that expiry, and #[field(locked_until)] beside
clock = DatabaseClock does not compile.
§Examples
use std::time::Duration;
use chrono::{DateTime, Utc};
use ruststream_sqlx::prelude::*;
use ruststream_sqlx::DatabaseClock;
use serde::Deserialize;
use sqlx::PgPool;
/// Tasks written by hosts whose clocks drift: every claim and every delay reads one clock.
#[derive(Inbox, sqlx::FromRow)]
#[inbox(table = "sync_jobs", clock = DatabaseClock)]
pub struct SyncJob {
#[field(id, generated)]
id: i64,
#[field(retry_after, generated)]
retry_after: DateTime<Utc>,
#[field(payload)]
payload: Vec<u8>,
}
#[derive(Deserialize)]
struct Change {
record: u64,
}
#[subscriber(InboxQueue::<SyncJob>::new("sync"))]
async fn sync(change: &Change) -> HandlerOutcome {
if push(change).await {
return HandlerOutcome::ack();
}
// Ten seconds by the database's clock, whichever host claimed the row.
HandlerOutcome::retry_after(Duration::from_secs(10))
}
pub fn app(pool: PgPool) -> RustStream {
RustStream::new(AppInfo::new("sync", "0.1.0")).with_broker(SqlxBroker::new(pool), |b| {
b.include(sync);
})
}Trait Implementations§
Source§impl Clone for DatabaseClock
impl Clone for DatabaseClock
impl Copy for DatabaseClock
Source§impl Debug for DatabaseClock
impl Debug for DatabaseClock
Source§impl Default for DatabaseClock
impl Default for DatabaseClock
impl Eq for DatabaseClock
Source§impl Hash for DatabaseClock
impl Hash for DatabaseClock
Source§impl PartialEq for DatabaseClock
impl PartialEq for DatabaseClock
impl StructuralPartialEq for DatabaseClock
Source§impl TimeSource for DatabaseClock
impl TimeSource for DatabaseClock
Auto Trait Implementations§
impl Freeze for DatabaseClock
impl RefUnwindSafe for DatabaseClock
impl Send for DatabaseClock
impl Sync for DatabaseClock
impl Unpin for DatabaseClock
impl UnsafeUnpin for DatabaseClock
impl UnwindSafe for DatabaseClock
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more