Retry after
retry_after publishes a copy of the delivery after a delay: to its own topic for MqttTopic, to a topic the mount site names for MqttFilter.
The server moves nothing on a negative answer, so the copy is a publish made by the service.
A subscription on a topic is reached again by publishing to that topic, and the descriptor reports it, so the mount site names nothing.
A subscription on a filter reads many topics and reports no address.
There the mount names the destination with .out_retry(policy).to(name), and a chain that names none does not compile.
The copy comes back to the subscription when the named topic matches the filter.
The copy leaves through the policy of out_retry, such as MqttPublish, and carries the retry count in the RETRY_COUNT_HEADER header.
On a device the copy waits in the store of the mount, and .retry_capacity::<N>() after the mount sets how many wait at once.
Example
A topic subscription, asked for again in five seconds.
- Macros
- Manual
/// The valve is still moving: the job is asked for again in five seconds. A copy that carries its
/// retry count is the second attempt, and it is acknowledged.
#[subscriber(MqttTopic::new("jobs/run"))]
async fn run(job: &Job, ctx: &mut Context<'_>) -> HandlerOutcome {
let _ = job.id;
match ctx.headers().get_str(RETRY_COUNT_HEADER) {
Some(_) => HandlerOutcome::ack(),
None => HandlerOutcome::retry_after(Duration::from_secs(5)),
}
}
#[ruststream::app(runtime = embassy)]
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
// Room for two copies waiting at once.
EmbeddedApp::new().with_broker(broker, |b| b.include(run).retry_capacity::<2>())
}
/// The valve is still moving: the job is asked for again in five seconds. A copy that carries its
/// retry count is the second attempt, and it is acknowledged.
struct Run;
impl Handle<Job> for Run {
fn handle(
&self,
_job: &Job,
_outs: &(),
ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
ready(match ctx.headers().get_str(RETRY_COUNT_HEADER) {
Some(_) => Ok(()),
None => Err(HandlerOutcome::retry_after(Duration::from_secs(5))),
})
}
}
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
// Room for two copies waiting at once.
EmbeddedApp::new().with_broker(broker, |b| {
b.include(
subscriber("jobs/run", Run)
.map_source(|_| MqttTopic::new("jobs/run"))
.build(),
)
.retry_capacity::<2>()
})
}
A filter
The mount names jobs/retry, which the filter jobs/+ matches.
- Macros
- Manual
/// A filter reads many topics and addresses none of them, so the mount site names where a copy
/// goes: `jobs/retry` matches the filter, and the copy comes back to the handler.
#[subscriber(MqttFilter::new("jobs/+"))]
async fn reconcile(job: &Job, ctx: &mut Context<'_>) -> HandlerOutcome {
let _ = job.id;
match ctx.headers().get_str(RETRY_COUNT_HEADER) {
Some(_) => HandlerOutcome::ack(),
None => HandlerOutcome::retry_after(Duration::from_secs(5)),
}
}
#[ruststream::app(runtime = embassy)]
fn filter_app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| {
b.include(reconcile)
.out_retry(MqttPublish::default())
.to("jobs/retry")
.retry_capacity::<2>()
})
}
/// A filter reads many topics and addresses none of them, so the mount site names where a copy
/// goes: `jobs/retry` matches the filter, and the copy comes back to the handler.
struct Reconcile;
impl Handle<Job> for Reconcile {
fn handle(
&self,
_job: &Job,
_outs: &(),
ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
ready(match ctx.headers().get_str(RETRY_COUNT_HEADER) {
Some(_) => Ok(()),
None => Err(HandlerOutcome::retry_after(Duration::from_secs(5))),
})
}
}
fn filter_app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| {
b.include(
subscriber("jobs/+", Reconcile)
.map_source(|_| MqttFilter::new("jobs/+"))
.build(),
)
.out_retry(MqttPublish::default())
.to("jobs/retry")
.retry_capacity::<2>()
})
}