diff options
author | Vika <vika@fireburn.ru> | 2021-08-05 13:38:42 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2021-08-05 15:10:43 +0300 |
commit | a2c37d319b22849fce034ed6658fe1f13c8737cf (patch) | |
tree | 3f499f8b17c01d723f29ea94aa37968c750a6f69 | |
parent | 5ec8d86738f5c16befcec9721576cbb3d194fe42 (diff) | |
download | kittybox-a2c37d319b22849fce034ed6658fe1f13c8737cf.tar.zst |
Trying to mitigate and log more about the HTTP 500s
-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()), |