Skip to content

Commit 35e6546

Browse files
committed
feat: h1 add with_max_connections
Allows setting the max number of current connections maintained.
1 parent be5462f commit 35e6546

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/h1/mod.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ mod tls;
2222
use tcp::{TcpConnWrapper, TcpConnection};
2323
use tls::{TlsConnWrapper, TlsConnection};
2424

25-
// TODO: Move this to a parameter. This current number is based on a few
26-
// random benchmarks and see whatever gave decent perf vs resource use.
27-
static MAX_CONCURRENT_CONNECTIONS: usize = 50;
25+
// This number is based on a few random benchmarks and see whatever gave decent perf vs resource use.
26+
const DEFAULT_MAX_CONCURRENT_CONNECTIONS: usize = 50;
2827

2928
type HttpPool = DashMap<SocketAddr, Pool<TcpStream, std::io::Error>>;
3029
type HttpsPool = DashMap<SocketAddr, Pool<TlsStream<TcpStream>, Error>>;
@@ -33,6 +32,7 @@ type HttpsPool = DashMap<SocketAddr, Pool<TlsStream<TcpStream>, Error>>;
3332
pub struct H1Client {
3433
http_pools: HttpPool,
3534
https_pools: HttpsPool,
35+
max_concurrent_connections: usize,
3636
}
3737

3838
impl Debug for H1Client {
@@ -53,6 +53,16 @@ impl H1Client {
5353
Self {
5454
http_pools: DashMap::new(),
5555
https_pools: DashMap::new(),
56+
max_concurrent_connections: DEFAULT_MAX_CONCURRENT_CONNECTIONS,
57+
}
58+
}
59+
60+
/// Create a new instance.
61+
pub fn with_max_connections(max: usize) -> Self {
62+
Self {
63+
http_pools: DashMap::new(),
64+
https_pools: DashMap::new(),
65+
max_concurrent_connections: max,
5666
}
5767
}
5868
}
@@ -96,8 +106,10 @@ impl HttpClient for H1Client {
96106
pool
97107
} else {
98108
let manager = TcpConnection::new(addr);
99-
let pool =
100-
Pool::<TcpStream, std::io::Error>::new(manager, MAX_CONCURRENT_CONNECTIONS);
109+
let pool = Pool::<TcpStream, std::io::Error>::new(
110+
manager,
111+
self.max_concurrent_connections,
112+
);
101113
self.http_pools.insert(addr, pool);
102114
self.http_pools.get(&addr).unwrap()
103115
};
@@ -114,7 +126,7 @@ impl HttpClient for H1Client {
114126
let manager = TlsConnection::new(host.clone(), addr);
115127
let pool = Pool::<TlsStream<TcpStream>, Error>::new(
116128
manager,
117-
MAX_CONCURRENT_CONNECTIONS,
129+
self.max_concurrent_connections,
118130
);
119131
self.https_pools.insert(addr, pool);
120132
self.https_pools.get(&addr).unwrap()

0 commit comments

Comments
 (0)