Skip to content

Commit 5262c16

Browse files
committed
fix: retry insert_payload on failure
1 parent 4f21011 commit 5262c16

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

testnet/stacks-node/src/event_dispatcher.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,42 @@ impl EventObserver {
355355
Ok(())
356356
}
357357

358+
/// Insert a payload into the database, retrying on failure.
359+
fn insert_payload_with_retry(
360+
conn: &Connection,
361+
url: &str,
362+
payload: &serde_json::Value,
363+
timeout: Duration,
364+
) {
365+
let mut attempts = 0i64;
366+
let mut backoff = Duration::from_millis(100); // Initial backoff duration
367+
let max_backoff = Duration::from_secs(5); // Cap the backoff duration
368+
369+
loop {
370+
match Self::insert_payload(conn, url, payload, timeout) {
371+
Ok(_) => {
372+
// Successful insert, break the loop
373+
return;
374+
}
375+
Err(err) => {
376+
// Log the error, then retry after a delay
377+
warn!("Failed to insert payload into event observer database: {:?}", err;
378+
"backoff" => ?backoff,
379+
"attempts" => attempts
380+
);
381+
382+
// Wait for the backoff duration
383+
sleep(backoff);
384+
385+
// Increase the backoff duration (with exponential backoff)
386+
backoff = std::cmp::min(backoff.saturating_mul(2), max_backoff);
387+
388+
attempts = attempts.saturating_add(1);
389+
}
390+
}
391+
}
392+
}
393+
358394
fn get_pending_payloads(
359395
conn: &Connection,
360396
) -> Result<Vec<(i64, String, serde_json::Value, u64)>, db_error> {
@@ -524,8 +560,7 @@ impl EventObserver {
524560
Connection::open(db_path).expect("Failed to open database for event observer");
525561

526562
// Insert the new payload into the database
527-
Self::insert_payload(&conn, &full_url, payload, self.timeout)
528-
.expect("Failed to insert payload into event observer database");
563+
Self::insert_payload_with_retry(&conn, &full_url, payload, self.timeout);
529564

530565
// Process all pending payloads
531566
Self::process_pending_payloads(&conn);

0 commit comments

Comments
 (0)