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

Don't poll network unnecessarily. #1977

Merged
merged 10 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions examples/chat-tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

// Create a Swarm to manage peers and events.
let mut swarm = {
let mdns = Mdns::new().await?;
let mdns = Mdns::new(Default::default()).await?;
let mut behaviour = MyBehaviour {
floodsub: Floodsub::new(peer_id.clone()),
mdns,
Expand Down Expand Up @@ -175,4 +175,4 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}
}
}
}
4 changes: 2 additions & 2 deletions examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use libp2p::{
NetworkBehaviour,
identity,
floodsub::{self, Floodsub, FloodsubEvent},
mdns::{Mdns, MdnsEvent},
mdns::{Mdns, MdnsConfig, MdnsEvent},
swarm::NetworkBehaviourEventProcess
};
use std::{error::Error, task::{Context, Poll}};
Expand Down Expand Up @@ -121,7 +121,7 @@ fn main() -> Result<(), Box<dyn Error>> {

// Create a Swarm to manage peers and events
let mut swarm = {
let mdns = task::block_on(Mdns::new())?;
let mdns = task::block_on(Mdns::new(MdnsConfig::default()))?;
let mut behaviour = MyBehaviour {
floodsub: Floodsub::new(local_peer_id.clone()),
mdns,
Expand Down
4 changes: 2 additions & 2 deletions examples/distributed-key-value-store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use libp2p::{
Swarm,
build_development_transport,
identity,
mdns::{Mdns, MdnsEvent},
mdns::{Mdns, MdnsConfig, MdnsEvent},
swarm::NetworkBehaviourEventProcess
};
use std::{error::Error, task::{Context, Poll}};
Expand Down Expand Up @@ -151,7 +151,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// Create a Kademlia behaviour.
let store = MemoryStore::new(local_peer_id.clone());
let kademlia = Kademlia::new(local_peer_id.clone(), store);
let mdns = task::block_on(Mdns::new())?;
let mdns = task::block_on(Mdns::new(MdnsConfig::default()))?;
let behaviour = MyBehaviour { kademlia, mdns };
Swarm::new(transport, behaviour, local_peer_id)
};
Expand Down
65 changes: 32 additions & 33 deletions examples/mdns-passive-discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,42 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use async_std::task;
use libp2p::mdns::service::{MdnsPacket, MdnsService};
use libp2p::{identity, mdns::{Mdns, MdnsConfig, MdnsEvent}, PeerId, Swarm};
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
// This example provides passive discovery of the libp2p nodes on the
// network that send mDNS queries and answers.
task::block_on(async move {
let mut service = MdnsService::new().await?;
loop {
let (srv, packet) = service.next().await;
match packet {
MdnsPacket::Query(query) => {
// We detected a libp2p mDNS query on the network. In a real application, you
// probably want to answer this query by doing `query.respond(...)`.
println!("Detected query from {:?}", query.remote_addr());
}
MdnsPacket::Response(response) => {
// We detected a libp2p mDNS response on the network. Responses are for
// everyone and not just for the requester, which makes it possible to
// passively listen.
for peer in response.discovered_peers() {
println!("Discovered peer {:?}", peer.id());
// These are the self-reported addresses of the peer we just discovered.
for addr in peer.addresses() {
println!(" Address = {:?}", addr);
}
}
#[async_std::main]
async fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();

// Create a random PeerId.
let id_keys = identity::Keypair::generate_ed25519();
let peer_id = PeerId::from(id_keys.public());
println!("Local peer id: {:?}", peer_id);

// Create a transport.
let transport = libp2p::build_development_transport(id_keys)?;

// Create an MDNS network behaviour.
let behaviour = Mdns::new(MdnsConfig::default()).await?;

// Create a Swarm that establishes connections through the given transport.
// Note that the MDNS behaviour itself will not actually inititiate any connections,
// as it only uses UDP.
let mut swarm = Swarm::new(transport, behaviour, peer_id);
Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse()?)?;
romanb marked this conversation as resolved.
Show resolved Hide resolved

loop {
match swarm.next().await {
MdnsEvent::Discovered(peers) => {
for (peer, addr) in peers {
println!("discovered {} {}", peer, addr);
}
MdnsPacket::ServiceDiscovery(query) => {
// The last possibility is a service detection query from DNS-SD.
// Just like `Query`, in a real application you probably want to call
// `query.respond`.
println!("Detected service query from {:?}", query.remote_addr());
}
MdnsEvent::Expired(expired) => {
for (peer, addr) in expired {
println!("expired {} {}", peer, addr);
}
}
service = srv
}
})
}
}
13 changes: 13 additions & 0 deletions protocols/mdns/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# 0.29.0 [unreleased]

- Introduce `MdnsConfig` with configurable TTL of discovered peer
records and configurable multicast query interval. The default
query interval is increased from 20 seconds to 5 minutes, to
significantly reduce bandwidth usage. To ensure timely peer
discovery in the majority of cases, a multicast query is
initiated whenever a change on a network interface is detected,
which includes MDNS initialisation at node startup. If necessary
the MDNS query interval can be reduced via the `MdnsConfig`.
The `MdnsService` has been removed from the public API, making
it compulsory that all uses occur through the `Mdns` `NetworkBehaviour`.
An `MdnsConfig` must now be given to `Mdns::new()`.
[PR 1977](https://github.com/libp2p/rust-libp2p/pull/1977).

- Update `libp2p-swarm`.

# 0.28.1 [2021-02-15]
Expand Down
20 changes: 10 additions & 10 deletions protocols/mdns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"]

[dependencies]
async-io = "1.3.0"
data-encoding = "2.3.1"
async-io = "1.3.1"
data-encoding = "2.3.2"
dns-parser = "0.8.0"
futures = "0.3.8"
if-watch = "0.1.8"
futures = "0.3.13"
if-watch = "0.2.0"
lazy_static = "1.4.0"
libp2p-core = { version = "0.27.0", path = "../../core" }
libp2p-swarm = { version = "0.28.0", path = "../../swarm" }
log = "0.4.11"
rand = "0.7.3"
smallvec = "1.5.0"
socket2 = { version = "0.3.17", features = ["reuseport"] }
log = "0.4.14"
rand = "0.8.3"
smallvec = "1.6.1"
socket2 = { version = "0.3.19", features = ["reuseport"] }
void = "1.0.2"

[dev-dependencies]
async-std = "1.7.0"
async-std = "1.9.0"
if-addrs = "0.6.5"
tokio = { version = "1.0.1", default-features = false, features = ["rt", "rt-multi-thread"] }
tokio = { version = "1.2.0", default-features = false, features = ["rt", "rt-multi-thread"] }
Loading