Skip to main content

ReconnectPolicy

Trait ReconnectPolicy 

Source
pub trait ReconnectPolicy:
    Send
    + Sync
    + Debug {
    // Required method
    fn next_delay(&self, attempt: Attempt<'_>) -> Option<Duration>;
}
Expand description

How a RabbitMqBackend paces its recovery of a lost connection.

One method, asked before every attempt including the first: return how long to wait, or None to give up. Giving up surfaces as Error::Backend on whatever operation asked for the connection, and ends consumer streams, so Worker::run returns as it did before reconnection existed.

BackoffPolicy covers the usual cases (a backoff curve and an optional attempt limit) and is the default. Implement this directly when the decision needs something a curve cannot express: a circuit breaker, a schedule, a budget shared with the rest of the process, or a different answer for an authentication failure than for a refused connection.

use std::time::Duration;
use queuey_rabbitmq::{Attempt, Rebuilding, ReconnectPolicy};

/// Waits a flat second, but never retries a consumer more than twice:
/// a subscription that fails on a live connection is usually a deleted
/// queue, and waiting does not bring one back.
#[derive(Debug)]
struct Impatient;

impl ReconnectPolicy for Impatient {
    fn next_delay(&self, attempt: Attempt<'_>) -> Option<Duration> {
        if attempt.rebuilding == Rebuilding::Consumer && attempt.failures >= 2 {
            return None;
        }
        Some(Duration::from_secs(1))
    }
}

assert_eq!(
    Impatient.next_delay(Attempt::first(Rebuilding::Connection)),
    Some(Duration::from_secs(1)),
);

Implementations are shared across tasks and consulted from several at once, hence Send + Sync. Debug is required because RabbitMqOptions is Debug, and a policy that prints as nothing would make that output a lie.

Required Methods§

Source

fn next_delay(&self, attempt: Attempt<'_>) -> Option<Duration>

How long to wait before making attempt, or None to stop trying.

Called before every attempt, attempt.failures == 0 included, so a policy controls the first try as well as the retries: returning Duration::ZERO there attempts immediately, and returning None there refuses to reconnect at all.

Must not block: it is called from the task that is holding up every other publisher waiting on the connection.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§