Skip to content

Commit

Permalink
suggestions from omar: don't send to all
Browse files Browse the repository at this point in the history
  • Loading branch information
mariocynicys committed Sep 24, 2024
1 parent 2c61c61 commit 25b174a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
5 changes: 3 additions & 2 deletions mm2src/coins/utxo/rpc_clients/electrum_rpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,12 @@ impl ElectrumClient {
{
Ok(response) => Ok(response),
// If we failed the request using only the active connections, try again using all connections.
Err(_) => {
Err(_) if !send_to_all => {
warn!("Failed to send the request using active connections, trying all connections.");
let connections = self.connection_manager.get_all_connections();
// The concurrency here must be `1`, because we are trying out connections that aren't maintained
// which means we might break the max connections rule.
// We will at most we will break this rule by `1` (have `max_connected + 1` open connections).
// We will at most break this rule by `1` (have `max_connected + 1` open connections).
let concurrency = 1;
match self
.send_request_using(&request, connections, send_to_all, concurrency)
Expand All @@ -325,6 +325,7 @@ impl ElectrumClient {
Err(err_vec) => Err(JsonRpcErrorType::Internal(format!("All servers errored: {err_vec:?}"))),
}
},
Err(e) => Err(JsonRpcErrorType::Internal(format!("All servers errored: {e:?}"))),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::connection_context::ConnectionContext;
use crate::utxo::rpc_clients::UtxoRpcClientOps;
use common::executor::abortable_queue::AbortableQueue;
use common::executor::{AbortableSystem, SpawnFuture, Timer};
use common::log::debug;
use common::notifier::{Notifiee, Notifier};
use common::now_ms;
use keys::Address;
Expand Down Expand Up @@ -267,6 +268,7 @@ impl ConnectionManager {

/// Handles the disconnection event from an Electrum server.
pub fn on_disconnected(&self, server_address: &str) {
debug!("Electrum server disconnected: {}", server_address);
let all_connections = self.read_connections();
let connection_ctx = unwrap_or_return!(all_connections.get(server_address));

Expand Down Expand Up @@ -387,9 +389,8 @@ impl ConnectionManager {
let client = unwrap_or_return!(self.get_client());
// Sort the candidate connections by their priority/ID.
candidate_connections.sort_by_key(|(_, priority)| *priority);
for (connection, _) in candidate_connections {
for (connection, connection_id) in candidate_connections {
let address = connection.address().to_string();
let connection_id = unwrap_or_continue!(self.read_connections().get(&address)).id;
let (maintained_connections_size, lowest_priority_connection_id) = {
let maintained_connections = self.read_maintained_connections();
let maintained_connections_size = maintained_connections.len() as u32;
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/utxo/rpc_clients/electrum_rpc/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub const NO_FORCE_CONNECT_METHODS: &[&str] = &[
"server.version",
];
/// Electrum methods that should be sent to all connections even after receiving a response from a subset of them.
/// Note that this is only applicable to active/maintained connections. If an electrum request fails by all maintained
/// connections, a fallback using all connections will *NOT* be attempted.
pub const SEND_TO_ALL_METHODS: &[&str] = &[
// A ping should be sent to all connections even if we got a response from one of them early.
"server.ping",
Expand Down

0 comments on commit 25b174a

Please sign in to comment.