wait
wait(duration) sets how long one receive waits for a message, 20 seconds by default and at most; a longer wait stops the service at startup.
The wait is the WaitTimeSeconds of each ReceiveMessage: the long poll.
A receive returns as soon as a message arrives, and returns empty when the wait runs out.
An empty receive hands the handler nothing.
A longer wait means fewer empty receives, and SQS bills per request.
The default is the SQS maximum of 20 seconds, so a subscription that names no wait already polls as long as SQS allows.
A wait over 20 seconds stops the service at startup with SqsError::InvalidQueue.
A zero wait is short polling: each receive returns at once, with what a sample of the queue's servers holds.
The step is on SqsQueue, and on the mount chain through SqsSubscription::wait.
Example
Each receive on alerts waits up to five seconds.
- Macros
- Manual
/// Each receive on `alerts` waits up to five seconds and returns as soon as an alert arrives.
#[subscriber(SqsQueue::new("alerts").wait(Duration::from_secs(5)))]
async fn page(alert: &Alert) -> HandlerOutcome {
tracing::info!(alert.id, "on-call paged");
HandlerOutcome::ack()
}
/// Each receive on `alerts` waits up to five seconds and returns as soon as an alert arrives.
fn alerts() -> SqsQueue {
SqsQueue::new("alerts").wait(Duration::from_secs(5))
}
fn service(broker: SqsBroker) -> impl App {
RustStream::new(AppInfo::new("alerts", "0.1.0")).with_broker(broker, |b| {
b.include(subscriber("alerts", Page).build().map_source(|_| alerts()));
})
}