feat: add timeout for set command on hashes and add function to allow retry in database (#509)

This commit is contained in:
Nishant Joshi
2023-02-07 16:28:39 +05:30
committed by GitHub
parent b4da08666a
commit 2e98670aa7
9 changed files with 169 additions and 119 deletions

View File

@ -10,7 +10,7 @@ use std::fmt::Debug;
use common_utils::{
errors::CustomResult,
ext_traits::{ByteSliceExt, Encode, StringExt},
ext_traits::{AsyncExt, ByteSliceExt, Encode, StringExt},
fp_utils,
};
use error_stack::{IntoReport, ResultExt};
@ -207,11 +207,17 @@ impl super::RedisConnectionPool {
V: TryInto<RedisMap> + Debug,
V::Error: Into<fred::error::RedisError>,
{
self.pool
let output: Result<(), _> = self
.pool
.hset(key, values)
.await
.into_report()
.change_context(errors::RedisError::SetHashFailed)
.change_context(errors::RedisError::SetHashFailed);
// setting expiry for the key
output
.async_and_then(|_| self.set_expiry(key, self.config.default_hash_ttl.into()))
.await
}
#[instrument(level = "DEBUG", skip(self))]
@ -225,11 +231,20 @@ impl super::RedisConnectionPool {
V: TryInto<RedisValue> + Debug,
V::Error: Into<fred::error::RedisError>,
{
self.pool
let output: Result<HsetnxReply, _> = self
.pool
.hsetnx(key, field, value)
.await
.into_report()
.change_context(errors::RedisError::SetHashFieldFailed)
.change_context(errors::RedisError::SetHashFieldFailed);
output
.async_and_then(|inner| async {
self.set_expiry(key, self.config.default_hash_ttl.into())
.await?;
Ok(inner)
})
.await
}
#[instrument(level = "DEBUG", skip(self))]

View File

@ -100,6 +100,7 @@ impl RedisConnectionPool {
struct RedisConfig {
default_ttl: u32,
default_stream_read_count: u64,
default_hash_ttl: u32,
}
impl From<&RedisSettings> for RedisConfig {
@ -107,6 +108,7 @@ impl From<&RedisSettings> for RedisConfig {
Self {
default_ttl: config.default_ttl,
default_stream_read_count: config.stream_read_count,
default_hash_ttl: config.default_hash_ttl,
}
}
}

View File

@ -22,6 +22,8 @@ pub struct RedisSettings {
pub reconnect_delay: u32,
/// TTL in seconds
pub default_ttl: u32,
/// TTL for hash-tables in seconds
pub default_hash_ttl: u32,
pub stream_read_count: u64,
}
@ -59,6 +61,7 @@ impl Default for RedisSettings {
reconnect_delay: 5,
default_ttl: 300,
stream_read_count: 1,
default_hash_ttl: 900,
}
}
}