Skip to content

Commit

Permalink
Added more error bubbling up
Browse files Browse the repository at this point in the history
Both in sync redis as well as the cached version
  • Loading branch information
alexsnaps committed Oct 12, 2022
1 parent b4a399c commit 53b01bb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
8 changes: 7 additions & 1 deletion limitador-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,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
2 changes: 1 addition & 1 deletion 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
19 changes: 8 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?;

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 Down
8 changes: 4 additions & 4 deletions limitador/src/storage/redis/redis_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ impl CounterStorage for RedisStorage {
}

impl RedisStorage {
pub fn new(redis_url: &str) -> Self {
let conn_manager = RedisConnectionManager::new(redis_url).unwrap();
pub fn new(redis_url: &str) -> Result<Self, RedisError> {
let conn_manager = RedisConnectionManager::new(redis_url)?;
let conn_pool = Pool::builder()
.connection_timeout(Duration::from_secs(3))
.max_size(MAX_REDIS_CONNS)
.build(conn_manager)
.unwrap();

Self { conn_pool }
Ok(Self { conn_pool })
}
}

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

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

Expand Down

0 comments on commit 53b01bb

Please sign in to comment.