diff options
-rw-r--r-- | src/database/redis/mod.rs | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/src/database/redis/mod.rs b/src/database/redis/mod.rs index 205af76..00932de 100644 --- a/src/database/redis/mod.rs +++ b/src/database/redis/mod.rs @@ -107,31 +107,31 @@ fn filter_post(mut post: serde_json::Value, user: &'_ Option<String>) -> Option< #[async_trait] impl Storage for RedisStorage { async fn get_setting<'a>(&self, setting: &'a str, user: &'a str) -> Result<String> { - let mut conn = self.redis.get().await?; + let mut conn = self.redis.get().await.map_err(|e| StorageError::with_source(ErrorKind::Backend, "Error getting a connection from the pool", Box::new(e)))?; Ok(conn .hget::<String, &str, String>(format!("settings_{}", user), setting) .await?) } async fn set_setting<'a>(&self, setting: &'a str, user: &'a str, value: &'a str) -> Result<()> { - let mut conn = self.redis.get().await?; + let mut conn = self.redis.get().await.map_err(|e| StorageError::with_source(ErrorKind::Backend, "Error getting a connection from the pool", Box::new(e)))?; Ok(conn .hset::<String, &str, &str, ()>(format!("settings_{}", user), setting, value) .await?) } async fn delete_post<'a>(&self, url: &'a str) -> Result<()> { - let mut conn = self.redis.get().await?; + let mut conn = self.redis.get().await.map_err(|e| StorageError::with_source(ErrorKind::Backend, "Error getting a connection from the pool", Box::new(e)))?; Ok(conn.hdel::<&str, &str, ()>("posts", url).await?) } async fn post_exists(&self, url: &str) -> Result<bool> { - let mut conn = self.redis.get().await?; + let mut conn = self.redis.get().await.map_err(|e| StorageError::with_source(ErrorKind::Backend, "Error getting a connection from the pool", Box::new(e)))?; Ok(conn.hexists::<&str, &str, bool>(&"posts", url).await?) } async fn get_post(&self, url: &str) -> Result<Option<serde_json::Value>> { - let mut conn = self.redis.get().await?; + let mut conn = self.redis.get().await.map_err(|e| StorageError::with_source(ErrorKind::Backend, "Error getting a connection from the pool", Box::new(e)))?; match conn .hget::<&str, &str, Option<String>>(&"posts", url) .await? @@ -155,7 +155,7 @@ impl Storage for RedisStorage { } async fn get_channels(&self, user: &User) -> Result<Vec<MicropubChannel>> { - let mut conn = self.redis.get().await?; + let mut conn = self.redis.get().await.map_err(|e| StorageError::with_source(ErrorKind::Backend, "Error getting a connection from the pool", Box::new(e)))?; let channels = conn .smembers::<String, Vec<String>>("channels_".to_string() + user.me.as_str()) .await?; @@ -182,7 +182,7 @@ impl Storage for RedisStorage { } async fn put_post<'a>(&self, post: &'a serde_json::Value, user: &'a str) -> Result<()> { - let mut conn = self.redis.get().await?; + let mut conn = self.redis.get().await.map_err(|e| StorageError::with_source(ErrorKind::Backend, "Error getting a connection from the pool", Box::new(e)))?; let key: &str; match post["properties"]["uid"][0].as_str() { Some(uid) => key = uid, @@ -239,7 +239,8 @@ impl Storage for RedisStorage { let mut feed; match conn .hget::<&str, &str, Option<String>>(&"posts", url) - .await? + .await + .map_err(|e| StorageError::with_source(ErrorKind::Backend, "Error getting a connection from the pool", Box::new(e)))? { Some(post) => feed = serde_json::from_str::<serde_json::Value>(&post)?, None => return Ok(None), @@ -294,7 +295,7 @@ impl Storage for RedisStorage { Err(err) => Err(StorageError::from(err)) } } - Err(err) => Err(StorageError::from(err)) + Err(err) => Err(StorageError::with_source(ErrorKind::Backend, "Error getting a connection from the pool", Box::new(err))) } }) // TODO: determine the optimal value for this buffer @@ -325,7 +326,7 @@ impl Storage for RedisStorage { } async fn update_post<'a>(&self, mut url: &'a str, update: serde_json::Value) -> Result<()> { - let mut conn = self.redis.get().await?; + let mut conn = self.redis.get().await.map_err(|e| StorageError::with_source(ErrorKind::Backend, "Error getting a connection from the pool", Box::new(e)))?; if !conn .hexists::<&str, &str, bool>("posts", url) .await @@ -358,6 +359,7 @@ impl RedisStorage { Ok(client) => Ok(Self { redis: Pool::builder() .max_open(20) + .max_idle(5) .build(RedisConnectionManager::new(client)), }), Err(e) => Err(e.into()), |