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

fix(error): Bubble error up to main when connecting to Redis fails #130

Merged
merged 6 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion limitador-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ impl Limiter {
}

async fn storage_using_async_redis(redis_url: &str) -> AsyncRedisStorage {
AsyncRedisStorage::new(redis_url).await
match AsyncRedisStorage::new(redis_url).await {
Ok(storage) => storage,
Err(err) => {
eprintln!("Failed to connect to Redis at {}: {}", redis_url, err);
process::exit(1)
}
}
}

async fn storage_using_redis_and_local_cache(
Expand Down
4 changes: 2 additions & 2 deletions limitador/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
//!
//! async {
//! let rate_limiter = AsyncRateLimiter::new_with_storage(
//! Box::new(AsyncRedisStorage::new("redis://127.0.0.1:7777").await)
//! Box::new(AsyncRedisStorage::new("redis://127.0.0.1:7777").await.unwrap())
//! );
//! };
//! # }
Expand All @@ -171,7 +171,7 @@
//!
//! async {
//! let rate_limiter = AsyncRateLimiter::new_with_storage(
//! Box::new(AsyncRedisStorage::new("redis://127.0.0.1:7777").await)
//! Box::new(AsyncRedisStorage::new("redis://127.0.0.1:7777").await.unwrap())
//! );
//! rate_limiter.add_limit(limit);
//! };
Expand Down
14 changes: 7 additions & 7 deletions limitador/src/storage/redis/redis_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::storage::keys::*;
use crate::storage::redis::scripts::SCRIPT_UPDATE_COUNTER;
use crate::storage::{AsyncCounterStorage, Authorization, StorageErr};
use async_trait::async_trait;
use redis::AsyncCommands;
use redis::{AsyncCommands, RedisError};
use std::collections::HashSet;
use std::str::FromStr;
use std::time::Duration;
Expand Down Expand Up @@ -145,14 +145,14 @@ impl AsyncCounterStorage for AsyncRedisStorage {
}

impl AsyncRedisStorage {
pub async fn new(redis_url: &str) -> Self {
Self {
pub async fn new(redis_url: &str) -> Result<Self, RedisError> {
let info = ConnectionInfo::from_str(redis_url)?;
Ok(Self {
conn_manager: ConnectionManager::new(
redis::Client::open(ConnectionInfo::from_str(redis_url).unwrap()).unwrap(),
redis::Client::open(info).expect("Somehow couldn't create Redis client!"),
eguzki marked this conversation as resolved.
Show resolved Hide resolved
)
.await
.unwrap(),
}
.await?,
})
}

pub fn new_with_conn_manager(conn_manager: ConnectionManager) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions limitador/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ macro_rules! test_with_all_storage_impls {
#[tokio::test]
#[serial]
async fn [<$function _with_async_redis>]() {
let storage = AsyncRedisStorage::new("redis://127.0.0.1:6379").await;
let storage = AsyncRedisStorage::new("redis://127.0.0.1:6379").await.expect("We need a Redis running locally");
eguzki marked this conversation as resolved.
Show resolved Hide resolved
storage.clear().await.unwrap();
let rate_limiter = AsyncRateLimiter::new_with_storage(
Box::new(storage)
);
AsyncRedisStorage::new("redis://127.0.0.1:6379").await.clear().await.unwrap();
AsyncRedisStorage::new("redis://127.0.0.1:6379").await.expect("We need a Redis running locally").clear().await.unwrap();
$function(&mut TestsLimiter::new_from_async_impl(rate_limiter)).await;
}

Expand Down