Skip to content

Commit 1b385e9

Browse files
committed
fixes after PR review
1 parent 29873e1 commit 1b385e9

File tree

4 files changed

+49
-51
lines changed

4 files changed

+49
-51
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ description = "small embeddable ipfs implementation"
1111
repository = "https://github.com/ipfs-rust/ipfs-embed"
1212

1313
[features]
14-
default = ["async_global"]
14+
default = ["async_global", "rsa"]
15+
rsa = ["libp2p/rsa"]
1516
async_global = ["async-global-executor", "libp2p/tcp-async-io", "libp2p/dns-async-std", "libp2p/mdns-async-io"]
1617
tokio = ["tokio-crate", "libp2p/tcp-tokio", "libp2p/dns-tokio", "libp2p/mdns-tokio"]
1718
telemetry = ["tide", "async_global"]

src/net/mod.rs

+45-49
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use chrono::{DateTime, Utc};
2323
use fnv::{FnvHashMap, FnvHashSet};
2424
use futures::{
2525
channel::{
26-
mpsc::{self, Receiver, Sender, UnboundedReceiver, UnboundedSender},
26+
mpsc::{self, Receiver, Sender, TrySendError, UnboundedReceiver, UnboundedSender},
2727
oneshot,
2828
},
2929
future::{self, Either},
@@ -42,7 +42,7 @@ use libp2p::tcp::TokioTcpConfig as TcpConfig;
4242
use libp2p::{
4343
core::{
4444
either::EitherTransport,
45-
transport::Transport,
45+
transport::{ListenerId, Transport},
4646
upgrade::{SelectUpgrade, Version},
4747
},
4848
identity::ed25519::PublicKey,
@@ -273,23 +273,17 @@ impl NetworkService {
273273
}
274274

275275
fn cmd(&mut self, msg: NetworkCommand) -> Option<(NetworkCommand, &'static str)> {
276-
match self.cmd.try_send(msg) {
277-
Ok(_) => None,
278-
Err(err) => {
279-
let reason = if err.is_disconnected() {
280-
"receiver went away"
281-
} else {
282-
"channel is full"
283-
};
284-
let val = err.into_inner();
285-
tracing::warn!("failed IPFS swarm command {:?}: {}", val, reason);
286-
Some((val, reason))
287-
}
288-
}
276+
Self::handle_send_result(self.cmd.try_send(msg))
289277
}
290278

291279
fn cmd_shared(&self, msg: NetworkCommand) -> Option<(NetworkCommand, &'static str)> {
292-
match self.cmd.clone().try_send(msg) {
280+
Self::handle_send_result(self.cmd.clone().try_send(msg))
281+
}
282+
283+
fn handle_send_result(
284+
res: Result<(), TrySendError<NetworkCommand>>,
285+
) -> Option<(NetworkCommand, &'static str)> {
286+
match res {
293287
Ok(_) => None,
294288
Err(err) => {
295289
let reason = if err.is_disconnected() {
@@ -328,7 +322,7 @@ impl NetworkService {
328322
}
329323

330324
pub fn external_addresses(&self) -> Vec<AddressRecord> {
331-
self.external.cloned()
325+
self.external.get_cloned()
332326
}
333327

334328
pub fn add_address(&mut self, peer: PeerId, addr: Multiaddr) {
@@ -504,6 +498,7 @@ impl NetworkService {
504498
async { rx.await? }.right_future()
505499
}
506500

501+
// This cannot take `&mut self` due to trait constraints, so it needs to use the less efficient cmd_shared.
507502
pub fn get(&self, cid: Cid, providers: Vec<PeerId>) -> impl Future<Output = Result<GetQuery>> {
508503
let (tx, rx) = oneshot::channel();
509504
if let Some((_, err)) = self.cmd_shared(NetworkCommand::Get(cid, providers, tx)) {
@@ -512,6 +507,7 @@ impl NetworkService {
512507
async { Ok(rx.await?) }.right_future()
513508
}
514509

510+
// This cannot take `&mut self` due to trait constraints, so it needs to use the less efficient cmd_shared.
515511
pub fn sync(
516512
&self,
517513
cid: Cid,
@@ -581,6 +577,7 @@ async fn poll_swarm<P: StoreParams>(
581577
behaviour::NetworkBackendBehaviourEvent::Kad(e) => {
582578
let mut bootstrap_complete = *bootstrapped.read();
583579
let bootstrap_old = bootstrap_complete;
580+
// DO NOT HOLD bootstrapped LOCK ACROSS ARBITRARY CODE
584581
swarm.inject_kad_event(e, &mut bootstrap_complete, &mut queries);
585582
if bootstrap_complete != bootstrap_old {
586583
*bootstrapped.write() = bootstrap_complete;
@@ -618,33 +615,7 @@ async fn poll_swarm<P: StoreParams>(
618615
swarm.behaviour_mut().swarm_events(tx);
619616
match swarm.listen_on(addr.clone()) {
620617
Ok(listener) => executor
621-
.spawn(
622-
rx.take_while(move |event| match event {
623-
Event::ListenerClosed(id) if *id == listener => {
624-
future::ready(false)
625-
}
626-
Event::NewListenAddr(id, addr) if *id == listener => {
627-
future::ready(
628-
response
629-
.unbounded_send(ListenerEvent::NewListenAddr(
630-
addr.clone(),
631-
))
632-
.is_ok(),
633-
)
634-
}
635-
Event::ExpiredListenAddr(id, addr) if *id == listener => {
636-
future::ready(
637-
response
638-
.unbounded_send(ListenerEvent::ExpiredListenAddr(
639-
addr.clone(),
640-
))
641-
.is_ok(),
642-
)
643-
}
644-
_ => future::ready(true),
645-
})
646-
.for_each(|_| future::ready(())),
647-
)
618+
.spawn(forward_listener_events(listener, response, rx))
648619
.detach(),
649620
Err(error) => {
650621
response
@@ -777,6 +748,28 @@ async fn poll_swarm<P: StoreParams>(
777748
}
778749
}
779750

751+
fn forward_listener_events(
752+
listener: ListenerId,
753+
response: UnboundedSender<ListenerEvent>,
754+
rx: UnboundedReceiver<Event>,
755+
) -> impl Future<Output = ()> {
756+
rx.take_while(move |event| match event {
757+
Event::ListenerClosed(id) if *id == listener => future::ready(false),
758+
Event::NewListenAddr(id, addr) if *id == listener => future::ready(
759+
response
760+
.unbounded_send(ListenerEvent::NewListenAddr(addr.clone()))
761+
.is_ok(),
762+
),
763+
Event::ExpiredListenAddr(id, addr) if *id == listener => future::ready(
764+
response
765+
.unbounded_send(ListenerEvent::ExpiredListenAddr(addr.clone()))
766+
.is_ok(),
767+
),
768+
_ => future::ready(true),
769+
})
770+
.for_each(|_| future::ready(()))
771+
}
772+
780773
#[derive(Debug)]
781774
pub struct GetQuery {
782775
swarm: Sender<NetworkCommand>,
@@ -790,7 +783,7 @@ impl Future for GetQuery {
790783
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
791784
match Pin::new(&mut self.rx).poll(cx) {
792785
Poll::Ready(Ok(result)) => Poll::Ready(result),
793-
Poll::Ready(Err(err)) => Poll::Ready(Err(anyhow!("{}", err))),
786+
Poll::Ready(Err(err)) => Poll::Ready(Err(err.into())),
794787
Poll::Pending => Poll::Pending,
795788
}
796789
}
@@ -799,7 +792,9 @@ impl Future for GetQuery {
799792
impl Drop for GetQuery {
800793
fn drop(&mut self) {
801794
if let Err(err) = self.swarm.try_send(NetworkCommand::CancelQuery(self.id)) {
802-
tracing::warn!("cannot cancel dropped GetQuery: {}", err.into_send_error());
795+
if !err.is_disconnected() {
796+
tracing::warn!("cannot cancel dropped GetQuery: {}", err.into_send_error());
797+
}
803798
}
804799
}
805800
}
@@ -852,10 +847,11 @@ impl Stream for SyncQuery {
852847

853848
impl Drop for SyncQuery {
854849
fn drop(&mut self) {
855-
if let Some(id) = self.id.take() {
856-
let swarm = self.swarm.as_mut().unwrap();
850+
if let (Some(id), Some(mut swarm)) = (self.id.take(), self.swarm.take()) {
857851
if let Err(err) = swarm.try_send(NetworkCommand::CancelQuery(id)) {
858-
tracing::warn!("cannot cancel dropped SyncQuery: {}", err.into_send_error());
852+
if !err.is_disconnected() {
853+
tracing::warn!("cannot cancel dropped SyncQuery: {}", err.into_send_error());
854+
}
859855
}
860856
}
861857
}

src/variable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<T: Copy> Reader<T> {
5656
}
5757

5858
impl<T: Clone> Reader<T> {
59-
pub fn cloned(&self) -> T {
59+
pub fn get_cloned(&self) -> T {
6060
self.0.value.read().clone()
6161
}
6262
}

0 commit comments

Comments
 (0)