Skip to content

Commit

Permalink
Expose client internal information (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 authored Jan 13, 2025
1 parent 60e2bae commit 2fb8e6b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [1.6.0] - 2025-01-13

* Expose client internal information

## [1.5.0] - 2024-12-04

* Use updated Service trait
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-h2"
version = "1.5.0"
version = "1.6.0"
license = "MIT OR Apache-2.0"
authors = ["Nikolay Kim <[email protected]>"]
description = "An HTTP/2 client and server"
Expand Down
49 changes: 34 additions & 15 deletions src/client/pool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cell::Cell, cell::RefCell, collections::VecDeque, fmt, rc::Rc, time::Duration};

use nanorand::{Rng, WyRand};
use ntex_bytes::{ByteString, PoolId, PoolRef};
use ntex_bytes::ByteString;
use ntex_http::{uri::Scheme, HeaderMap, Method};
use ntex_io::IoBoxed;
use ntex_net::connect::{self as connect, Address, Connect, Connector as DefaultConnector};
Expand Down Expand Up @@ -136,7 +136,6 @@ impl Client {
notify(&mut waiters2.borrow_mut());
});
// construct client
io.set_memory_pool(inner.pool);
let client = SimpleClient::with_params(
io,
inner.config.clone(),
Expand All @@ -145,6 +144,9 @@ impl Client {
storage,
);
inner.connections.borrow_mut().push(client.clone());
inner
.total_connections
.set(inner.total_connections.get() + 1);
Ok(client)
}
Ok(Err(err)) => Err(ClientError::from(err)),
Expand All @@ -154,6 +156,10 @@ impl Client {
for waiter in waiters.borrow_mut().drain(..) {
let _ = waiter.send(());
}

if res.is_err() {
inner.connect_errors.set(inner.connect_errors.get() + 1);
}
let _ = tx.send(res);
});
return rx
Expand Down Expand Up @@ -212,6 +218,28 @@ impl Client {
}
}

#[doc(hidden)]
impl Client {
pub fn stat_active_connections(&self) -> usize {
self.inner.connections.borrow().len()
}

pub fn stat_total_connections(&self) -> usize {
self.inner.total_connections.get()
}

pub fn stat_connect_errors(&self) -> usize {
self.inner.connect_errors.get()
}

pub fn stat_connections<F, R>(&self, f: F) -> R
where
F: FnOnce(&[SimpleClient]) -> R,
{
f(&*self.inner.connections.borrow())
}
}

/// Manages http client network connectivity.
///
/// The `ClientBuilder` type uses a builder-like combinator pattern for service
Expand All @@ -229,9 +257,10 @@ struct Inner {
config: crate::Config,
authority: ByteString,
connector: Connector,
pool: PoolRef,
connecting: Cell<bool>,
connections: RefCell<Vec<SimpleClient>>,
total_connections: Cell<usize>,
connect_errors: Cell<usize>,
}

impl ClientBuilder {
Expand Down Expand Up @@ -268,7 +297,8 @@ impl ClientBuilder {
config: crate::Config::client(),
connecting: Cell::new(false),
connections: Default::default(),
pool: PoolId::P5.pool_ref(),
total_connections: Cell::new(0),
connect_errors: Cell::new(0),
})
}

Expand All @@ -289,15 +319,6 @@ impl ClientBuilder {
self
}

/// Set memory pool.
///
/// Use specified memory pool for memory allocations. By default P5
/// memory pool is used.
pub fn memory_pool(mut self, id: PoolId) -> Self {
self.0.pool = id.pool_ref();
self
}

/// Connection timeout.
///
/// i.e. max time to connect to remote host including dns name resolution.
Expand Down Expand Up @@ -416,7 +437,6 @@ impl fmt::Debug for Client {
.field("minconn", &self.inner.minconn)
.field("maxconn", &self.inner.maxconn)
.field("max-streams", &self.inner.max_streams)
.field("pool", &self.inner.pool)
.field("config", &self.inner.config)
.finish()
}
Expand All @@ -433,7 +453,6 @@ impl fmt::Debug for ClientBuilder {
.field("minconn", &self.0.minconn)
.field("maxconn", &self.0.maxconn)
.field("max-streams", &self.0.max_streams)
.field("pool", &self.0.pool)
.field("config", &self.0.config)
.finish()
}
Expand Down
8 changes: 7 additions & 1 deletion src/client/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{fmt, rc::Rc};

use ntex_bytes::ByteString;
use ntex_http::{uri::Scheme, HeaderMap, Method};
use ntex_io::{Dispatcher as IoDispatcher, IoBoxed, OnDisconnect};
use ntex_io::{Dispatcher as IoDispatcher, IoBoxed, IoRef, OnDisconnect};

use crate::connection::Connection;
use crate::default::DefaultControlService;
Expand Down Expand Up @@ -151,6 +151,12 @@ impl SimpleClient {
pub fn active_streams(&self) -> u32 {
self.0.con.active_streams()
}

#[doc(hidden)]
/// Get access to underlining io object
pub fn io_ref(&self) -> &IoRef {
self.0.con.io()
}
}

impl Drop for SimpleClient {
Expand Down

0 comments on commit 2fb8e6b

Please sign in to comment.