refactor(redis): add redis commands required for success rate based routing (#5610)

This commit is contained in:
Chethan Rao
2024-08-14 18:22:33 +05:30
committed by GitHub
parent 27096e71e4
commit 340714ce1b
2 changed files with 73 additions and 1 deletions

View File

@ -15,7 +15,7 @@ use common_utils::{
}; };
use error_stack::{report, ResultExt}; use error_stack::{report, ResultExt};
use fred::{ use fred::{
interfaces::{HashesInterface, KeysInterface, SetsInterface, StreamsInterface}, interfaces::{HashesInterface, KeysInterface, ListInterface, SetsInterface, StreamsInterface},
prelude::RedisErrorKind, prelude::RedisErrorKind,
types::{ types::{
Expiration, FromRedis, MultipleIDs, MultipleKeys, MultipleOrderedPairs, MultipleStrings, Expiration, FromRedis, MultipleIDs, MultipleKeys, MultipleOrderedPairs, MultipleStrings,
@ -371,6 +371,19 @@ impl super::RedisConnectionPool {
Ok(hsetnx) Ok(hsetnx)
} }
#[instrument(level = "DEBUG", skip(self))]
pub async fn increment_field_in_hash(
&self,
key: &str,
field: &str,
increment: i64,
) -> CustomResult<usize, errors::RedisError> {
self.pool
.hincrby(self.add_prefix(key), field, increment)
.await
.change_context(errors::RedisError::IncrementHashFieldFailed)
}
#[instrument(level = "DEBUG", skip(self))] #[instrument(level = "DEBUG", skip(self))]
pub async fn hscan( pub async fn hscan(
&self, &self,
@ -630,6 +643,55 @@ impl super::RedisConnectionPool {
}) })
} }
#[instrument(level = "DEBUG", skip(self))]
pub async fn append_elements_to_list<V>(
&self,
key: &str,
elements: V,
) -> CustomResult<(), errors::RedisError>
where
V: TryInto<MultipleValues> + Debug + Send,
V::Error: Into<fred::error::RedisError> + Send,
{
self.pool
.rpush(self.add_prefix(key), elements)
.await
.change_context(errors::RedisError::AppendElementsToListFailed)
}
#[instrument(level = "DEBUG", skip(self))]
pub async fn get_list_elements(
&self,
key: &str,
start: i64,
stop: i64,
) -> CustomResult<Vec<String>, errors::RedisError> {
self.pool
.lrange(self.add_prefix(key), start, stop)
.await
.change_context(errors::RedisError::GetListElementsFailed)
}
#[instrument(level = "DEBUG", skip(self))]
pub async fn get_list_length(&self, key: &str) -> CustomResult<usize, errors::RedisError> {
self.pool
.llen(self.add_prefix(key))
.await
.change_context(errors::RedisError::GetListLengthFailed)
}
#[instrument(level = "DEBUG", skip(self))]
pub async fn lpop_list_elements(
&self,
key: &str,
count: Option<usize>,
) -> CustomResult<Vec<String>, errors::RedisError> {
self.pool
.lpop(self.add_prefix(key), count)
.await
.change_context(errors::RedisError::PopListElementsFailed)
}
// Consumer Group API // Consumer Group API
#[instrument(level = "DEBUG", skip(self))] #[instrument(level = "DEBUG", skip(self))]

View File

@ -68,4 +68,14 @@ pub enum RedisError {
OnMessageError, OnMessageError,
#[error("Got an unknown result from redis")] #[error("Got an unknown result from redis")]
UnknownResult, UnknownResult,
#[error("Failed to append elements to list in Redis")]
AppendElementsToListFailed,
#[error("Failed to get list elements in Redis")]
GetListElementsFailed,
#[error("Failed to get length of list")]
GetListLengthFailed,
#[error("Failed to pop list elements in Redis")]
PopListElementsFailed,
#[error("Failed to increment hash field in Redis")]
IncrementHashFieldFailed,
} }