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

Libp2p stream limits #1017

Merged
merged 14 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions crates/subspace-networking/src/node_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::pin::Pin;
use std::sync::atomic::Ordering;
use std::sync::Weak;
use std::time::Duration;
use tokio::time::Sleep;
Expand Down Expand Up @@ -293,9 +294,18 @@ where
endpoint,
..
} => {
let shared = match self.shared_weak.upgrade() {
Some(shared) => shared,
None => {
return;
}
};
Comment on lines +333 to +338
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a note. We can do this right now:

let Some(shared) = self.shared_weak.upgrade() else { return };

Copy link
Member Author

Choose a reason for hiding this comment

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

That is exactly what I wrote initially. Unfortunately rustfmt seems to be unable to format let .. else, which is why I'm not using it yet anywhere. I'll be checking with new Rust releases as we upgrade and will start using it once formatting works.


let is_reserved_peer = self.reserved_peers.contains_key(&peer_id);
debug!(%peer_id, %is_reserved_peer, "Connection established [{num_established} from peer]");

shared.connected_peers_count.fetch_add(1, Ordering::SeqCst);

let (in_connections_number, out_connections_number) = {
let network_info = self.swarm.network_info();
let connections = network_info.connection_counters();
Expand Down Expand Up @@ -346,7 +356,15 @@ where
num_established,
..
} => {
let shared = match self.shared_weak.upgrade() {
Some(shared) => shared,
None => {
return;
}
};
debug!("Connection closed with peer {peer_id} [{num_established} from peer]");

shared.connected_peers_count.fetch_sub(1, Ordering::SeqCst);
}
SwarmEvent::OutgoingConnectionError { peer_id, error } => match error {
DialError::Transport(ref addresses) => {
Expand Down
3 changes: 3 additions & 0 deletions crates/subspace-networking/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use libp2p::gossipsub::error::{PublishError, SubscriptionError};
use libp2p::gossipsub::Sha256Topic;
use libp2p::{Multiaddr, PeerId};
use parking_lot::Mutex;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;

#[derive(Debug)]
Expand Down Expand Up @@ -83,6 +84,7 @@ pub(crate) struct Shared {
pub(crate) id: PeerId,
/// Addresses on which node is listening for incoming requests.
pub(crate) listeners: Mutex<Vec<Multiaddr>>,
pub(crate) connected_peers_count: Arc<AtomicUsize>,
/// Sender end of the channel for sending commands to the swarm.
pub(crate) command_sender: mpsc::Sender<Command>,
}
Expand All @@ -93,6 +95,7 @@ impl Shared {
handlers: Handlers::default(),
id,
listeners: Mutex::default(),
connected_peers_count: Arc::new(AtomicUsize::new(0)),
command_sender,
}
}
Expand Down