Skip to content

Commit f4a65e7

Browse files
authored
[mplex] Small enhancements. (#1785)
* More granular execution of pending flushes. Also replace fnv hashing with nohash-hasher. * Don't forget the pending case. * Simplify. * Use AtomicU32 for connection IDs. * Revert to random u64.
1 parent b48b93f commit f4a65e7

File tree

3 files changed

+99
-57
lines changed

3 files changed

+99
-57
lines changed

muxers/mplex/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ categories = ["network-programming", "asynchronous"]
1111

1212
[dependencies]
1313
bytes = "0.5"
14-
fnv = "1.0"
1514
futures = "0.3.1"
1615
futures_codec = "0.4"
1716
libp2p-core = { version = "0.22.0", path = "../../core" }
1817
log = "0.4"
18+
nohash-hasher = "0.2"
1919
parking_lot = "0.11"
20+
rand = "0.7"
2021
smallvec = "1.4"
2122
unsigned-varint = { version = "0.5", features = ["futures-codec"] }
2223

muxers/mplex/src/codec.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
use libp2p_core::Endpoint;
22-
use futures_codec::{Decoder, Encoder};
23-
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
24-
use std::{fmt, mem};
2521
use bytes::{BufMut, Bytes, BytesMut};
22+
use futures_codec::{Decoder, Encoder};
23+
use libp2p_core::Endpoint;
24+
use std::{fmt, hash::{Hash, Hasher}, io, mem};
2625
use unsigned_varint::{codec, encode};
2726

2827
// Maximum size for a packet: 1MB as per the spec.
@@ -46,7 +45,7 @@ pub(crate) const MAX_FRAME_SIZE: usize = 1024 * 1024;
4645
/// > we initiated the stream, so the local ID has the role `Endpoint::Dialer`.
4746
/// > Conversely, when receiving a frame with a flag identifying the remote as a "sender",
4847
/// > the corresponding local ID has the role `Endpoint::Listener`.
49-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
48+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
5049
pub struct LocalStreamId {
5150
num: u32,
5251
role: Endpoint,
@@ -61,6 +60,14 @@ impl fmt::Display for LocalStreamId {
6160
}
6261
}
6362

63+
impl Hash for LocalStreamId {
64+
fn hash<H: Hasher>(&self, state: &mut H) {
65+
state.write_u32(self.num);
66+
}
67+
}
68+
69+
impl nohash_hasher::IsEnabled for LocalStreamId {}
70+
6471
/// A unique identifier used by the remote node for a substream.
6572
///
6673
/// `RemoteStreamId`s are received with frames from the remote
@@ -161,7 +168,7 @@ impl Codec {
161168

162169
impl Decoder for Codec {
163170
type Item = Frame<RemoteStreamId>;
164-
type Error = IoError;
171+
type Error = io::Error;
165172

166173
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
167174
loop {
@@ -182,7 +189,7 @@ impl Decoder for Codec {
182189
Some(len) => {
183190
if len as usize > MAX_FRAME_SIZE {
184191
let msg = format!("Mplex frame length {} exceeds maximum", len);
185-
return Err(IoError::new(IoErrorKind::InvalidData, msg));
192+
return Err(io::Error::new(io::ErrorKind::InvalidData, msg));
186193
}
187194

188195
self.decoder_state = CodecDecodeState::HasHeaderAndLen(header, len as usize);
@@ -213,7 +220,7 @@ impl Decoder for Codec {
213220
6 => Frame::Reset { stream_id: RemoteStreamId::dialer(num) },
214221
_ => {
215222
let msg = format!("Invalid mplex header value 0x{:x}", header);
216-
return Err(IoError::new(IoErrorKind::InvalidData, msg));
223+
return Err(io::Error::new(io::ErrorKind::InvalidData, msg));
217224
},
218225
};
219226

@@ -222,7 +229,7 @@ impl Decoder for Codec {
222229
},
223230

224231
CodecDecodeState::Poisoned => {
225-
return Err(IoError::new(IoErrorKind::InvalidData, "Mplex codec poisoned"));
232+
return Err(io::Error::new(io::ErrorKind::InvalidData, "Mplex codec poisoned"));
226233
}
227234
}
228235
}
@@ -231,7 +238,7 @@ impl Decoder for Codec {
231238

232239
impl Encoder for Codec {
233240
type Item = Frame<LocalStreamId>;
234-
type Error = IoError;
241+
type Error = io::Error;
235242

236243
fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
237244
let (header, data) = match item {
@@ -266,7 +273,7 @@ impl Encoder for Codec {
266273
let data_len_bytes = encode::usize(data_len, &mut data_buf);
267274

268275
if data_len > MAX_FRAME_SIZE {
269-
return Err(IoError::new(IoErrorKind::InvalidData, "data size exceed maximum"));
276+
return Err(io::Error::new(io::ErrorKind::InvalidData, "data size exceed maximum"));
270277
}
271278

272279
dst.reserve(header_bytes.len() + data_len_bytes.len() + data_len);

0 commit comments

Comments
 (0)