From a2c37d319b22849fce034ed6658fe1f13c8737cf Mon Sep 17 00:00:00 2001 From: Vika Date: Thu, 5 Aug 2021 13:38:42 +0300 Subject: Trying to mitigate and log more about the HTTP 500s --- src/database/redis/mod.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'src/database') 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) -> Option< #[async_trait] impl Storage for RedisStorage { async fn get_setting<'a>(&self, setting: &'a str, 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)))?; Ok(conn .hget::(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::(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 { - 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> { - 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>(&"posts", url) .await? @@ -155,7 +155,7 @@ impl Storage for RedisStorage { } async fn get_channels(&self, user: &User) -> 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 channels = conn .smembers::>("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>(&"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::(&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()), -- cgit 1.4.1