pub struct Middleware(/* private fields */);Expand description
A condition checked before a job’s attempt; see Job::middleware.
use renox::queue::Middleware;
impl Job for SyncStock {
const NAME: &'static str = "sync-stock";
fn middleware(&self) -> Vec<Middleware> {
vec![
// One sync per shop at a time; others wait 10 s and try again.
Middleware::without_overlapping(format!("shop:{}", self.shop_id))
.release_after(Duration::from_secs(10)),
// The supplier's API allows 60 calls a minute.
Middleware::rate_limited("supplier-api", 60, Duration::from_secs(60)),
]
}
async fn handle(self, _ctx: JobContext) -> Result { Ok(()) }
}Both use the cache (CACHE_STORE): with memory, they hold within one
process; use database when several processes run workers.
Implementations§
Source§impl Middleware
impl Middleware
Sourcepub fn without_overlapping(key: impl Into<String>) -> Self
pub fn without_overlapping(key: impl Into<String>) -> Self
One job with this key at a time; others are put back for 5 seconds
(see Middleware::release_after).
Sourcepub fn rate_limited(key: impl Into<String>, max: u32, per: Duration) -> Self
pub fn rate_limited(key: impl Into<String>, max: u32, per: Duration) -> Self
At most max attempts with this key per per (a fixed window);
the rest are put back until the window ends.
Sourcepub fn release_after(self, wait: Duration) -> Self
pub fn release_after(self, wait: Duration) -> Self
How long a job waits before trying again when it would overlap.
Trait Implementations§
Source§impl Clone for Middleware
impl Clone for Middleware
Auto Trait Implementations§
impl Freeze for Middleware
impl RefUnwindSafe for Middleware
impl Send for Middleware
impl Sync for Middleware
impl Unpin for Middleware
impl UnsafeUnpin for Middleware
impl UnwindSafe for Middleware
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
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