maxcountryman / tower-sessions-stores

🚃 Previously bundled session stores for `tower-sessions`.
https://github.com/maxcountryman/tower-sessions
MIT License
27 stars 19 forks source link

Does RedisStore not support Sentinels? #7

Closed Syphixs closed 7 months ago

Syphixs commented 7 months ago

I am testing a simple Sentinel Config like this:

    let redis_config = RedisConfig {
        server: ServerConfig::Sentinel {
            hosts: vec![fredServer::new("127.0.0.1", 26379)],
            service_name: "mymaster".to_string(),
            username: None,
            password: None,
        },
        ..Default::default()
    };

but I don't seem to get the example to work. The normal centralized version works no problem. I am certain that it's not a connection problem as i can connect via redis-cli just fine.

    let redis_config = RedisConfig {
        server: ServerConfig::Centralized {
          server: fredServer::new("127.0.0.1", 6379),
        },
        ..Default::default()
    };
maxcountryman commented 7 months ago

The underlying crate here is fred so sentinel support would depend on how fred supports that.

Do you know if fred supports sentinel?

maxcountryman commented 7 months ago

I don't really know Sentinel, but this seems to be working for me:

diff --git a/redis-store/Cargo.toml b/redis-store/Cargo.toml
index 826b47b..c89392e 100644
--- a/redis-store/Cargo.toml
+++ b/redis-store/Cargo.toml
@@ -26,6 +26,7 @@ tower-sessions = "0.10.0"
 tokio = { version = "1.32.0", features = ["full"] }
 tokio-test = "0.4.3"
 serde = "1"
+fred = { version = "7", features = ["sentinel-client"] }

 [[example]]
 name = "redis"
diff --git a/redis-store/examples/redis.rs b/redis-store/examples/redis.rs
index c3dcccf..2556794 100644
--- a/redis-store/examples/redis.rs
+++ b/redis-store/examples/redis.rs
@@ -4,7 +4,7 @@ use axum::{response::IntoResponse, routing::get, Router};
 use serde::{Deserialize, Serialize};
 use time::Duration;
 use tower_sessions::{Expiry, Session, SessionManagerLayer};
-use tower_sessions_redis_store::{fred::prelude::*, RedisStore};
+use tower_sessions_redis_store::{fred::prelude::*, fred::types::Server, RedisStore};

 const COUNTER_KEY: &str = "counter";

@@ -19,12 +19,22 @@ async fn handler(session: Session) -> impl IntoResponse {

 #[tokio::main]
 async fn main() -> Result<(), Box<dyn std::error::Error>> {
-    let pool = RedisPool::new(RedisConfig::default(), None, None, None, 6)?;
-
-    let redis_conn = pool.connect();
-    pool.wait_for_connect().await?;
-
-    let session_store = RedisStore::new(pool);
+    let config = RedisConfig {
+        server: ServerConfig::Sentinel {
+            service_name: "mymaster".into(),
+            hosts: vec![
+                Server::new("localhost", 26379),
+                Server::new("localhost", 26380),
+            ],
+        },
+        ..Default::default()
+    };
+
+    let client = Builder::from_config(config).build()?;
+    let redis_conn = client.connect();
+    client.wait_for_connect().await?;
+
+    let session_store = RedisStore::new(client);
     let session_layer = SessionManagerLayer::new(session_store)
         .with_secure(false)
         .with_expiry(Expiry::OnInactivity(Duration::seconds(10)));
Syphixs commented 7 months ago

Thanks for the response. I will test it after work, if it's not working i'll reopen again. Cheers mate