@@ -22,9 +22,8 @@ mod tls;
2222use tcp:: { TcpConnWrapper , TcpConnection } ;
2323use 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
2928type HttpPool = DashMap < SocketAddr , Pool < TcpStream , std:: io:: Error > > ;
3029type HttpsPool = DashMap < SocketAddr , Pool < TlsStream < TcpStream > , Error > > ;
@@ -33,6 +32,7 @@ type HttpsPool = DashMap<SocketAddr, Pool<TlsStream<TcpStream>, Error>>;
3332pub struct H1Client {
3433 http_pools : HttpPool ,
3534 https_pools : HttpsPool ,
35+ max_concurrent_connections : usize ,
3636}
3737
3838impl 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