Reduce compile times even more
This commit is contained in:
parent
34f3ec6c29
commit
1b79b66cc3
2 changed files with 135 additions and 116 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
use std::{
|
use std::{
|
||||||
cmp::Reverse,
|
cmp::Reverse,
|
||||||
net::SocketAddr,
|
net::SocketAddr,
|
||||||
|
pin::Pin,
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
sync::{
|
sync::{
|
||||||
atomic::{AtomicU16, Ordering},
|
atomic::{AtomicU16, Ordering},
|
||||||
|
|
@ -23,7 +24,7 @@ use anyhow::{bail, Context};
|
||||||
use backoff::{backoff::Backoff, ExponentialBackoffBuilder};
|
use backoff::{backoff::Backoff, ExponentialBackoffBuilder};
|
||||||
use bencode::ByteString;
|
use bencode::ByteString;
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
use futures::{stream::FuturesUnordered, Stream, StreamExt, TryFutureExt};
|
use futures::{stream::FuturesUnordered, Future, FutureExt, Stream, StreamExt, TryFutureExt};
|
||||||
|
|
||||||
use leaky_bucket::RateLimiter;
|
use leaky_bucket::RateLimiter;
|
||||||
use librqbit_core::{
|
use librqbit_core::{
|
||||||
|
|
@ -232,6 +233,7 @@ impl Drop for RequestPeersStream {
|
||||||
impl Stream for RequestPeersStream {
|
impl Stream for RequestPeersStream {
|
||||||
type Item = SocketAddr;
|
type Item = SocketAddr;
|
||||||
|
|
||||||
|
#[inline(never)]
|
||||||
fn poll_next(
|
fn poll_next(
|
||||||
mut self: std::pin::Pin<&mut Self>,
|
mut self: std::pin::Pin<&mut Self>,
|
||||||
cx: &mut std::task::Context<'_>,
|
cx: &mut std::task::Context<'_>,
|
||||||
|
|
@ -1144,49 +1146,56 @@ impl DhtState {
|
||||||
&self.cancellation_token
|
&self.cancellation_token
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn with_config(mut config: DhtConfig) -> anyhow::Result<Arc<Self>> {
|
#[inline(never)]
|
||||||
let socket = match config.listen_addr {
|
pub fn with_config(
|
||||||
Some(addr) => UdpSocket::bind(addr)
|
mut config: DhtConfig,
|
||||||
.await
|
) -> Pin<Box<dyn Future<Output = anyhow::Result<Arc<Self>>> + Send>> {
|
||||||
.with_context(|| format!("error binding socket, address {addr}")),
|
async move {
|
||||||
None => UdpSocket::bind("0.0.0.0:0")
|
let socket = match config.listen_addr {
|
||||||
.await
|
Some(addr) => UdpSocket::bind(addr)
|
||||||
.context("error binding socket, address 0.0.0.0:0"),
|
.await
|
||||||
}?;
|
.with_context(|| format!("error binding socket, address {addr}")),
|
||||||
|
None => UdpSocket::bind("0.0.0.0:0")
|
||||||
|
.await
|
||||||
|
.context("error binding socket, address 0.0.0.0:0"),
|
||||||
|
}?;
|
||||||
|
|
||||||
let listen_addr = socket
|
let listen_addr = socket
|
||||||
.local_addr()
|
.local_addr()
|
||||||
.context("cannot determine UDP listen addr")?;
|
.context("cannot determine UDP listen addr")?;
|
||||||
info!("DHT listening on {:?}", listen_addr);
|
info!("DHT listening on {:?}", listen_addr);
|
||||||
|
|
||||||
let peer_id = config.peer_id.unwrap_or_else(generate_peer_id);
|
let peer_id = config.peer_id.unwrap_or_else(generate_peer_id);
|
||||||
info!("starting up DHT with peer id {:?}", peer_id);
|
info!("starting up DHT with peer id {:?}", peer_id);
|
||||||
let bootstrap_addrs = config
|
let bootstrap_addrs = config
|
||||||
.bootstrap_addrs
|
.bootstrap_addrs
|
||||||
.unwrap_or_else(|| crate::DHT_BOOTSTRAP.iter().map(|v| v.to_string()).collect());
|
.unwrap_or_else(|| crate::DHT_BOOTSTRAP.iter().map(|v| v.to_string()).collect());
|
||||||
|
|
||||||
let token = config.cancellation_token.take().unwrap_or_default();
|
let token = config.cancellation_token.take().unwrap_or_default();
|
||||||
|
|
||||||
let (in_tx, in_rx) = unbounded_channel();
|
let (in_tx, in_rx) = unbounded_channel();
|
||||||
let state = Arc::new(Self::new_internal(
|
let state = Arc::new(Self::new_internal(
|
||||||
peer_id,
|
peer_id,
|
||||||
in_tx,
|
in_tx,
|
||||||
config.routing_table,
|
config.routing_table,
|
||||||
listen_addr,
|
listen_addr,
|
||||||
config.peer_store.unwrap_or_else(|| PeerStore::new(peer_id)),
|
config.peer_store.unwrap_or_else(|| PeerStore::new(peer_id)),
|
||||||
token,
|
token,
|
||||||
));
|
));
|
||||||
|
|
||||||
spawn_with_cancel(error_span!("dht"), state.cancellation_token.clone(), {
|
spawn_with_cancel(error_span!("dht"), state.cancellation_token.clone(), {
|
||||||
let state = state.clone();
|
let state = state.clone();
|
||||||
async move {
|
async move {
|
||||||
let worker = DhtWorker { socket, dht: state };
|
let worker = DhtWorker { socket, dht: state };
|
||||||
worker.start(in_rx, &bootstrap_addrs).await
|
worker.start(in_rx, &bootstrap_addrs).await
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Ok(state)
|
Ok(state)
|
||||||
|
}
|
||||||
|
.boxed()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline(never)]
|
||||||
pub fn get_peers(
|
pub fn get_peers(
|
||||||
self: &Arc<Self>,
|
self: &Arc<Self>,
|
||||||
info_hash: Id20,
|
info_hash: Id20,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
// TODO: this now stores only the routing table, but we also need AT LEAST the same socket address...
|
// TODO: this now stores only the routing table, but we also need AT LEAST the same socket address...
|
||||||
|
|
||||||
|
use futures::{Future, FutureExt};
|
||||||
use librqbit_core::directories::get_configuration_directory;
|
use librqbit_core::directories::get_configuration_directory;
|
||||||
use librqbit_core::spawn_utils::spawn_with_cancel;
|
use librqbit_core::spawn_utils::spawn_with_cancel;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
@ -7,6 +8,7 @@ use std::fs::OpenOptions;
|
||||||
use std::io::{BufReader, BufWriter};
|
use std::io::{BufReader, BufWriter};
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::pin::Pin;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
|
|
||||||
|
|
@ -75,94 +77,102 @@ impl PersistentDht {
|
||||||
Ok(path)
|
Ok(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create(
|
#[inline(never)]
|
||||||
|
pub fn create(
|
||||||
config: Option<PersistentDhtConfig>,
|
config: Option<PersistentDhtConfig>,
|
||||||
cancellation_token: Option<CancellationToken>,
|
cancellation_token: Option<CancellationToken>,
|
||||||
) -> anyhow::Result<Dht> {
|
) -> Pin<Box<dyn Future<Output = anyhow::Result<Dht>> + Send>> {
|
||||||
let mut config = config.unwrap_or_default();
|
async move {
|
||||||
let config_filename = match config.config_filename.take() {
|
let mut config = config.unwrap_or_default();
|
||||||
Some(config_filename) => config_filename,
|
let config_filename = match config.config_filename.take() {
|
||||||
None => Self::default_persistence_filename()?,
|
Some(config_filename) => config_filename,
|
||||||
};
|
None => Self::default_persistence_filename()?,
|
||||||
|
};
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
filename=?config_filename,
|
filename=?config_filename,
|
||||||
"will store DHT routing table periodically",
|
"will store DHT routing table periodically",
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(parent) = config_filename.parent() {
|
if let Some(parent) = config_filename.parent() {
|
||||||
std::fs::create_dir_all(parent)
|
std::fs::create_dir_all(parent)
|
||||||
.with_context(|| format!("error creating dir {:?}", &parent))?;
|
.with_context(|| format!("error creating dir {:?}", &parent))?;
|
||||||
}
|
|
||||||
|
|
||||||
let de = match OpenOptions::new().read(true).open(&config_filename) {
|
|
||||||
Ok(dht_json) => {
|
|
||||||
let reader = BufReader::new(dht_json);
|
|
||||||
match serde_json::from_reader::<_, DhtSerialize<RoutingTable, PeerStore>>(reader) {
|
|
||||||
Ok(r) => {
|
|
||||||
info!(filename=?config_filename, "loaded DHT routing table from");
|
|
||||||
Some(r)
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
warn!(
|
|
||||||
filename=?config_filename,
|
|
||||||
"cannot deserialize routing table: {:#}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(e) => match e.kind() {
|
|
||||||
std::io::ErrorKind::NotFound => None,
|
|
||||||
_ => return Err(e).with_context(|| format!("error reading {config_filename:?}")),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
let (listen_addr, routing_table, peer_store) = de
|
|
||||||
.map(|de| (Some(de.addr), Some(de.table), de.peer_store))
|
|
||||||
.unwrap_or((None, None, None));
|
|
||||||
let peer_id = routing_table.as_ref().map(|r| r.id());
|
|
||||||
|
|
||||||
let dht_config = DhtConfig {
|
let de = match OpenOptions::new().read(true).open(&config_filename) {
|
||||||
peer_id,
|
Ok(dht_json) => {
|
||||||
routing_table,
|
let reader = BufReader::new(dht_json);
|
||||||
listen_addr,
|
match serde_json::from_reader::<_, DhtSerialize<RoutingTable, PeerStore>>(
|
||||||
peer_store,
|
reader,
|
||||||
cancellation_token,
|
) {
|
||||||
..Default::default()
|
Ok(r) => {
|
||||||
};
|
info!(filename=?config_filename, "loaded DHT routing table from");
|
||||||
let dht = DhtState::with_config(dht_config).await?;
|
Some(r)
|
||||||
spawn_with_cancel(
|
}
|
||||||
error_span!("dht_persistence"),
|
Err(e) => {
|
||||||
dht.cancellation_token().clone(),
|
warn!(
|
||||||
{
|
filename=?config_filename,
|
||||||
let dht = dht.clone();
|
"cannot deserialize routing table: {:#}",
|
||||||
let dump_interval = config
|
e
|
||||||
.dump_interval
|
);
|
||||||
.unwrap_or_else(|| Duration::from_secs(3));
|
None
|
||||||
async move {
|
|
||||||
let tempfile_name = {
|
|
||||||
let file_name = format!("dht.json.tmp.{}", std::process::id());
|
|
||||||
let mut tmp = config_filename.clone();
|
|
||||||
tmp.set_file_name(file_name);
|
|
||||||
tmp
|
|
||||||
};
|
|
||||||
|
|
||||||
loop {
|
|
||||||
trace!("sleeping for {:?}", &dump_interval);
|
|
||||||
tokio::time::sleep(dump_interval).await;
|
|
||||||
|
|
||||||
match dump_dht(&dht, &config_filename, &tempfile_name) {
|
|
||||||
Ok(_) => trace!(filename=?config_filename, "dumped DHT"),
|
|
||||||
Err(e) => {
|
|
||||||
error!(filename=?config_filename, "error dumping DHT: {:#}", e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
Err(e) => match e.kind() {
|
||||||
);
|
std::io::ErrorKind::NotFound => None,
|
||||||
|
_ => {
|
||||||
|
return Err(e).with_context(|| format!("error reading {config_filename:?}"))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
let (listen_addr, routing_table, peer_store) = de
|
||||||
|
.map(|de| (Some(de.addr), Some(de.table), de.peer_store))
|
||||||
|
.unwrap_or((None, None, None));
|
||||||
|
let peer_id = routing_table.as_ref().map(|r| r.id());
|
||||||
|
|
||||||
Ok(dht)
|
let dht_config = DhtConfig {
|
||||||
|
peer_id,
|
||||||
|
routing_table,
|
||||||
|
listen_addr,
|
||||||
|
peer_store,
|
||||||
|
cancellation_token,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let dht = DhtState::with_config(dht_config).await?;
|
||||||
|
spawn_with_cancel(
|
||||||
|
error_span!("dht_persistence"),
|
||||||
|
dht.cancellation_token().clone(),
|
||||||
|
{
|
||||||
|
let dht = dht.clone();
|
||||||
|
let dump_interval = config
|
||||||
|
.dump_interval
|
||||||
|
.unwrap_or_else(|| Duration::from_secs(3));
|
||||||
|
async move {
|
||||||
|
let tempfile_name = {
|
||||||
|
let file_name = format!("dht.json.tmp.{}", std::process::id());
|
||||||
|
let mut tmp = config_filename.clone();
|
||||||
|
tmp.set_file_name(file_name);
|
||||||
|
tmp
|
||||||
|
};
|
||||||
|
|
||||||
|
loop {
|
||||||
|
trace!("sleeping for {:?}", &dump_interval);
|
||||||
|
tokio::time::sleep(dump_interval).await;
|
||||||
|
|
||||||
|
match dump_dht(&dht, &config_filename, &tempfile_name) {
|
||||||
|
Ok(_) => trace!(filename=?config_filename, "dumped DHT"),
|
||||||
|
Err(e) => {
|
||||||
|
error!(filename=?config_filename, "error dumping DHT: {:#}", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(dht)
|
||||||
|
}
|
||||||
|
.boxed()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue