Skip to content

chore: deprecate async-io support in mDNS crate #5958

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion 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 libp2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ full = [
"upnp",
]

async-std = [ "libp2p-swarm/async-std", "libp2p-mdns?/async-io", "libp2p-tcp?/async-io", "libp2p-dns?/async-std", "libp2p-quic?/async-std",]
async-std = [ "libp2p-swarm/async-std", "libp2p-tcp?/async-io", "libp2p-dns?/async-std", "libp2p-quic?/async-std",]
autonat = ["dep:libp2p-autonat"]
cbor = ["libp2p-request-response?/cbor"]
dcutr = ["dep:libp2p-dcutr", "libp2p-metrics?/dcutr"]
Expand Down
9 changes: 1 addition & 8 deletions protocols/mdns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"]

[dependencies]
async-std = { version = "1.12.0", optional = true }
async-io = { version = "2.3.3", optional = true }
futures = { workspace = true }
if-watch = { workspace = true }
Expand All @@ -27,19 +26,13 @@ hickory-proto = { workspace = true, features = ["mdns"] }

[features]
tokio = ["dep:tokio", "if-watch/tokio"]
async-io = ["dep:async-io", "dep:async-std", "if-watch/smol"]

[dev-dependencies]
async-std = { version = "1.9.0", features = ["attributes"] }
libp2p-swarm = { workspace = true, features = ["tokio", "async-std"] }
libp2p-swarm = { workspace = true, features = ["tokio"] }
tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread", "time"] }
libp2p-swarm-test = { path = "../../swarm-test" }
tracing-subscriber = { workspace = true, features = ["env-filter"] }

[[test]]
name = "use-async-std"
required-features = ["async-io"]

[[test]]
name = "use-tokio"
required-features = ["tokio"]
Expand Down
38 changes: 0 additions & 38 deletions protocols/mdns/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,44 +78,6 @@ pub trait Abort {
fn abort(self);
}

/// The type of a [`Behaviour`] using the `async-io` implementation.
#[cfg(feature = "async-io")]
pub mod async_io {
use std::future::Future;

use async_std::task::JoinHandle;
use if_watch::smol::IfWatcher;

use super::Provider;
use crate::behaviour::{socket::asio::AsyncUdpSocket, timer::asio::AsyncTimer, Abort};

#[doc(hidden)]
pub enum AsyncIo {}

impl Provider for AsyncIo {
type Socket = AsyncUdpSocket;
type Timer = AsyncTimer;
type Watcher = IfWatcher;
type TaskHandle = JoinHandle<()>;

fn new_watcher() -> Result<Self::Watcher, std::io::Error> {
IfWatcher::new()
}

fn spawn(task: impl Future<Output = ()> + Send + 'static) -> JoinHandle<()> {
async_std::task::spawn(task)
}
}

impl Abort for JoinHandle<()> {
fn abort(self) {
async_std::task::spawn(self.cancel());
}
}

pub type Behaviour = super::Behaviour<AsyncIo>;
}

/// The type of a [`Behaviour`] using the `tokio` implementation.
#[cfg(feature = "tokio")]
pub mod tokio {
Expand Down
43 changes: 0 additions & 43 deletions protocols/mdns/src/behaviour/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,49 +50,6 @@ pub trait AsyncSocket: Unpin + Send + 'static {
) -> Poll<Result<(), Error>>;
}

#[cfg(feature = "async-io")]
pub(crate) mod asio {
use async_io::Async;
use futures::FutureExt;

use super::*;

/// AsyncIo UdpSocket
pub(crate) type AsyncUdpSocket = Async<UdpSocket>;
impl AsyncSocket for AsyncUdpSocket {
fn from_std(socket: UdpSocket) -> std::io::Result<Self> {
Async::new(socket)
}

fn poll_read(
&mut self,
cx: &mut Context,
buf: &mut [u8],
) -> Poll<Result<(usize, SocketAddr), Error>> {
// Poll receive socket.
futures::ready!(self.poll_readable(cx))?;
match self.recv_from(buf).now_or_never() {
Some(data) => Poll::Ready(data),
None => Poll::Pending,
}
}

fn poll_write(
&mut self,
cx: &mut Context,
packet: &[u8],
to: SocketAddr,
) -> Poll<Result<(), Error>> {
futures::ready!(self.poll_writable(cx))?;
match self.send_to(packet, to).now_or_never() {
Some(Ok(_)) => Poll::Ready(Ok(())),
Some(Err(err)) => Poll::Ready(Err(err)),
None => Poll::Pending,
}
}
}
}

#[cfg(feature = "tokio")]
pub(crate) mod tokio {
use ::tokio::{io::ReadBuf, net::UdpSocket as TkUdpSocket};
Expand Down
45 changes: 1 addition & 44 deletions protocols/mdns/src/behaviour/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::time::{Duration, Instant};

/// Simple wrapper for the different type of timers
#[derive(Debug)]
#[cfg(any(feature = "async-io", feature = "tokio"))]
#[cfg(feature = "tokio")]
pub struct Timer<T> {
inner: T,
}
Expand All @@ -40,49 +40,6 @@ pub trait Builder: Send + Unpin + 'static {
fn interval_at(start: Instant, duration: Duration) -> Self;
}

#[cfg(feature = "async-io")]
pub(crate) mod asio {
use std::{
pin::Pin,
task::{Context, Poll},
};

use async_io::Timer as AsioTimer;
use futures::Stream;

use super::*;

/// Async Timer
pub(crate) type AsyncTimer = Timer<AsioTimer>;
impl Builder for AsyncTimer {
fn at(instant: Instant) -> Self {
Self {
inner: AsioTimer::at(instant),
}
}

fn interval(duration: Duration) -> Self {
Self {
inner: AsioTimer::interval(duration),
}
}

fn interval_at(start: Instant, duration: Duration) -> Self {
Self {
inner: AsioTimer::interval_at(start, duration),
}
}
}

impl Stream for AsyncTimer {
type Item = Instant;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.inner).poll_next(cx)
}
}
}

#[cfg(feature = "tokio")]
pub(crate) mod tokio {
use std::{
Expand Down
6 changes: 2 additions & 4 deletions protocols/mdns/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
//!
//! # Usage
//!
//! This crate provides a `Mdns` and `TokioMdns`, depending on the enabled features, which
//! implements the `NetworkBehaviour` trait. This struct will automatically discover other
//! This crate provides `TokioMdns`
//! which implements the `NetworkBehaviour` trait. This struct will automatically discover other
//! libp2p nodes on the local network.

#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
Expand All @@ -40,8 +40,6 @@ use std::{
};

mod behaviour;
#[cfg(feature = "async-io")]
pub use crate::behaviour::async_io;
#[cfg(feature = "tokio")]
pub use crate::behaviour::tokio;
pub use crate::behaviour::{Behaviour, Event};
Expand Down
176 changes: 0 additions & 176 deletions protocols/mdns/tests/use-async-std.rs

This file was deleted.

Loading