Skip to content

Commit f014c14

Browse files
author
Ludo Galabru
committed
fix: add exp backoff
1 parent e6c5d0e commit f014c14

File tree

1 file changed

+8
-2
lines changed
  • components/chainhook-sdk/src/hord/db

1 file changed

+8
-2
lines changed

components/chainhook-sdk/src/hord/db/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use chainhook_types::{
1111
use dashmap::DashMap;
1212
use fxhash::FxHasher;
1313
use hiro_system_kit::slog;
14+
use rand::{thread_rng, Rng};
1415

1516
use rocksdb::DB;
1617
use rusqlite::{Connection, OpenFlags, ToSql};
@@ -254,12 +255,17 @@ pub fn find_lazy_block_at_block_height(
254255
// let mut read_options = rocksdb::ReadOptions::default();
255256
// read_options.fill_cache(true);
256257
// read_options.set_verify_checksums(false);
258+
let mut backoff: f64 = 1.0;
259+
let mut rng = thread_rng();
260+
257261
loop {
258262
match blocks_db.get(block_height.to_be_bytes()) {
259263
Ok(Some(res)) => return Some(LazyBlock::new(res)),
260264
_ => {
261265
attempt += 1;
262-
std::thread::sleep(std::time::Duration::from_secs(2));
266+
backoff = 2.0 * backoff + (backoff * rng.gen_range(0.0..1.0));
267+
let duration = std::time::Duration::from_millis((backoff * 1_000.0) as u64);
268+
std::thread::sleep(duration);
263269
if attempt > retry {
264270
return None;
265271
}
@@ -965,7 +971,7 @@ pub fn retrieve_satoshi_point_using_lazy_storage(
965971
traversals_cache.insert((ordinal_block_number, txid.clone()), tx);
966972
(sats_ranges, inscription_offset_cross_outputs)
967973
}
968-
None => return Err(format!("block #{ordinal_block_number} not in database")),
974+
None => return Err(format!("txid not in block #{ordinal_block_number}")),
969975
},
970976
},
971977
};

0 commit comments

Comments
 (0)