Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup Forest execution threads #236

Merged
merged 7 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions forest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ slog-async = "2.3.0"
slog-term = "2.4.2"
async-std = { version = "1.4.0", features = ["attributes"] }
serde = { version = "1.0", features = ["derive"] }
ctrlc = "3.1.4"
28 changes: 26 additions & 2 deletions forest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use async_std::task;
use forest_libp2p::{get_keypair, Libp2pService};
use libp2p::identity::{ed25519, Keypair};
use slog::{info, trace};
use std::process;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use utils::write_to_file;

#[async_std::main]
Expand Down Expand Up @@ -36,11 +39,32 @@ async fn main() {
}
};

let lp2p_service = Libp2pService::new(logger, &config.network, net_keypair);
let running = Arc::new(AtomicUsize::new(0));
let r = running.clone();
ctrlc::set_handler(move || {
let prev = r.fetch_add(1, Ordering::SeqCst);
if prev == 0 {
println!("Got interrupt, shutting down...");
} else {
process::exit(0);
}
})
.expect("Error setting Ctrl-C handler");

task::block_on(async move {
// Start libp2p service
let lp2p_service = Libp2pService::new(logger, &config.network, net_keypair);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just call it p2p_service

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unchanged in this PR, but changed it haha

let lp2p_thread = task::spawn(async {
lp2p_service.run().await;
});

loop {
if running.load(Ordering::SeqCst) > 0 {
// TODO change dropping threads to gracefully shutting down services
// or implement drop on components with sensitive shutdown
drop(lp2p_thread);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: p2p_thread

break;
}
}

info!(log, "Forest finish shutdown");
}