|
1 | 1 | use colored::Colorize;
|
2 | 2 | use std::time::Duration;
|
3 | 3 |
|
4 |
| -use crate::{utils::get_points, DriaComputeNode, DRIA_COMPUTE_NODE_VERSION}; |
| 4 | +use crate::{ |
| 5 | + utils::{get_points, DriaRPC}, |
| 6 | + DriaComputeNode, DRIA_COMPUTE_NODE_VERSION, |
| 7 | +}; |
5 | 8 |
|
6 | 9 | /// Number of seconds such that if the last heartbeat ACK is older than this, the node is considered unreachable.
|
7 | 10 | const HEARTBEAT_LIVENESS_SECS: Duration = Duration::from_secs(150);
|
@@ -82,29 +85,47 @@ impl DriaComputeNode {
|
82 | 85 | }
|
83 | 86 | }
|
84 | 87 |
|
85 |
| - /// Updates the local list of available nodes by refreshing it. |
86 |
| - /// Dials the RPC nodes again for better connectivity. |
| 88 | + /// Dials the existing RPC node if we are not connected to it. |
| 89 | + /// |
| 90 | + /// If there is an error while doing that, |
| 91 | + /// it will try to get a new RPC node and dial it. |
87 | 92 | pub(crate) async fn handle_available_nodes_refresh(&mut self) {
|
88 |
| - log::info!("Refreshing available Dria nodes."); |
| 93 | + log::debug!("Checking RPC connections for diagnostics."); |
89 | 94 |
|
90 |
| - // FIXME: what to do for refreshing nodes |
91 |
| - // if let Err(e) = refresh_dria_nodes(&mut self.dria_nodes).await { |
92 |
| - // log::error!("Error refreshing available nodes: {:?}", e); |
93 |
| - // }; |
94 |
| - |
95 |
| - // TODO: check if we are connected to the node, and dial again if not |
96 |
| - |
97 |
| - // dial the RPC |
98 |
| - log::info!("Dialling RPC at: {}", self.dria_nodes.addr); |
99 |
| - let fut = self |
| 95 | + // check if we are connected |
| 96 | + let is_connected = self |
100 | 97 | .p2p
|
101 |
| - .dial(self.dria_nodes.peer_id, self.dria_nodes.addr.clone()); |
102 |
| - match tokio::time::timeout(Duration::from_secs(10), fut).await { |
103 |
| - Err(timeout) => log::error!("Timeout dialling RPC node: {:?}", timeout), |
104 |
| - Ok(res) => match res { |
105 |
| - Err(e) => log::warn!("Error dialling RPC node: {:?}", e), |
106 |
| - Ok(_) => log::info!("Successfully dialled RPC!"), |
107 |
| - }, |
108 |
| - }; |
| 98 | + .is_connected(self.dria_rpc.peer_id) |
| 99 | + .await |
| 100 | + .unwrap_or(false); |
| 101 | + |
| 102 | + // if we are not connected, try to dial it again |
| 103 | + if !is_connected { |
| 104 | + log::info!("Dialling RPC at: {}", self.dria_rpc.addr); |
| 105 | + if let Err(err) = self |
| 106 | + .dial_with_timeout(self.dria_rpc.peer_id, self.dria_rpc.addr.clone()) |
| 107 | + .await |
| 108 | + { |
| 109 | + // if we also cannot dial it, get a new RPC node |
| 110 | + log::warn!( |
| 111 | + "Could not dial to RPC at: {}: {err:?}\nWill get a new RPC node.", |
| 112 | + self.dria_rpc.addr, |
| 113 | + ); |
| 114 | + self.dria_rpc = DriaRPC::new(self.dria_rpc.network).await; |
| 115 | + |
| 116 | + // now dial this new RPC again |
| 117 | + if let Err(err) = self |
| 118 | + .dial_with_timeout(self.dria_rpc.peer_id, self.dria_rpc.addr.clone()) |
| 119 | + .await |
| 120 | + { |
| 121 | + // worst-case we cant dial this one too, just leave it for the next diagnostic |
| 122 | + log::error!("Could not dial the new RPC: {err:?}\nWill try again in the next diagnostic refresh."); |
| 123 | + } |
| 124 | + } else { |
| 125 | + log::info!("Successfully dialled to RPC at: {}", self.dria_rpc.addr); |
| 126 | + } |
| 127 | + } else { |
| 128 | + log::debug!("Connection with {} is intact.", self.dria_rpc.peer_id); |
| 129 | + } |
109 | 130 | }
|
110 | 131 | }
|
0 commit comments