Skip to content

Commit

Permalink
use a mutex to sync the client execution
Browse files Browse the repository at this point in the history
  • Loading branch information
musitdev committed Oct 23, 2024
1 parent e545a3a commit bc21ebb
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions protocol-units/bridge/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use crate::types::BridgeTransferId;
use crate::types::ChainId;
use futures::stream::FuturesUnordered;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::select;
use tokio::sync::Mutex;
use tokio_stream::StreamExt;

mod actions;
Expand Down Expand Up @@ -42,6 +44,10 @@ where
let mut client_exec_result_futures_one = FuturesUnordered::new();
let mut client_exec_result_futures_two = FuturesUnordered::new();

//only one client can use at a time.
let one_client_lock = Arc::new(Mutex::new(()));
let two_client_lock = Arc::new(Mutex::new(()));

// let mut action_to_exec_futures_one = FuturesUnordered::new();
// let mut action_to_exec_futures_two = FuturesUnordered::new();

Expand Down Expand Up @@ -70,15 +76,27 @@ where
ChainId::ONE => {
let fut = process_action(action, one_client.clone());
if let Some(fut) = fut {
let jh = tokio::spawn(fut);
let jh = tokio::spawn({
let client_lock_clone = one_client_lock.clone();
async move {
let _lock = client_lock_clone.lock().await;
fut.await
}
});
client_exec_result_futures_one.push(jh);
}

},
ChainId::TWO => {
let fut = process_action(action, two_client.clone());
if let Some(fut) = fut {
let jh = tokio::spawn(fut);
let jh = tokio::spawn({
let client_lock_clone = two_client_lock.clone();
async move {
let _lock = client_lock_clone.lock().await;
fut.await
}
});
client_exec_result_futures_two.push(jh);
}
}
Expand Down

0 comments on commit bc21ebb

Please sign in to comment.