Make connections short-lived redis (#3509)

This commit is contained in:
Jai Agrawal 2025-04-15 15:39:13 -07:00 committed by GitHub
parent 04659a8198
commit 76be502e16
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -224,8 +224,6 @@ impl RedisPool {
+ Serialize, + Serialize,
S: Display + Clone + DeserializeOwned + Serialize + Debug, S: Display + Clone + DeserializeOwned + Serialize + Debug,
{ {
let connection = self.connect().await?.connection;
let ids = keys let ids = keys
.iter() .iter()
.map(|x| (x.to_string(), x.clone())) .map(|x| (x.to_string(), x.clone()))
@ -235,10 +233,9 @@ impl RedisPool {
return Ok(HashMap::new()); return Ok(HashMap::new());
} }
let get_cached_values = let get_cached_values = |ids: DashMap<String, I>| async move {
|ids: DashMap<String, I>,
mut connection: deadpool_redis::Connection| async move {
let slug_ids = if let Some(slug_namespace) = slug_namespace { let slug_ids = if let Some(slug_namespace) = slug_namespace {
let mut connection = self.pool.get().await?;
cmd("MGET") cmd("MGET")
.arg( .arg(
ids.iter() ids.iter()
@ -264,6 +261,7 @@ impl RedisPool {
Vec::new() Vec::new()
}; };
let mut connection = self.pool.get().await?;
let cached_values = cmd("MGET") let cached_values = cmd("MGET")
.arg( .arg(
ids.iter() ids.iter()
@ -275,10 +273,7 @@ impl RedisPool {
})) }))
.chain(slug_ids) .chain(slug_ids)
.map(|x| { .map(|x| {
format!( format!("{}_{namespace}:{x}", self.meta_namespace)
"{}_{namespace}:{x}",
self.meta_namespace
)
}) })
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
) )
@ -287,21 +282,19 @@ impl RedisPool {
.into_iter() .into_iter()
.filter_map(|x| { .filter_map(|x| {
x.and_then(|val| { x.and_then(|val| {
serde_json::from_str::<RedisValue<T, K, S>>(&val) serde_json::from_str::<RedisValue<T, K, S>>(&val).ok()
.ok()
}) })
.map(|val| (val.key.clone(), val)) .map(|val| (val.key.clone(), val))
}) })
.collect::<HashMap<_, _>>(); .collect::<HashMap<_, _>>();
Ok::<_, DatabaseError>((cached_values, connection, ids)) Ok::<_, DatabaseError>((cached_values, ids))
}; };
let current_time = Utc::now(); let current_time = Utc::now();
let mut expired_values = HashMap::new(); let mut expired_values = HashMap::new();
let (cached_values_raw, mut connection, ids) = let (cached_values_raw, ids) = get_cached_values(ids).await?;
get_cached_values(ids, connection).await?;
let mut cached_values = cached_values_raw let mut cached_values = cached_values_raw
.into_iter() .into_iter()
.filter_map(|(key, val)| { .filter_map(|(key, val)| {
@ -352,9 +345,12 @@ impl RedisPool {
.with_expiration(SetExpiry::EX(60)), .with_expiration(SetExpiry::EX(60)),
); );
}); });
let results = pipe let results = {
.query_async::<Vec<Option<i32>>>(&mut connection) let mut connection = self.pool.get().await?;
.await?;
pipe.query_async::<Vec<Option<i32>>>(&mut connection)
.await?
};
for (idx, key) in fetch_ids.into_iter().enumerate() { for (idx, key) in fetch_ids.into_iter().enumerate() {
if let Some(locked) = results.get(idx) { if let Some(locked) = results.get(idx) {
@ -487,6 +483,7 @@ impl RedisPool {
)); ));
} }
let mut connection = self.pool.get().await?;
pipe.query_async::<()>(&mut connection).await?; pipe.query_async::<()>(&mut connection).await?;
Ok(return_values) Ok(return_values)
@ -495,13 +492,13 @@ impl RedisPool {
if !subscribe_ids.is_empty() { if !subscribe_ids.is_empty() {
fetch_tasks.push(Box::pin(async { fetch_tasks.push(Box::pin(async {
let mut connection = self.pool.get().await?;
let mut interval = let mut interval =
tokio::time::interval(Duration::from_millis(100)); tokio::time::interval(Duration::from_millis(100));
let start = Utc::now(); let start = Utc::now();
loop { loop {
let results = cmd("MGET") let results = {
let mut connection = self.pool.get().await?;
cmd("MGET")
.arg( .arg(
subscribe_ids subscribe_ids
.iter() .iter()
@ -516,7 +513,8 @@ impl RedisPool {
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
) )
.query_async::<Vec<Option<String>>>(&mut connection) .query_async::<Vec<Option<String>>>(&mut connection)
.await?; .await?
};
if results.into_iter().all(|x| x.is_none()) { if results.into_iter().all(|x| x.is_none()) {
break; break;
@ -529,8 +527,8 @@ impl RedisPool {
interval.tick().await; interval.tick().await;
} }
let (return_values, _, _) = let (return_values, _) =
get_cached_values(subscribe_ids, connection).await?; get_cached_values(subscribe_ids).await?;
Ok(return_values) Ok(return_values)
})); }));