Skip to content

Commit

Permalink
Avoid long handling locks in electrum_request_to. Ensure server.versi…
Browse files Browse the repository at this point in the history
…on is sent first.

#745
  • Loading branch information
artemii235 committed Nov 20, 2020
1 parent 5b70ff3 commit 15a225a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
7 changes: 4 additions & 3 deletions mm2src/coins/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,14 +692,15 @@ pub async fn utxo_fields_from_conf_and_request(

let client = Arc::new(client);

let weak_client = Arc::downgrade(&client);
spawn_electrum_ping_loop(weak_client, servers);

let weak_client = Arc::downgrade(&client);
let client_name = format!("{} GUI/MM2 {}", ctx.gui().unwrap_or("UNKNOWN"), MM_VERSION);
spawn_electrum_version_loop(weak_client, on_connect_rx, client_name);

try_s!(wait_for_protocol_version_checked(&client).await);

let weak_client = Arc::downgrade(&client);
spawn_electrum_ping_loop(weak_client, servers);

UtxoRpcClientEnum::Electrum(ElectrumClient(client))
},
_ => return ERR!("utxo_arc_from_conf_and_request should be called only by enable or electrum requests"),
Expand Down
28 changes: 15 additions & 13 deletions mm2src/coins/utxo/rpc_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,21 +1050,23 @@ async fn electrum_request_to(
request: JsonRpcRequest,
to_addr: String,
) -> Result<(JsonRpcRemoteAddr, JsonRpcResponse), String> {
let connections = client.connections.lock().await;
let connection = connections
.iter()
.find(|c| c.addr == to_addr)
.ok_or(ERRL!("Unknown destination address {}", to_addr))?;

let response = match &*connection.tx.lock().await {
Some(tx) => try_s!(
electrum_request(request.clone(), tx.clone(), connection.responses.clone())
.compat()
.await
),
None => return ERR!("Connection {} is not established yet", to_addr),
let (tx, responses) = {
let connections = client.connections.lock().await;
let connection = connections
.iter()
.find(|c| c.addr == to_addr)
.ok_or(ERRL!("Unknown destination address {}", to_addr))?;
let responses = connection.responses.clone();
let tx = {
match &*connection.tx.lock().await {
Some(tx) => tx.clone(),
None => return ERR!("Connection {} is not established yet", to_addr),
}
};
(tx, responses)
};

let response = try_s!(electrum_request(request.clone(), tx, responses).compat().await);
Ok((JsonRpcRemoteAddr(to_addr.to_owned()), response))
}

Expand Down

0 comments on commit 15a225a

Please sign in to comment.