Skip to content

Commit 2098be0

Browse files
authored
Add Client::client() method, returns available client from the pool (#47)
1 parent 114afac commit 2098be0

File tree

4 files changed

+44
-34
lines changed

4 files changed

+44
-34
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.8.0] - 2025-01-31
4+
5+
* Add Client::client() method, returns available client from the pool
6+
37
## [1.7.0] - 2025-01-30
48

59
* Add disconnect on drop request for client

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.7.0"
3+
version = "1.8.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: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ impl Client {
5656
ClientBuilder::with_default(addr)
5757
}
5858

59-
#[inline]
6059
/// Send request to the peer
6160
pub async fn send(
6261
&self,
@@ -65,41 +64,46 @@ impl Client {
6564
headers: HeaderMap,
6665
eof: bool,
6766
) -> Result<(SendStream, RecvStream), ClientError> {
67+
self.client()
68+
.await?
69+
.send(method, path, headers, eof)
70+
.await
71+
.map_err(From::from)
72+
}
73+
74+
/// Get client from the pool
75+
pub async fn client(&self) -> Result<SimpleClient, ClientError> {
6876
loop {
6977
let (client, num) = self.get_client();
7078

7179
if let Some(client) = client {
72-
return client
73-
.send(method, path, headers, eof)
74-
.await
75-
.map_err(From::from);
80+
return Ok(client);
81+
} else {
82+
self.connect(num).await?;
7683
}
84+
}
85+
}
7786

78-
// can create new connection
79-
if !self.inner.connecting.get()
80-
&& (num < self.inner.maxconn
81-
|| (self.inner.minconn > 0 && num < self.inner.minconn))
82-
{
83-
// create new connection
84-
self.inner.connecting.set(true);
85-
86-
return self
87-
.create_connection()
88-
.await?
89-
.send(method, path, headers, eof)
90-
.await
91-
.map_err(From::from);
92-
} else {
93-
log::debug!(
94-
"New connection is being established {:?} or number of existing cons {} greater than allowed {}",
95-
self.inner.connecting.get(), num, self.inner.maxconn);
87+
async fn connect(&self, num: usize) -> Result<(), ClientError> {
88+
// can create new connection
89+
if !self.inner.connecting.get()
90+
&& (num < self.inner.maxconn || (self.inner.minconn > 0 && num < self.inner.minconn))
91+
{
92+
// create new connection
93+
self.inner.connecting.set(true);
9694

97-
// wait for available connection
98-
let (tx, rx) = oneshot::channel();
99-
self.waiters.borrow_mut().push_back(tx);
100-
let _ = rx.await;
101-
}
95+
self.create_connection().await?;
96+
} else {
97+
log::debug!(
98+
"New connection is being established {:?} or number of existing cons {} greater than allowed {}",
99+
self.inner.connecting.get(), num, self.inner.maxconn);
100+
101+
// wait for available connection
102+
let (tx, rx) = oneshot::channel();
103+
self.waiters.borrow_mut().push_back(tx);
104+
let _ = rx.await?;
102105
}
106+
Ok(())
103107
}
104108

105109
fn get_client(&self) -> (Option<SimpleClient>, usize) {
@@ -113,9 +117,10 @@ impl Client {
113117
} else if connections[idx].is_disconnecting() {
114118
let con = connections.remove(idx);
115119
let timeout = self.inner.disconnect_timeout;
116-
ntex_util::spawn(async move {
120+
let f = ntex_util::spawn(async move {
117121
let _ = con.disconnect().disconnect_timeout(timeout).await;
118122
});
123+
drop(f);
119124
} else {
120125
idx += 1;
121126
}
@@ -151,7 +156,7 @@ impl Client {
151156
}
152157
}
153158

154-
async fn create_connection(&self) -> Result<SimpleClient, ClientError> {
159+
async fn create_connection(&self) -> Result<(), ClientError> {
155160
let (tx, rx) = oneshot::channel();
156161

157162
let inner = self.inner.clone();
@@ -173,11 +178,11 @@ impl Client {
173178
inner.authority.clone(),
174179
storage,
175180
);
176-
inner.connections.borrow_mut().push(client.clone());
181+
inner.connections.borrow_mut().push(client);
177182
inner
178183
.total_connections
179184
.set(inner.total_connections.get() + 1);
180-
Ok(client)
185+
Ok(())
181186
}
182187
Ok(Err(err)) => Err(ClientError::from(err)),
183188
Err(_) => Err(ClientError::HandshakeTimeout),

src/dispatcher.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,12 @@ where
136136
if let Err(e) = self.inner.publish.poll(cx) {
137137
let inner = self.inner.clone();
138138
let con = self.connection.connection();
139-
ntex_util::spawn(async move {
139+
let f = ntex_util::spawn(async move {
140140
if inner.control.call_nowait(Control::error(e)).await.is_ok() {
141141
con.close();
142142
}
143143
});
144+
drop(f);
144145
}
145146
self.inner.control.poll(cx).map_err(|_| ())
146147
}

0 commit comments

Comments
 (0)