Skip to content

Commit 8ecf645

Browse files
authored
Merge pull request #1 from commonprefix/fix/exponential-backoff-in-redis-read
fix(redis): exponential backoff in get_cost_by_message_id
2 parents 7a0fcfe + 2cc4909 commit 8ecf645

File tree

2 files changed

+65
-41
lines changed

2 files changed

+65
-41
lines changed

Cargo.lock

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/redis.rs

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use relayer_core::utils::ThreadSafe;
33
use serde::{Deserialize, Serialize};
44
use std::fmt::{Display, Formatter};
55
use std::sync::Arc;
6+
use std::time::Duration;
7+
use tokio::time::sleep;
68
use tracing::{debug, error};
79

810
#[derive(Clone)]
@@ -38,32 +40,54 @@ impl CostCacheTrait for CostCache {
3840
) -> Result<u64, anyhow::Error> {
3941
let key = format!("cost:{}:{}", transaction_type, message_id);
4042
let mut conn = self.conn.clone();
41-
match conn.get::<_, Option<String>>(&key).await {
42-
Ok(Some(serialized)) => {
43-
if let Ok(cost) = serialized.parse::<u64>() {
44-
debug!("Cost for key {} is {}", key, cost);
45-
return Ok(cost);
46-
} else {
47-
error!("Failed to parse cost for key {}: {}", key, serialized);
48-
return Err(anyhow::anyhow!(
49-
"Failed to parse cost for key {}: {}",
50-
key,
51-
serialized
52-
));
43+
let max_retries = 5;
44+
let mut backoff_duration = Duration::from_millis(500);
45+
46+
for attempt in 0..max_retries {
47+
match conn.get::<_, Option<String>>(&key).await {
48+
Ok(Some(serialized)) => {
49+
if let Ok(cost) = serialized.parse::<u64>() {
50+
debug!("Cost for key {} is {}", key, cost);
51+
return Ok(cost);
52+
} else {
53+
error!("Failed to parse cost for key {}: {}", key, serialized);
54+
return Err(anyhow::anyhow!(
55+
"Failed to parse cost for key {}: {}",
56+
key,
57+
serialized
58+
));
59+
}
60+
}
61+
_ => {
62+
if attempt < max_retries - 1 {
63+
debug!(
64+
"Failed to get cost from Redis for key {} (attempt {}/{}): Key not found. Retrying in {:?}...",
65+
key,
66+
attempt + 1,
67+
max_retries,
68+
backoff_duration
69+
);
70+
sleep(backoff_duration).await;
71+
backoff_duration *= 2;
72+
} else {
73+
error!(
74+
"Failed to get cost from Redis for key {} after {} attempts: Key not found in Redis",
75+
key, max_retries
76+
);
77+
return Err(anyhow::anyhow!(
78+
"Failed to get cost for key {} after {} attempts: Key not found in Redis",
79+
key,
80+
max_retries
81+
));
82+
}
5383
}
54-
}
55-
Ok(None) => {
56-
error!("Failed to get cost for key {}: Key not found in Redis", key);
57-
return Err(anyhow::anyhow!(
58-
"Failed to get cost for key {}: Key not found in Redis",
59-
key
60-
));
61-
}
62-
Err(e) => {
63-
error!("Failed to get context from Redis for key {}: {}", key, e);
64-
return Err(anyhow::anyhow!("Failed to get cost for key {}: {}", key, e));
6584
}
6685
}
86+
87+
Err(anyhow::anyhow!(
88+
"Failed to get cost for key {}: Max retries exceeded",
89+
key
90+
))
6791
}
6892
}
6993

0 commit comments

Comments
 (0)