Skip to content

Commit

Permalink
Address review; minor nits
Browse files Browse the repository at this point in the history
  • Loading branch information
claddyy committed Dec 18, 2024
1 parent e1c0526 commit 09718c6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
47 changes: 21 additions & 26 deletions src/maker/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,7 @@ pub struct ConnectionState {

pub struct ThreadPool {
pub threads: Mutex<Vec<JoinHandle<()>>>,
}

impl Default for ThreadPool {
fn default() -> Self {
Self::new()
}
pub port: u16,
}

impl Drop for ThreadPool {
Expand All @@ -110,9 +105,10 @@ impl Drop for ThreadPool {
}

impl ThreadPool {
pub fn new() -> Self {
pub fn new(port: u16) -> Self {
Self {
threads: Mutex::new(Vec::new()),
port,
}
}

Expand All @@ -127,29 +123,29 @@ impl ThreadPool {
.lock()
.map_err(|_| MakerError::General("Failed to lock threads"))?;

let thread_count = threads.len();
log::info!("Joining {} threads", thread_count);
log::info!("Joining {} threads", threads.len());

let mut joined_count = 0;
while let Some(thread) = threads.pop() {
let thread_name = thread.thread().name().unwrap().to_string();

match thread.join() {
Ok(_) => {
log::info!("Thread {} terminated successfully", thread_name);
log::info!("[{}] Thread {} joined", self.port, thread_name);
joined_count += 1;
}
Err(e) => {
log::error!("Thread {} terminated due to error {:?}", thread_name, e);
log::error!(
"[{}] Error {:?} while joining thread {}",
self.port,
e,
thread_name

Check warning on line 142 in src/maker/api.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/api.rs#L137-L142

Added lines #L137 - L142 were not covered by tests
);
}
}
}

log::info!(
"Successfully joined {} out of {} threads",
joined_count,
thread_count
);
log::info!("Successfully joined {} threads", joined_count,);
Ok(())
}
}
Expand Down Expand Up @@ -272,6 +268,8 @@ impl Maker {
config.connection_type = connection_type;
}

let thread_pool_port = config.port;

config.write_to_file(&data_dir.join("config.toml"))?;

log::info!("Initializing wallet sync");
Expand All @@ -286,7 +284,7 @@ impl Maker {
connection_state: Mutex::new(HashMap::new()),
highest_fidelity_proof: RwLock::new(None),
is_setup_complete: AtomicBool::new(false),
thread_pool: Arc::new(ThreadPool::new()),
thread_pool: Arc::new(ThreadPool::new(thread_pool_port)),
})
}

Expand Down Expand Up @@ -532,12 +530,11 @@ pub fn check_for_broadcasted_contracts(maker: Arc<Maker>) -> Result<(), MakerErr
maker.config.port
);
let handle = std::thread::Builder::new()
.name("recovery: saw contracts in mempool".to_string())
.name("Swap recovery thread".to_string())
.spawn(move || {
if let Err(e) =
recover_from_swap(maker_clone.clone(), outgoings, incomings)
if let Err(e) = recover_from_swap(maker_clone, outgoings, incomings)
{
log::error!("Recovery thread failed with error: {:?}", e);
log::error!("Failed to recover from swap due to: {:?}", e);

Check warning on line 537 in src/maker/api.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/api.rs#L537

Added line #L537 was not covered by tests
}
})?;
maker.thread_pool.add_thread(handle);
Expand Down Expand Up @@ -620,12 +617,10 @@ pub fn check_for_idle_states(maker: Arc<Maker>) -> Result<(), MakerError> {
maker.config.port
);
let handle = std::thread::Builder::new()
.name("recovery: taker dropped".to_string())
.name("Swap Recovery Thread".to_string())
.spawn(move || {
if let Err(e) =
recover_from_swap(maker_clone.clone(), outgoings, incomings)
{
log::error!("Recovery thread failed with error: {:?}", e);
if let Err(e) = recover_from_swap(maker_clone, outgoings, incomings) {
log::error!("Failed to recover from swap due to: {:?}", e);

Check warning on line 623 in src/maker/api.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/api.rs#L623

Added line #L623 was not covered by tests
}
})?;
maker.thread_pool.add_thread(handle);
Expand Down
8 changes: 6 additions & 2 deletions src/maker/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,12 @@ fn unexpected_recovery(maker: Arc<Maker>) -> Result<(), MakerError> {
// Spawn a separate thread to wait for contract maturity and broadcasting timelocked.
let maker_clone = maker.clone();
let handle = std::thread::Builder::new()
.name("recovery: wait for contract maturity & broadcasting timelocked".to_string())
.spawn(move || recover_from_swap(maker_clone, outgoings, incomings).unwrap())?;
.name("Swap Recovery Thread".to_string())
.spawn(move || {
if let Err(e) = recover_from_swap(maker_clone, outgoings, incomings) {
log::error!("Failed to recover from swap due to: {:?}", e);

Check warning on line 685 in src/maker/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/handlers.rs#L685

Added line #L685 was not covered by tests
}
})?;
maker.thread_pool.add_thread(handle);
}
Ok(())
Expand Down

0 comments on commit 09718c6

Please sign in to comment.