mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-06 18:21:45 +00:00
Improve tokio task execution (#1181)
* Add logging on shutdown
* Replace tokio::spawn with handle.spawn
* Upgrade tokio
* Add a task executor
* Beacon chain tasks use task executor
* Validator client tasks use task executor
* Rename runtime_handle to executor
* Add duration histograms; minor fixes
* Cleanup
* Fix logs
* Fix tests
* Remove random file
* Get enr dependency instead of libp2p
* Address some review comments
* Libp2p takes a TaskExecutor
* Ugly fix libp2p tests
* Move TaskExecutor to own file
* Upgrade Dockerfile rust version
* Minor fixes
* Revert "Ugly fix libp2p tests"
This reverts commit 58d4bb690f.
* Pretty fix libp2p tests
* Add spawn_without_exit; change Counter to Gauge
* Tidy
* Move log from RuntimeContext to TaskExecutor
* Fix errors
* Replace histogram with int_gauge for async tasks
* Fix todo
* Fix memory leak in test by exiting all spawned tasks at the end
This commit is contained in:
@@ -12,6 +12,7 @@ serde = "1.0.110"
|
||||
serde_derive = "1.0.110"
|
||||
serde_json = "1.0.52"
|
||||
slog = "2.5.2"
|
||||
tokio = { version = "0.2.20", features = ["full"] }
|
||||
tokio = { version = "0.2.21", features = ["full"] }
|
||||
types = { path = "../../consensus/types" }
|
||||
ws = "0.9.1"
|
||||
environment = { path = "../../lighthouse/environment" }
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use slog::{debug, error, info, warn, Logger};
|
||||
use slog::{debug, error, info, warn};
|
||||
use std::marker::PhantomData;
|
||||
use std::net::SocketAddr;
|
||||
use types::EthSpec;
|
||||
@@ -34,16 +34,10 @@ impl<T: EthSpec> WebSocketSender<T> {
|
||||
}
|
||||
|
||||
pub fn start_server<T: EthSpec>(
|
||||
executor: environment::TaskExecutor,
|
||||
config: &Config,
|
||||
log: &Logger,
|
||||
) -> Result<
|
||||
(
|
||||
WebSocketSender<T>,
|
||||
tokio::sync::oneshot::Sender<()>,
|
||||
SocketAddr,
|
||||
),
|
||||
String,
|
||||
> {
|
||||
) -> Result<(WebSocketSender<T>, SocketAddr), String> {
|
||||
let log = executor.log();
|
||||
let server_string = format!("{}:{}", config.listen_address, config.port);
|
||||
|
||||
// Create a server that simply ignores any incoming messages.
|
||||
@@ -67,31 +61,26 @@ pub fn start_server<T: EthSpec>(
|
||||
let broadcaster = server.broadcaster();
|
||||
|
||||
// Produce a signal/channel that can gracefully shutdown the websocket server.
|
||||
let exit_channel = {
|
||||
let (exit_channel, exit) = tokio::sync::oneshot::channel();
|
||||
|
||||
let log_inner = log.clone();
|
||||
let broadcaster_inner = server.broadcaster();
|
||||
let exit_future = async move {
|
||||
let _ = exit.await;
|
||||
if let Err(e) = broadcaster_inner.shutdown() {
|
||||
warn!(
|
||||
log_inner,
|
||||
"Websocket server errored on shutdown";
|
||||
"error" => format!("{:?}", e)
|
||||
);
|
||||
} else {
|
||||
info!(log_inner, "Websocket server shutdown");
|
||||
}
|
||||
};
|
||||
|
||||
// Place a future on the handle that will shutdown the websocket server when the
|
||||
// application exits.
|
||||
tokio::spawn(exit_future);
|
||||
|
||||
exit_channel
|
||||
let exit = executor.exit();
|
||||
let log_inner = log.clone();
|
||||
let broadcaster_inner = server.broadcaster();
|
||||
let exit_future = async move {
|
||||
let _ = exit.await;
|
||||
if let Err(e) = broadcaster_inner.shutdown() {
|
||||
warn!(
|
||||
log_inner,
|
||||
"Websocket server errored on shutdown";
|
||||
"error" => format!("{:?}", e)
|
||||
);
|
||||
} else {
|
||||
info!(log_inner, "Websocket server shutdown");
|
||||
}
|
||||
};
|
||||
|
||||
// Place a future on the handle that will shutdown the websocket server when the
|
||||
// application exits.
|
||||
executor.runtime_handle().spawn(exit_future);
|
||||
|
||||
let log_inner = log.clone();
|
||||
|
||||
let _ = std::thread::spawn(move || match server.run() {
|
||||
@@ -122,7 +111,6 @@ pub fn start_server<T: EthSpec>(
|
||||
sender: Some(broadcaster),
|
||||
_phantom: PhantomData,
|
||||
},
|
||||
exit_channel,
|
||||
actual_listen_addr,
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user