1 year ago

#387146

test-img

Jeyko22

How can I use the Connection Manager on Redis?

extern crate redis;

use redis::aio::ConnectionManager;
use std::env;

pub async fn connect() -> redis::aio::ConnectionManager {
    let redis_host_name =
        env::var("REDIS_HOSTNAME").expect("Missing environment variable REDIS_HOSTNAME");

    let redis_tls = env::var("REDIS_TLS").map(|redis_tls| redis_tls == "1").unwrap_or(false);

    let uri_scheme = match redis_tls {
        true => "rediss",
        false => "redis",
    };

    let redis_conn_url = format!("{}://{}", uri_scheme, redis_host_name);

    let client = redis::Client::open(redis_conn_url)
        .expect("Invalid connection URL");

    let redis = ConnectionManager::new(client.clone()).await.unwrap();

    redis
}

I have a pub/sub system with Redis in Rust. For now it works perfectly creating the connections separately (one to publish and another to subscribe), but if the redis server goes down, it does not try to reconnect. To try to solve it, I have seen the option of the ConnectionManager of the following link:

https://docs.rs/redis/0.21.5/redis/aio/struct.ConnectionManager.html

Which implements the reconnection. The problem is that I don't see documentation to be able to implement it. With the above function I return a ConnectionManager, which I then want to pass through the AppData in the routes with ActixWeb. But I don't understand how I use those connections to publish and subscribe to the channel. Since it tells me that those functions don't exist in the ConnectionManager. Any ideas?

rust

redis

actix-web

0 Answers

Your Answer

Accepted video resources