mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-07 22:12:51 -04:00
Disallow concurrent swaps
This commit is contained in:
parent
7d2b7bee92
commit
849e6e7a14
1 changed files with 38 additions and 16 deletions
|
@ -22,6 +22,11 @@ use structopt::lazy_static::lazy_static;
|
||||||
use tokio::sync::broadcast::Receiver;
|
use tokio::sync::broadcast::Receiver;
|
||||||
use tracing::{debug_span, Instrument};
|
use tracing::{debug_span, Instrument};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref SWAP_MUTEX: Mutex<Option<Uuid>> = Mutex::new(None);
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub struct Request {
|
pub struct Request {
|
||||||
|
@ -558,10 +563,26 @@ impl Request {
|
||||||
pub async fn call(self, context: Arc<Context>) -> Result<serde_json::Value> {
|
pub async fn call(self, context: Arc<Context>) -> Result<serde_json::Value> {
|
||||||
// If the swap ID is set, we add it to the span
|
// If the swap ID is set, we add it to the span
|
||||||
let call_span = debug_span!(
|
let call_span = debug_span!(
|
||||||
"call",
|
"cmd",
|
||||||
method = ?self.cmd,
|
method = ?self.cmd,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if let Some(swap_id) = self.has_lockable_swap_id() {
|
||||||
|
println!("taking lock for swap_id: {}", swap_id);
|
||||||
|
let mut guard = SWAP_MUTEX.try_lock().context("Another swap is already running")?;
|
||||||
|
if guard.is_some() {
|
||||||
|
bail!("Another swap is already running");
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = guard.insert(swap_id.clone());
|
||||||
|
|
||||||
|
let result = self.handle_cmd(context).instrument(call_span).await;
|
||||||
|
guard.take();
|
||||||
|
|
||||||
|
println!("releasing lock for swap_id: {}", swap_id);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
self.handle_cmd(context).instrument(call_span).await
|
self.handle_cmd(context).instrument(call_span).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -667,3 +688,4 @@ where
|
||||||
|
|
||||||
Ok((btc_swap_amount, fees))
|
Ok((btc_swap_amount, fees))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue