Skip to content

Commit 40987fa

Browse files
authored
Generate unique id for simple client (#58)
1 parent 7112763 commit 40987fa

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## [1.10.0] - 2025-06-29
4+
5+
* Generate unique id for simple client
6+
37
## [1.9.0] - 2025-06-13
48

59
* Update nanorand 0.8

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex-h2"
3-
version = "1.9.0"
3+
version = "1.10.0"
44
license = "MIT OR Apache-2.0"
55
authors = ["Nikolay Kim <[email protected]>"]
66
description = "An HTTP/2 client and server"
@@ -36,7 +36,7 @@ fxhash = "0.2"
3636
log = "0.4"
3737
pin-project-lite = "0.2"
3838
thiserror = "2"
39-
nanorand = { version = "0.8", default-features = false, features = ["std", "wyrand", "entropy"] }
39+
nanorand = { version = "0.8", default-features = false, features = ["std", "wyrand", "entropy", "tls"] }
4040

4141
[dev-dependencies]
4242
# Fuzzing

src/client/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl From<ConnectionError> for ClientError {
4343

4444
impl From<ntex_util::channel::Canceled> for ClientError {
4545
fn from(err: ntex_util::channel::Canceled) -> Self {
46-
Self::Disconnected(std::io::Error::new(std::io::ErrorKind::Other, err))
46+
Self::Disconnected(std::io::Error::other(err))
4747
}
4848
}
4949

src/client/simple.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{fmt, future::Future, pin::Pin, rc::Rc, task::Context, task::Poll};
22

3+
use nanorand::Rng;
34
use ntex_bytes::ByteString;
45
use ntex_http::{uri::Scheme, HeaderMap, Method};
56
use ntex_io::{Dispatcher as IoDispatcher, IoBoxed, IoRef, OnDisconnect};
@@ -18,6 +19,7 @@ pub struct SimpleClient(Rc<ClientRef>);
1819

1920
/// Http2 client
2021
struct ClientRef {
22+
id: ByteString,
2123
con: Connection,
2224
authority: ByteString,
2325
storage: InflightStorage,
@@ -69,9 +71,16 @@ impl SimpleClient {
6971
con,
7072
authority,
7173
storage,
74+
id: gen_id(),
7275
}))
7376
}
7477

78+
#[inline]
79+
/// Get client id
80+
pub fn id(&self) -> &ByteString {
81+
&self.0.id
82+
}
83+
7584
#[inline]
7685
/// Get io tag
7786
pub fn tag(&self) -> &'static str {
@@ -256,3 +265,15 @@ impl Future for ClientDisconnect {
256265
Poll::Pending
257266
}
258267
}
268+
269+
fn gen_id() -> ByteString {
270+
const BASE: &str = "abcdefghijklmnopqrstuvwxyz234567";
271+
272+
let mut rng = nanorand::tls_rng();
273+
let mut id = String::with_capacity(16);
274+
for _ in 0..16 {
275+
let idx = rng.generate_range::<usize, _>(..BASE.len());
276+
id.push_str(&BASE[idx..idx + 1]);
277+
}
278+
ByteString::from(id)
279+
}

src/hpack/table.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,10 @@ impl Table {
303303

304304
let pos_idx = 0usize.wrapping_sub(self.inserted);
305305

306-
let prev = mem::replace(
307-
&mut self.indices[probe],
308-
Some(Pos {
309-
index: pos_idx,
310-
hash,
311-
}),
312-
);
306+
let prev = self.indices[probe].replace(Pos {
307+
index: pos_idx,
308+
hash,
309+
});
313310

314311
if let Some(mut prev) = prev {
315312
// Shift forward

0 commit comments

Comments
 (0)