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

rust-peer: feat/rust/ping instead of identify #228

Closed
Closed
Changes from all commits
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
2 changes: 1 addition & 1 deletion rust-peer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust-peer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust-libp2p-webrtc-peer"
version = "0.1.1"
version = "0.1.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
28 changes: 8 additions & 20 deletions rust-peer/src/main.rs
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ use libp2p::{
kad::{Behaviour as Kademlia, Config as KademliaConfig},
memory_connection_limits,
multiaddr::{Multiaddr, Protocol},
relay,
ping, relay,
request_response::{self, ProtocolSupport},
swarm::{NetworkBehaviour, Swarm, SwarmEvent},
PeerId, StreamProtocol, SwarmBuilder, Transport,
@@ -196,21 +196,7 @@ async fn main() -> Result<()> {
}
SwarmEvent::Behaviour(BehaviourEvent::Identify(e)) => {
info!("BehaviourEvent::Identify {:?}", e);

if let identify::Event::Error { peer_id, error, .. } = e {
match error {
libp2p::swarm::StreamUpgradeError::Timeout => {
// When a browser tab closes, we don't get a swarm event
// maybe there's a way to get this with TransportEvent
// but for now remove the peer from routing table if there's an Identify timeout
swarm.behaviour_mut().kademlia.remove_peer(&peer_id);
info!("Removed {peer_id} from the routing table (if it was in there).");
}
_ => {
debug!("{error}");
}
}
} else if let identify::Event::Received {
if let identify::Event::Received {
info: identify::Info { observed_addr, .. },
..
} = e
@@ -280,6 +266,7 @@ struct Behaviour {
relay: relay::Behaviour,
request_response: request_response::Behaviour<FileExchangeCodec>,
connection_limits: memory_connection_limits::Behaviour,
ping: ping::Behaviour,
}

fn create_swarm(
@@ -318,10 +305,10 @@ fn create_swarm(
gossipsub.subscribe(&gossipsub::IdentTopic::new(GOSSIPSUB_CHAT_FILE_TOPIC))?;
gossipsub.subscribe(&gossipsub::IdentTopic::new(GOSSIPSUB_PEER_DISCOVERY))?;

let identify_config = identify::Behaviour::new(
identify::Config::new("/ipfs/0.1.0".into(), local_key.public())
.with_interval(Duration::from_secs(60)), // do this so we can get timeouts for dropped WebRTC connections
);
let identify_config = identify::Behaviour::new(identify::Config::new(
"/ipfs/0.1.0".into(),
local_key.public(),
));

// Create a Kademlia behaviour.
let cfg = KademliaConfig::new(KADEMLIA_PROTOCOL_NAME);
@@ -349,6 +336,7 @@ fn create_swarm(
request_response::Config::default(),
),
connection_limits: memory_connection_limits::Behaviour::with_max_percentage(0.9),
ping: ping::Behaviour::default(),
};
Ok(SwarmBuilder::with_existing_identity(local_key.clone())
.with_tokio()
Loading