Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement a simple reconnect logic? #41

Open
Ks89 opened this issue Sep 17, 2022 · 1 comment
Open

How to implement a simple reconnect logic? #41

Ks89 opened this issue Sep 17, 2022 · 1 comment

Comments

@Ks89
Copy link

Ks89 commented Sep 17, 2022

Is there a suggested way (or even better a runnable example) to implement reconnect in these cases:

  • it starts, but it isn't able to connect to RabbitMQ
  • it's already running, but suddenly the connection is not available

Thanks

@ragyabraham
Copy link

pub struct RabbitConnect {
    pub host: String,
    pub port: u16,
    pub username: String,
    pub password: String,
}

pub async fn connect_rabbitmq(connection_details: &RabbitConnect) -> Result<Connection> {
    let mut res = Connection::insecure_open(
        format!(
            "amqp://{}:{}@{}:{}",
            connection_details.username,
            connection_details.password,
            connection_details.host,
            connection_details.port
        )
        .as_str(),
    );

    while res.is_err() {
        println!("trying to connect after error");
        sleep(Duration::from_millis(2000)).await;
        res = Connection::insecure_open(
            format!(
                "amqp://{}:{}@{}:{}",
                connection_details.username,
                connection_details.password,
                connection_details.host,
                connection_details.port
            )
            .as_str(),
        );
    }
    res
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants