-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework of the verifier. Now tasks are no more spawned like crazy
- Loading branch information
Showing
5 changed files
with
134 additions
and
80 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,102 @@ | ||
use crate::rpc_pool::verify_endpoint; | ||
use crate::Web3RpcPool; | ||
use futures_util::future; | ||
use parking_lot::Mutex; | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
|
||
#[derive(Debug)] | ||
pub struct EndpointsVerifier { | ||
pub last_verify: Arc<Mutex<Option<std::time::Instant>>>, | ||
pub is_finished: Arc<Mutex<bool>>, | ||
pub verify_handle: Arc<Mutex<Option<tokio::task::JoinHandle<()>>>>, | ||
} | ||
|
||
impl Default for EndpointsVerifier { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
impl EndpointsVerifier { | ||
pub fn new() -> Self { | ||
Self { | ||
last_verify: Arc::new(Mutex::new(None)), | ||
is_finished: Arc::new(Mutex::new(false)), | ||
verify_handle: Arc::new(Mutex::new(None)), | ||
} | ||
} | ||
pub fn is_finished(&self) -> bool { | ||
*self | ||
.is_finished | ||
.try_lock_for(Duration::from_secs(5)) | ||
.unwrap() | ||
} | ||
pub fn get_join_handle(&self) -> Option<tokio::task::JoinHandle<()>> { | ||
self.verify_handle | ||
.try_lock_for(Duration::from_secs(5)) | ||
.unwrap() | ||
.take() | ||
} | ||
|
||
pub fn start_verify_if_needed(self: &Arc<Self>, pool: Arc<Web3RpcPool>) { | ||
let mut last_verify = self | ||
.last_verify | ||
.try_lock_for(Duration::from_secs(5)) | ||
.unwrap(); | ||
if let Some(last_verify) = last_verify.as_ref() { | ||
if last_verify.elapsed() < pool.check_external_sources_interval { | ||
log::debug!( | ||
"Last external check was less than check_external_sources_interval ago" | ||
); | ||
return; | ||
} | ||
} | ||
last_verify.replace(std::time::Instant::now()); | ||
//spawn async task and return immediately | ||
let pool = pool.clone(); | ||
let self_cloned = self.clone(); | ||
let h = tokio::spawn(async move { | ||
log::error!("Starting external resolver"); | ||
self_cloned | ||
.clone() | ||
.verify_unverified_endpoints(pool.clone()) | ||
.await; | ||
*self_cloned | ||
.is_finished | ||
.try_lock_for(Duration::from_secs(5)) | ||
.unwrap() = true; | ||
}); | ||
self.verify_handle | ||
.try_lock_for(Duration::from_secs(5)) | ||
.unwrap() | ||
.replace(h); | ||
} | ||
|
||
async fn verify_unverified_endpoints(self: Arc<EndpointsVerifier>, pool: Arc<Web3RpcPool>) { | ||
let _guard = pool.verify_mutex.lock().await; | ||
let futures = { | ||
let endpoints_copy = pool | ||
.endpoints | ||
.try_lock_for(Duration::from_secs(5)) | ||
.unwrap() | ||
.clone(); | ||
|
||
let mut futures = Vec::new(); | ||
for (_idx, endp) in endpoints_copy { | ||
{ | ||
if endp | ||
.try_read_for(Duration::from_secs(5)) | ||
.unwrap() | ||
.is_removed() | ||
{ | ||
continue; | ||
} | ||
} | ||
futures.push(verify_endpoint(pool.chain_id, endp.clone())); | ||
} | ||
futures | ||
}; | ||
|
||
future::join_all(futures).await; | ||
} | ||
} |
This file contains 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