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 4 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
16 changes: 14 additions & 2 deletions 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 All @@ -134,7 +140,13 @@ impl Limiter {
cached_redis_storage = cached_redis_storage.ttl_ratio_cached_counters(cache_cfg.ttl_ratio);
cached_redis_storage = cached_redis_storage.max_cached_counters(cache_cfg.max_counters);

cached_redis_storage.build().await
match cached_redis_storage.build().await {
Ok(storage) => storage,
Err(err) => {
eprintln!("Failed to connect to Redis at {}: {}", redis_url, err);
process::exit(1)
}
}
}

#[cfg(feature = "infinispan")]
Expand Down
6 changes: 3 additions & 3 deletions limitador/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//!
//! // Custom redis URL
//! let rate_limiter = RateLimiter::new_with_storage(
//! Box::new(RedisStorage::new("redis://127.0.0.1:7777"))
//! Box::new(RedisStorage::new("redis://127.0.0.1:7777").unwrap())
//! );
//! # }
//! ```
Expand Down 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
36 changes: 29 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 All @@ -173,3 +173,25 @@ impl AsyncRedisStorage {
Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::storage::redis::AsyncRedisStorage;
use redis::ErrorKind;

#[tokio::test]
async fn errs_on_bad_url() {
let result = AsyncRedisStorage::new("cassandra://127.0.0.1:6379").await;
assert!(result.is_err());
assert_eq!(result.err().unwrap().kind(), ErrorKind::InvalidClientConfig);
}

#[tokio::test]
async fn errs_on_connection_issue() {
let result = AsyncRedisStorage::new("redis://127.0.0.1:21").await;
assert!(result.is_err());
let error = result.err().unwrap();
assert_eq!(error.kind(), ErrorKind::IoError);
assert!(error.is_connection_refusal())
}
}
41 changes: 30 additions & 11 deletions limitador/src/storage/redis/redis_cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::storage::redis::{
use crate::storage::{AsyncCounterStorage, Authorization, StorageErr};
use async_trait::async_trait;
use redis::aio::ConnectionManager;
use redis::ConnectionInfo;
use redis::{ConnectionInfo, RedisError};
use std::collections::HashSet;
use std::str::FromStr;
use std::sync::Arc;
Expand Down Expand Up @@ -174,7 +174,7 @@ impl AsyncCounterStorage for CachedRedisStorage {
}

impl CachedRedisStorage {
pub async fn new(redis_url: &str) -> Self {
pub async fn new(redis_url: &str) -> Result<Self, RedisError> {
Self::new_with_options(
redis_url,
Some(Duration::from_secs(DEFAULT_FLUSHING_PERIOD_SEC)),
Expand All @@ -191,12 +191,9 @@ impl CachedRedisStorage {
max_cached_counters: usize,
ttl_cached_counters: Duration,
ttl_ratio_cached_counters: u64,
) -> Self {
let redis_conn_manager = ConnectionManager::new(
redis::Client::open(ConnectionInfo::from_str(redis_url).unwrap()).unwrap(),
)
.await
.unwrap();
) -> Result<Self, RedisError> {
let info = ConnectionInfo::from_str(redis_url)?;
let redis_conn_manager = ConnectionManager::new(redis::Client::open(info).unwrap()).await?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: maybe expect instead of unwrap for consistency?


let async_redis_storage =
AsyncRedisStorage::new_with_conn_manager(redis_conn_manager.clone());
Expand All @@ -222,13 +219,13 @@ impl CachedRedisStorage {
.ttl_ratio_cached_counter(ttl_ratio_cached_counters)
.build();

Self {
Ok(Self {
cached_counters: Mutex::new(cached_counters),
batcher_counter_updates: batcher,
redis_conn_manager,
async_redis_storage,
batching_is_enabled: flushing_period.is_some(),
}
})
}

async fn values_with_ttls(
Expand Down Expand Up @@ -302,7 +299,7 @@ impl CachedRedisStorageBuilder {
self
}

pub async fn build(self) -> CachedRedisStorage {
pub async fn build(self) -> Result<CachedRedisStorage, RedisError> {
CachedRedisStorage::new_with_options(
&self.redis_url,
self.flushing_period,
Expand All @@ -313,3 +310,25 @@ impl CachedRedisStorageBuilder {
.await
}
}

#[cfg(test)]
mod tests {
use crate::storage::redis::CachedRedisStorage;
use redis::ErrorKind;

#[tokio::test]
async fn errs_on_bad_url() {
let result = CachedRedisStorage::new("cassandra://127.0.0.1:6379").await;
assert!(result.is_err());
assert_eq!(result.err().unwrap().kind(), ErrorKind::InvalidClientConfig);
}

#[tokio::test]
async fn errs_on_connection_issue() {
let result = CachedRedisStorage::new("redis://127.0.0.1:21").await;
assert!(result.is_err());
let error = result.err().unwrap();
assert_eq!(error.kind(), ErrorKind::IoError);
assert!(error.is_connection_refusal())
}
}
37 changes: 28 additions & 9 deletions limitador/src/storage/redis/redis_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,21 @@ impl CounterStorage for RedisStorage {
}

impl RedisStorage {
pub fn new(redis_url: &str) -> Self {
let conn_manager = RedisConnectionManager::new(redis_url).unwrap();
let conn_pool = Pool::builder()
pub fn new(redis_url: &str) -> Result<Self, String> {
let conn_manager = match RedisConnectionManager::new(redis_url) {
Ok(conn_manager) => conn_manager,
Err(err) => {
return Err(err.to_string());
}
};
match Pool::builder()
.connection_timeout(Duration::from_secs(3))
.max_size(MAX_REDIS_CONNS)
.build(conn_manager)
.unwrap();

Self { conn_pool }
{
Ok(conn_pool) => Ok(Self { conn_pool }),
Err(err) => Err(err.to_string()),
}
}
}

Expand Down Expand Up @@ -190,7 +196,7 @@ impl ManageConnection for RedisConnectionManager {

impl Default for RedisStorage {
fn default() -> Self {
Self::new(DEFAULT_REDIS_URL)
Self::new(DEFAULT_REDIS_URL).unwrap()
}
}

Expand All @@ -199,8 +205,21 @@ mod test {
use crate::storage::redis::RedisStorage;

#[test]
fn create_default() {
let _ = RedisStorage::default();
fn errs_on_bad_url() {
let result = RedisStorage::new("cassandra://127.0.0.1:6379");
assert!(result.is_err());
assert_eq!(result.err().unwrap(), "Redis URL did not parse".to_string())
}

#[test]
fn errs_on_connection_issue() {
// this panic!s And I really don't see how to bubble the redis error back up:
// r2d2 consumes it
// RedisError are not publicly constructable
// So using String as error type… sad
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #131

let result = RedisStorage::new("redis://127.0.0.1:21");
assert!(result.is_err());
assert!(result.err().unwrap().contains("Connection refused"));
}

#[test]
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