-
Notifications
You must be signed in to change notification settings - Fork 4
Major Refactor Of Event Callbacks #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
18de6fb
smart retry
LeoPatOZ efe892d
Merge branch 'event-channeling' into smart-retry
LeoPatOZ c5beaff
Merge branch 'event-channeling' into smart-retry
LeoPatOZ 4453dc3
fix broken test
LeoPatOZ c72d34b
Merge branch 'event-channeling' into smart-retry
LeoPatOZ 1ce35da
clippy
LeoPatOZ ab202cd
Merge branch 'event-channeling' into smart-retry
LeoPatOZ 375c11a
remove clone
LeoPatOZ 8a39db4
refactor
LeoPatOZ b655b11
init refactor
LeoPatOZ 9556268
remove callback config
LeoPatOZ e9e2cfe
refactor fixed retry
LeoPatOZ 8ead188
added default strategy
LeoPatOZ 6f1bd5a
use fixed retry
LeoPatOZ 24a10f4
must use
LeoPatOZ 6abe76d
split callback into own trait
LeoPatOZ 5c8fa1d
formate
LeoPatOZ 1e974e6
reformat
LeoPatOZ b7b9d8f
remove option
LeoPatOZ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| use std::{sync::Arc, time::Duration}; | ||
|
|
||
| use alloy::rpc::types::Log; | ||
| use async_trait::async_trait; | ||
| use tracing::warn; | ||
|
|
||
| use crate::callback::EventCallback; | ||
|
|
||
| use super::CallbackStrategy; | ||
|
|
||
| pub const BACK_OFF_MAX_RETRIES: u64 = 5; | ||
| pub const BACK_OFF_MAX_DELAY_MS: u64 = 200; | ||
|
|
||
| #[derive(Clone, Copy, Debug)] | ||
| pub struct FixedRetryConfig { | ||
| pub max_attempts: u64, | ||
| pub delay_ms: u64, | ||
| } | ||
|
|
||
| impl Default for FixedRetryConfig { | ||
| fn default() -> Self { | ||
| Self { max_attempts: BACK_OFF_MAX_RETRIES, delay_ms: BACK_OFF_MAX_DELAY_MS } | ||
| } | ||
| } | ||
|
|
||
| pub struct FixedRetryStrategy { | ||
| cfg: FixedRetryConfig, | ||
| } | ||
|
|
||
| impl FixedRetryStrategy { | ||
| #[must_use] | ||
| pub fn new(cfg: FixedRetryConfig) -> Self { | ||
| Self { cfg } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl CallbackStrategy for FixedRetryStrategy { | ||
| async fn execute( | ||
| &self, | ||
| callback: &Arc<dyn EventCallback + Send + Sync>, | ||
| log: &Log, | ||
| ) -> anyhow::Result<()> { | ||
| match callback.on_event(log).await { | ||
| Ok(()) => Ok(()), | ||
| Err(mut last_err) => { | ||
| let attempts = self.cfg.max_attempts.max(1); | ||
| for _ in 1..attempts { | ||
| warn!( | ||
| delay_ms = self.cfg.delay_ms, | ||
| max_attempts = attempts, | ||
| "Callback failed: retrying after fixed delay" | ||
| ); | ||
| tokio::time::sleep(Duration::from_millis(self.cfg.delay_ms)).await; | ||
| match callback.on_event(log).await { | ||
| Ok(()) => return Ok(()), | ||
| Err(e) => last_err = e, | ||
| } | ||
| } | ||
| Err(last_err) | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use alloy::rpc::types::Log; | ||
| use async_trait::async_trait; | ||
|
|
||
| use crate::callback::EventCallback; | ||
|
|
||
| pub mod fixed_retry; | ||
| pub mod state_sync_aware; | ||
|
|
||
| pub use fixed_retry::{BACK_OFF_MAX_RETRIES, FixedRetryConfig, FixedRetryStrategy}; | ||
| pub use state_sync_aware::{StateSyncAwareStrategy, StateSyncConfig}; | ||
|
|
||
| #[async_trait] | ||
| pub trait CallbackStrategy: Send + Sync { | ||
| async fn execute( | ||
| &self, | ||
| callback: &Arc<dyn EventCallback + Send + Sync>, | ||
| log: &Log, | ||
| ) -> anyhow::Result<()>; | ||
0xNeshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| use std::{cmp, sync::Arc, time::Duration}; | ||
|
|
||
| use alloy::rpc::types::Log; | ||
| use async_trait::async_trait; | ||
| use tracing::{info, warn}; | ||
|
|
||
| use crate::{FixedRetryConfig, callback::EventCallback}; | ||
|
|
||
| use super::{CallbackStrategy, fixed_retry::FixedRetryStrategy}; | ||
|
|
||
| pub const STATE_SYNC_RETRY_INTERVAL: Duration = Duration::from_secs(30); | ||
| pub const STATE_SYNC_RETRY_MAX_INTERVAL: Duration = Duration::from_secs(120); | ||
| pub const STATE_SYNC_RETRY_MAX_ELAPSED: Duration = Duration::from_secs(600); | ||
| pub const STATE_SYNC_RETRY_MULTIPLIER: f64 = 1.5; | ||
|
|
||
| #[derive(Clone, Copy, Debug)] | ||
| pub struct StateSyncConfig { | ||
| pub initial_interval: Duration, | ||
| pub max_interval: Duration, | ||
| pub max_elapsed: Duration, | ||
| pub multiplier: f64, | ||
| } | ||
|
|
||
| impl Default for StateSyncConfig { | ||
| fn default() -> Self { | ||
| Self { | ||
| initial_interval: STATE_SYNC_RETRY_INTERVAL, | ||
| max_interval: STATE_SYNC_RETRY_MAX_INTERVAL, | ||
| max_elapsed: STATE_SYNC_RETRY_MAX_ELAPSED, | ||
| multiplier: STATE_SYNC_RETRY_MULTIPLIER, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub struct StateSyncAwareStrategy { | ||
| inner: FixedRetryStrategy, | ||
| cfg: StateSyncConfig, | ||
| } | ||
|
|
||
| impl Default for StateSyncAwareStrategy { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl StateSyncAwareStrategy { | ||
| #[must_use] | ||
| pub fn new() -> Self { | ||
| Self { | ||
| inner: FixedRetryStrategy::new(FixedRetryConfig::default()), | ||
| cfg: StateSyncConfig::default(), | ||
| } | ||
| } | ||
|
|
||
| #[must_use] | ||
| pub fn with_state_sync_config(mut self, cfg: StateSyncConfig) -> Self { | ||
| self.cfg = cfg; | ||
| self | ||
| } | ||
|
|
||
| #[must_use] | ||
| pub fn with_fixed_retry_config(mut self, cfg: super::fixed_retry::FixedRetryConfig) -> Self { | ||
| self.inner = FixedRetryStrategy::new(cfg); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl CallbackStrategy for StateSyncAwareStrategy { | ||
| async fn execute( | ||
| &self, | ||
| callback: &Arc<dyn EventCallback + Send + Sync>, | ||
| log: &Log, | ||
| ) -> anyhow::Result<()> { | ||
| match callback.on_event(log).await { | ||
| Ok(()) => Ok(()), | ||
| Err(first_err) => { | ||
| if is_missing_trie_node_error(&first_err) { | ||
| // state sync aware retry path | ||
| let mut delay = self.cfg.initial_interval; | ||
| let start = tokio::time::Instant::now(); | ||
0xNeshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| info!(initial_interval = ?self.cfg.initial_interval, max_interval = ?self.cfg.max_interval, | ||
| max_elapsed = ?self.cfg.max_elapsed, "Starting state-sync aware retry"); | ||
| let mut last_err: anyhow::Error = first_err; | ||
| loop { | ||
| if start.elapsed() >= self.cfg.max_elapsed { | ||
| return Err(last_err); | ||
| } | ||
| tokio::time::sleep(delay).await; | ||
| match callback.on_event(log).await { | ||
| Ok(()) => return Ok(()), | ||
| Err(e) => { | ||
| last_err = e; | ||
| let next_secs = delay.as_secs_f64() * self.cfg.multiplier; | ||
| let next = Duration::from_secs_f64(next_secs); | ||
| delay = cmp::min(self.cfg.max_interval, next); | ||
| let elapsed = start.elapsed(); | ||
| warn!(next_delay = ?delay, elapsed = ?elapsed, error = %last_err, | ||
| "State-sync retry operation failed: will retry"); | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| // Fixed retry for regular errors | ||
| self.inner.execute(callback, log).await | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn is_missing_trie_node_error(err: &anyhow::Error) -> bool { | ||
| let s = err.to_string().to_lowercase(); | ||
| s.contains("missing trie node") && s.contains("state") && s.contains("not available") | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.