Skip to content

Commit 2fb8e6b

Browse files
authored
Expose client internal information (#43)
1 parent 60e2bae commit 2fb8e6b

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
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.6.0] - 2025-01-13
4+
5+
* Expose client internal information
6+
37
## [1.5.0] - 2024-12-04
48

59
* Use updated Service trait

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex-h2"
3-
version = "1.5.0"
3+
version = "1.6.0"
44
license = "MIT OR Apache-2.0"
55
authors = ["Nikolay Kim <[email protected]>"]
66
description = "An HTTP/2 client and server"

src/client/pool.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{cell::Cell, cell::RefCell, collections::VecDeque, fmt, rc::Rc, time::Duration};
22

33
use nanorand::{Rng, WyRand};
4-
use ntex_bytes::{ByteString, PoolId, PoolRef};
4+
use ntex_bytes::ByteString;
55
use ntex_http::{uri::Scheme, HeaderMap, Method};
66
use ntex_io::IoBoxed;
77
use ntex_net::connect::{self as connect, Address, Connect, Connector as DefaultConnector};
@@ -136,7 +136,6 @@ impl Client {
136136
notify(&mut waiters2.borrow_mut());
137137
});
138138
// construct client
139-
io.set_memory_pool(inner.pool);
140139
let client = SimpleClient::with_params(
141140
io,
142141
inner.config.clone(),
@@ -145,6 +144,9 @@ impl Client {
145144
storage,
146145
);
147146
inner.connections.borrow_mut().push(client.clone());
147+
inner
148+
.total_connections
149+
.set(inner.total_connections.get() + 1);
148150
Ok(client)
149151
}
150152
Ok(Err(err)) => Err(ClientError::from(err)),
@@ -154,6 +156,10 @@ impl Client {
154156
for waiter in waiters.borrow_mut().drain(..) {
155157
let _ = waiter.send(());
156158
}
159+
160+
if res.is_err() {
161+
inner.connect_errors.set(inner.connect_errors.get() + 1);
162+
}
157163
let _ = tx.send(res);
158164
});
159165
return rx
@@ -212,6 +218,28 @@ impl Client {
212218
}
213219
}
214220

221+
#[doc(hidden)]
222+
impl Client {
223+
pub fn stat_active_connections(&self) -> usize {
224+
self.inner.connections.borrow().len()
225+
}
226+
227+
pub fn stat_total_connections(&self) -> usize {
228+
self.inner.total_connections.get()
229+
}
230+
231+
pub fn stat_connect_errors(&self) -> usize {
232+
self.inner.connect_errors.get()
233+
}
234+
235+
pub fn stat_connections<F, R>(&self, f: F) -> R
236+
where
237+
F: FnOnce(&[SimpleClient]) -> R,
238+
{
239+
f(&*self.inner.connections.borrow())
240+
}
241+
}
242+
215243
/// Manages http client network connectivity.
216244
///
217245
/// The `ClientBuilder` type uses a builder-like combinator pattern for service
@@ -229,9 +257,10 @@ struct Inner {
229257
config: crate::Config,
230258
authority: ByteString,
231259
connector: Connector,
232-
pool: PoolRef,
233260
connecting: Cell<bool>,
234261
connections: RefCell<Vec<SimpleClient>>,
262+
total_connections: Cell<usize>,
263+
connect_errors: Cell<usize>,
235264
}
236265

237266
impl ClientBuilder {
@@ -268,7 +297,8 @@ impl ClientBuilder {
268297
config: crate::Config::client(),
269298
connecting: Cell::new(false),
270299
connections: Default::default(),
271-
pool: PoolId::P5.pool_ref(),
300+
total_connections: Cell::new(0),
301+
connect_errors: Cell::new(0),
272302
})
273303
}
274304

@@ -289,15 +319,6 @@ impl ClientBuilder {
289319
self
290320
}
291321

292-
/// Set memory pool.
293-
///
294-
/// Use specified memory pool for memory allocations. By default P5
295-
/// memory pool is used.
296-
pub fn memory_pool(mut self, id: PoolId) -> Self {
297-
self.0.pool = id.pool_ref();
298-
self
299-
}
300-
301322
/// Connection timeout.
302323
///
303324
/// i.e. max time to connect to remote host including dns name resolution.
@@ -416,7 +437,6 @@ impl fmt::Debug for Client {
416437
.field("minconn", &self.inner.minconn)
417438
.field("maxconn", &self.inner.maxconn)
418439
.field("max-streams", &self.inner.max_streams)
419-
.field("pool", &self.inner.pool)
420440
.field("config", &self.inner.config)
421441
.finish()
422442
}
@@ -433,7 +453,6 @@ impl fmt::Debug for ClientBuilder {
433453
.field("minconn", &self.0.minconn)
434454
.field("maxconn", &self.0.maxconn)
435455
.field("max-streams", &self.0.max_streams)
436-
.field("pool", &self.0.pool)
437456
.field("config", &self.0.config)
438457
.finish()
439458
}

src/client/simple.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{fmt, rc::Rc};
22

33
use ntex_bytes::ByteString;
44
use ntex_http::{uri::Scheme, HeaderMap, Method};
5-
use ntex_io::{Dispatcher as IoDispatcher, IoBoxed, OnDisconnect};
5+
use ntex_io::{Dispatcher as IoDispatcher, IoBoxed, IoRef, OnDisconnect};
66

77
use crate::connection::Connection;
88
use crate::default::DefaultControlService;
@@ -151,6 +151,12 @@ impl SimpleClient {
151151
pub fn active_streams(&self) -> u32 {
152152
self.0.con.active_streams()
153153
}
154+
155+
#[doc(hidden)]
156+
/// Get access to underlining io object
157+
pub fn io_ref(&self) -> &IoRef {
158+
self.0.con.io()
159+
}
154160
}
155161

156162
impl Drop for SimpleClient {

0 commit comments

Comments
 (0)