mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-14 21:25:44 +00:00
committed by
Bastian Köcher
parent
25a4151a8b
commit
63ce1b5973
@@ -26,9 +26,9 @@ pub const RECONNECT_DELAY: Duration = Duration::from_secs(10);
|
||||
|
||||
/// Basic blockchain client from relay perspective.
|
||||
#[async_trait]
|
||||
pub trait Client: Clone + Send + Sync {
|
||||
pub trait Client: 'static + Clone + Send + Sync {
|
||||
/// Type of error this clients returns.
|
||||
type Error: Debug + MaybeConnectionError;
|
||||
type Error: 'static + Debug + MaybeConnectionError + Send + Sync;
|
||||
|
||||
/// Try to reconnect to source node.
|
||||
async fn reconnect(&mut self) -> Result<(), Self::Error>;
|
||||
@@ -105,63 +105,65 @@ impl<SC, TC, LM> Loop<SC, TC, LM> {
|
||||
/// This function represents an outer loop, which in turn calls provided `run_loop` function to do
|
||||
/// actual job. When `run_loop` returns, this outer loop reconnects to failed client (source,
|
||||
/// target or both) and calls `run_loop` again.
|
||||
pub async fn run<R, F>(mut self, run_loop: R) -> Result<(), String>
|
||||
pub async fn run<R, F>(mut self, loop_name: String, run_loop: R) -> Result<(), String>
|
||||
where
|
||||
R: Fn(SC, TC, Option<LM>) -> F,
|
||||
F: Future<Output = Result<(), FailedClient>>,
|
||||
SC: Client,
|
||||
TC: Client,
|
||||
LM: Clone,
|
||||
R: 'static + Send + Fn(SC, TC, Option<LM>) -> F,
|
||||
F: 'static + Send + Future<Output = Result<(), FailedClient>>,
|
||||
SC: 'static + Client,
|
||||
TC: 'static + Client,
|
||||
LM: 'static + Send + Clone,
|
||||
{
|
||||
loop {
|
||||
let result = run_loop(
|
||||
self.source_client.clone(),
|
||||
self.target_client.clone(),
|
||||
self.loop_metric.clone(),
|
||||
)
|
||||
.await;
|
||||
async_std::task::spawn(async move {
|
||||
crate::initialize::initialize_loop(loop_name);
|
||||
|
||||
match result {
|
||||
Ok(()) => break,
|
||||
Err(failed_client) => loop {
|
||||
async_std::task::sleep(self.reconnect_delay).await;
|
||||
if failed_client == FailedClient::Both || failed_client == FailedClient::Source {
|
||||
match self.source_client.reconnect().await {
|
||||
Ok(()) => (),
|
||||
Err(error) => {
|
||||
log::warn!(
|
||||
target: "bridge",
|
||||
"Failed to reconnect to source client. Going to retry in {}s: {:?}",
|
||||
self.reconnect_delay.as_secs(),
|
||||
error,
|
||||
);
|
||||
continue;
|
||||
loop {
|
||||
let loop_metric = self.loop_metric.clone();
|
||||
let future_result = run_loop(self.source_client.clone(), self.target_client.clone(), loop_metric);
|
||||
let result = future_result.await;
|
||||
|
||||
match result {
|
||||
Ok(()) => break,
|
||||
Err(failed_client) => loop {
|
||||
async_std::task::sleep(self.reconnect_delay).await;
|
||||
if failed_client == FailedClient::Both || failed_client == FailedClient::Source {
|
||||
match self.source_client.reconnect().await {
|
||||
Ok(()) => (),
|
||||
Err(error) => {
|
||||
log::warn!(
|
||||
target: "bridge",
|
||||
"Failed to reconnect to source client. Going to retry in {}s: {:?}",
|
||||
self.reconnect_delay.as_secs(),
|
||||
error,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if failed_client == FailedClient::Both || failed_client == FailedClient::Target {
|
||||
match self.target_client.reconnect().await {
|
||||
Ok(()) => (),
|
||||
Err(error) => {
|
||||
log::warn!(
|
||||
target: "bridge",
|
||||
"Failed to reconnect to target client. Going to retry in {}s: {:?}",
|
||||
self.reconnect_delay.as_secs(),
|
||||
error,
|
||||
);
|
||||
continue;
|
||||
if failed_client == FailedClient::Both || failed_client == FailedClient::Target {
|
||||
match self.target_client.reconnect().await {
|
||||
Ok(()) => (),
|
||||
Err(error) => {
|
||||
log::warn!(
|
||||
target: "bridge",
|
||||
"Failed to reconnect to target client. Going to retry in {}s: {:?}",
|
||||
self.reconnect_delay.as_secs(),
|
||||
error,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
},
|
||||
break;
|
||||
},
|
||||
}
|
||||
|
||||
log::debug!(target: "bridge", "Restarting relay loop");
|
||||
}
|
||||
|
||||
log::debug!(target: "bridge", "Restarting relay loop");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user