Dht persistence on drop (v2) (#2200)

## Issue Addressed

NA

## Proposed Changes

This is simply #2177 with a merge conflict fixed.

Co-authored-by: realbigsean <seananderson33@gmail.com>
This commit is contained in:
Paul Hauner 2021-02-15 06:09:55 +00:00
parent 1a748699d9
commit 3000f3e5da

View File

@ -229,43 +229,15 @@ fn spawn_service<T: BeaconChainTypes>(
executor: task_executor::TaskExecutor, executor: task_executor::TaskExecutor,
mut service: NetworkService<T>, mut service: NetworkService<T>,
) { ) {
let mut exit_rx = executor.exit();
let mut shutdown_sender = executor.shutdown_sender(); let mut shutdown_sender = executor.shutdown_sender();
// spawn on the current executor // spawn on the current executor
executor.spawn_without_exit(async move { executor.spawn(async move {
let mut metric_update_counter = 0; let mut metric_update_counter = 0;
loop { loop {
// build the futures to check simultaneously // build the futures to check simultaneously
tokio::select! { tokio::select! {
// handle network shutdown
_ = (&mut exit_rx) => {
// network thread is terminating
let enrs = service.libp2p.swarm.enr_entries();
debug!(
service.log,
"Persisting DHT to store";
"Number of peers" => enrs.len(),
);
match persist_dht::<T::EthSpec, T::HotStore, T::ColdStore>(service.store.clone(), enrs) {
Err(e) => error!(
service.log,
"Failed to persist DHT on drop";
"error" => ?e
),
Ok(_) => info!(
service.log,
"Saved DHT state";
),
}
// attempt to remove port mappings
crate::nat::remove_mappings(service.upnp_mappings.0, service.upnp_mappings.1, &service.log);
info!(service.log, "Network service shutdown");
return;
}
_ = service.metrics_update.tick() => { _ = service.metrics_update.tick() => {
// update various network metrics // update various network metrics
metric_update_counter +=1; metric_update_counter +=1;
@ -583,3 +555,31 @@ fn next_fork_delay<T: BeaconChainTypes>(
tokio::time::sleep_until(tokio::time::Instant::now() + until_fork + delay) tokio::time::sleep_until(tokio::time::Instant::now() + until_fork + delay)
}) })
} }
impl<T: BeaconChainTypes> Drop for NetworkService<T> {
fn drop(&mut self) {
// network thread is terminating
let enrs = self.libp2p.swarm.enr_entries();
debug!(
self.log,
"Persisting DHT to store";
"Number of peers" => enrs.len(),
);
match persist_dht::<T::EthSpec, T::HotStore, T::ColdStore>(self.store.clone(), enrs) {
Err(e) => error!(
self.log,
"Failed to persist DHT on drop";
"error" => ?e
),
Ok(_) => info!(
self.log,
"Saved DHT state";
),
}
// attempt to remove port mappings
crate::nat::remove_mappings(self.upnp_mappings.0, self.upnp_mappings.1, &self.log);
info!(self.log, "Network service shutdown");
}
}