Skip to content

Commit 7b21f3a

Browse files
author
germanius
committed
🧵zb : Use async versions of UnixStream when possible
When connecting to a UnixSocket a blocking/threaded API is being used. This commit changes this to use `tokio::net::UnixStream` and `Async<std::os::unix::net::UnixStream>` when possible preventing the use of extra threads. The windows version continues using the blocking/threaded API. The `UnixStream::connect_addr` function from `SocketAddrExt` is no longer used since neither tokio nor async-io supports it. Instead, we use the plain `connect` functions in both environments, since they allow for abstract names when the path is prepended with '\0' (a null byte).
1 parent 09ff54a commit 7b21f3a

1 file changed

Lines changed: 29 additions & 50 deletions

File tree

‎zbus/src/address/transport/unix.rs‎

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ use super::encode_percents;
1515
#[cfg(not(feature = "tokio"))]
1616
use async_io::Async;
1717

18-
#[cfg(target_os = "linux")]
19-
use std::os::linux::net::SocketAddrExt;
20-
#[cfg(unix)]
21-
use std::os::unix::net::SocketAddr;
2218
#[cfg(all(unix, not(feature = "tokio")))]
2319
pub use std::os::unix::net::UnixStream;
2420
#[cfg(all(unix, feature = "tokio"))]
@@ -48,26 +44,18 @@ impl Unix {
4844
self.path
4945
}
5046

51-
#[cfg(all(windows, not(feature = "tokio")))]
47+
#[cfg(any(unix, not(feature = "tokio")))]
5248
fn take_addr(self) -> Result<PathBuf> {
53-
// This is a `path` in case of Windows until uds_windows provides the needed API:
54-
// https://github.com/haraldh/rust_uds_windows/issues/14
49+
// This is a `path` because neither uds_windows, tokio, nor async_io provide
50+
// the SocketAddrExt functions.
5551
match self.take_path() {
5652
UnixSocket::File(path) => Ok(path),
57-
UnixSocket::Dir(_) | UnixSocket::TmpDir(_) => {
58-
// You can't connect to a unix:dir.
59-
Err(Error::Unsupported)
60-
}
61-
}
62-
}
63-
64-
#[cfg(unix)]
65-
fn take_addr(self) -> Result<SocketAddr> {
66-
match self.take_path() {
67-
UnixSocket::File(path) => Ok(SocketAddr::from_pathname(path)?),
6853
#[cfg(target_os = "linux")]
6954
UnixSocket::Abstract(name) => {
70-
Ok(SocketAddr::from_abstract_name(name.as_encoded_bytes())?)
55+
use std::{ffi::OsString, os::unix::ffi::OsStringExt, path::PathBuf};
56+
let mut v = name.into_vec();
57+
v.insert(0, 0);
58+
Ok(PathBuf::from(OsString::from_vec(v)))
7159
}
7260
UnixSocket::Dir(_) | UnixSocket::TmpDir(_) => {
7361
// You can't connect to a unix:dir.
@@ -105,42 +93,33 @@ impl Unix {
10593
pub(super) async fn connect(self) -> Result<Async<UnixStream>> {
10694
let addr = self.take_addr()?;
10795

108-
let stream = crate::Task::spawn_blocking(
109-
move || -> Result<_> {
110-
#[cfg(unix)]
111-
let stream = UnixStream::connect_addr(&addr)?;
112-
#[cfg(windows)]
113-
let stream = UnixStream::connect(addr)?;
114-
stream.set_nonblocking(true)?;
115-
116-
Ok(stream)
117-
},
118-
"unix stream connection",
119-
)
120-
.await??;
121-
122-
Async::new(stream).map_err(|e| Error::InputOutput(e.into()))
96+
#[cfg(unix)]
97+
let stream = Async::<UnixStream>::connect(addr).await;
98+
99+
#[cfg(not(unix))]
100+
let stream = {
101+
let stream = crate::Task::spawn_blocking(
102+
move || -> Result<_> {
103+
let stream = UnixStream::connect(addr)?;
104+
stream.set_nonblocking(true)?;
105+
106+
Ok(stream)
107+
},
108+
"unix stream connection",
109+
)
110+
.await??;
111+
Async::new(stream)
112+
};
113+
114+
stream.map_err(|e| Error::InputOutput(e.into()))
123115
}
124116

125117
#[cfg(all(unix, feature = "tokio"))]
126118
pub(super) async fn connect(self) -> Result<UnixStream> {
127119
let addr = self.take_addr()?;
128-
129-
let stream = crate::Task::spawn_blocking(
130-
move || -> Result<_> {
131-
#[cfg(unix)]
132-
let stream = UnixStream::connect_addr(&addr)?;
133-
#[cfg(windows)]
134-
let stream = UnixStream::connect(addr)?;
135-
stream.set_nonblocking(true)?;
136-
137-
Ok(stream)
138-
},
139-
"unix stream connection",
140-
)
141-
.await??;
142-
143-
UnixStream::from_std(stream).map_err(|e| Error::InputOutput(e.into()))
120+
UnixStream::connect(addr)
121+
.await
122+
.map_err(|e| Error::InputOutput(e.into()))
144123
}
145124
}
146125

0 commit comments

Comments
 (0)