1
1
//! http-client implementation for async-h1, with connecton pooling ("Keep-Alive").
2
2
3
+ use std:: fmt:: Debug ;
3
4
use std:: net:: SocketAddr ;
4
- use std:: { fmt:: Debug , sync:: Arc } ;
5
5
6
6
use async_h1:: client;
7
7
use async_std:: net:: TcpStream ;
@@ -31,8 +31,8 @@ type HttpsPool = DashMap<SocketAddr, Pool<TlsStream<TcpStream>, Error>>;
31
31
32
32
/// Async-h1 based HTTP Client, with connecton pooling ("Keep-Alive").
33
33
pub struct H1Client {
34
- http_pools : Arc < HttpPool > ,
35
- https_pools : Arc < HttpsPool > ,
34
+ http_pools : HttpPool ,
35
+ https_pools : HttpsPool ,
36
36
}
37
37
38
38
impl Debug for H1Client {
@@ -51,17 +51,15 @@ impl H1Client {
51
51
/// Create a new instance.
52
52
pub fn new ( ) -> Self {
53
53
Self {
54
- http_pools : Arc :: new ( DashMap :: new ( ) ) ,
55
- https_pools : Arc :: new ( DashMap :: new ( ) ) ,
54
+ http_pools : DashMap :: new ( ) ,
55
+ https_pools : DashMap :: new ( ) ,
56
56
}
57
57
}
58
58
}
59
59
60
60
#[ async_trait]
61
61
impl HttpClient for H1Client {
62
62
async fn send ( & self , mut req : Request ) -> Result < Response , Error > {
63
- let http_pools = self . http_pools . clone ( ) ;
64
- let https_pools = self . https_pools . clone ( ) ;
65
63
req. insert_header ( "Connection" , "keep-alive" ) ;
66
64
67
65
// Insert host
@@ -94,14 +92,14 @@ impl HttpClient for H1Client {
94
92
95
93
match scheme {
96
94
"http" => {
97
- let pool = if let Some ( pool) = http_pools. get ( & addr) {
95
+ let pool = if let Some ( pool) = self . http_pools . get ( & addr) {
98
96
pool
99
97
} else {
100
98
let manager = TcpConnection :: new ( addr) ;
101
99
let pool =
102
100
Pool :: < TcpStream , std:: io:: Error > :: new ( manager, MAX_CONCURRENT_CONNECTIONS ) ;
103
- http_pools. insert ( addr, pool) ;
104
- http_pools. get ( & addr) . unwrap ( )
101
+ self . http_pools . insert ( addr, pool) ;
102
+ self . http_pools . get ( & addr) . unwrap ( )
105
103
} ;
106
104
let pool = pool. clone ( ) ;
107
105
let stream = pool. get ( ) . await ?;
@@ -115,16 +113,16 @@ impl HttpClient for H1Client {
115
113
// client::connect(stream, req).await
116
114
}
117
115
"https" => {
118
- let pool = if let Some ( pool) = https_pools. get ( & addr) {
116
+ let pool = if let Some ( pool) = self . https_pools . get ( & addr) {
119
117
pool
120
118
} else {
121
119
let manager = TlsConnection :: new ( host. clone ( ) , addr) ;
122
120
let pool = Pool :: < TlsStream < TcpStream > , Error > :: new (
123
121
manager,
124
122
MAX_CONCURRENT_CONNECTIONS ,
125
123
) ;
126
- https_pools. insert ( addr, pool) ;
127
- https_pools. get ( & addr) . unwrap ( )
124
+ self . https_pools . insert ( addr, pool) ;
125
+ self . https_pools . get ( & addr) . unwrap ( )
128
126
} ;
129
127
let pool = pool. clone ( ) ;
130
128
let stream = pool. get ( ) . await . unwrap ( ) ; // TODO: remove unwrap
0 commit comments