mirror of
https://github.com/Cyrix126/gupaxx.git
synced 2025-01-19 04:14:31 +00:00
fix: more consistent XvB node ping
This commit is contained in:
parent
a966ab83d6
commit
279bdcc678
1 changed files with 32 additions and 13 deletions
|
@ -77,9 +77,14 @@ impl XvbNode {
|
||||||
let client_eu = client.clone();
|
let client_eu = client.clone();
|
||||||
let client_na = client.clone();
|
let client_na = client.clone();
|
||||||
// two spawn to ping the two nodes in parallel and not one after the other.
|
// two spawn to ping the two nodes in parallel and not one after the other.
|
||||||
let ms_eu = spawn(async move { XvbNode::ping(&XvbNode::Europe.url(), &client_eu).await });
|
let ms_eu = spawn(async move {
|
||||||
let ms_na =
|
info!("Node | ping European XvB Node");
|
||||||
spawn(async move { XvbNode::ping(&XvbNode::NorthAmerica.url(), &client_na).await });
|
XvbNode::ping(&XvbNode::Europe.url(), &client_eu).await
|
||||||
|
});
|
||||||
|
let ms_na = spawn(async move {
|
||||||
|
info!("Node | ping North America Node");
|
||||||
|
XvbNode::ping(&XvbNode::NorthAmerica.url(), &client_na).await
|
||||||
|
});
|
||||||
let node = if let Ok(ms_eu) = ms_eu.await {
|
let node = if let Ok(ms_eu) = ms_eu.await {
|
||||||
if let Ok(ms_na) = ms_na.await {
|
if let Ok(ms_na) = ms_na.await {
|
||||||
// if two nodes are up, compare ping latency and return fastest.
|
// if two nodes are up, compare ping latency and return fastest.
|
||||||
|
@ -138,31 +143,45 @@ impl XvbNode {
|
||||||
let request = client
|
let request = client
|
||||||
.post("http://".to_string() + ip + ":" + XVB_NODE_RPC + "/json_rpc")
|
.post("http://".to_string() + ip + ":" + XVB_NODE_RPC + "/json_rpc")
|
||||||
.body(r#"{"jsonrpc":"2.0","id":"0","method":"get_info"}"#);
|
.body(r#"{"jsonrpc":"2.0","id":"0","method":"get_info"}"#);
|
||||||
let ms;
|
let mut vec_ms = vec![];
|
||||||
let now = Instant::now();
|
for _ in 0..6 {
|
||||||
match tokio::time::timeout(Duration::from_secs(8), request.send()).await {
|
// clone request
|
||||||
|
let req = request
|
||||||
|
.try_clone()
|
||||||
|
.expect("should be able to clone a str body");
|
||||||
|
// begin timer
|
||||||
|
let now_req = Instant::now();
|
||||||
|
// get and store time of request
|
||||||
|
vec_ms.push(match tokio::time::timeout(Duration::from_millis(TIMEOUT_NODE_PING as u64), req.send()).await {
|
||||||
Ok(Ok(json_rpc)) => {
|
Ok(Ok(json_rpc)) => {
|
||||||
// Attempt to convert to JSON-RPC.
|
// Attempt to convert to JSON-RPC.
|
||||||
match json_rpc.bytes().await {
|
match json_rpc.bytes().await {
|
||||||
Ok(b) => match serde_json::from_slice::<GetInfo<'_>>(&b) {
|
Ok(b) => match serde_json::from_slice::<GetInfo<'_>>(&b) {
|
||||||
Ok(rpc) => {
|
Ok(rpc) => {
|
||||||
if rpc.result.mainnet && rpc.result.synchronized {
|
if rpc.result.mainnet && rpc.result.synchronized {
|
||||||
ms = now.elapsed().as_millis();
|
now_req.elapsed().as_millis()
|
||||||
} else {
|
} else {
|
||||||
ms = TIMEOUT_NODE_PING;
|
|
||||||
warn!("Ping | {ip} responded with valid get_info but is not in sync, remove this node!");
|
warn!("Ping | {ip} responded with valid get_info but is not in sync, remove this node!");
|
||||||
|
TIMEOUT_NODE_PING
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
ms = TIMEOUT_NODE_PING;
|
|
||||||
warn!("Ping | {ip} responded but with invalid get_info, remove this node!");
|
warn!("Ping | {ip} responded but with invalid get_info, remove this node!");
|
||||||
|
TIMEOUT_NODE_PING
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => ms = TIMEOUT_NODE_PING,
|
_ => TIMEOUT_NODE_PING,
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
_ => ms = TIMEOUT_NODE_PING,
|
_ => TIMEOUT_NODE_PING,
|
||||||
};
|
});
|
||||||
|
}
|
||||||
|
let ms = *vec_ms
|
||||||
|
.iter()
|
||||||
|
.min()
|
||||||
|
.expect("at least the value of timeout should be present");
|
||||||
|
info!("Ping | {ms}ms ... {ip}");
|
||||||
|
info!("{:?}", vec_ms);
|
||||||
ms
|
ms
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue