@@ -56,7 +56,6 @@ impl Client {
56
56
ClientBuilder :: with_default ( addr)
57
57
}
58
58
59
- #[ inline]
60
59
/// Send request to the peer
61
60
pub async fn send (
62
61
& self ,
@@ -65,41 +64,46 @@ impl Client {
65
64
headers : HeaderMap ,
66
65
eof : bool ,
67
66
) -> 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 > {
68
76
loop {
69
77
let ( client, num) = self . get_client ( ) ;
70
78
71
79
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 ?;
76
83
}
84
+ }
85
+ }
77
86
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 ) ;
96
94
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 ?;
102
105
}
106
+ Ok ( ( ) )
103
107
}
104
108
105
109
fn get_client ( & self ) -> ( Option < SimpleClient > , usize ) {
@@ -113,9 +117,10 @@ impl Client {
113
117
} else if connections[ idx] . is_disconnecting ( ) {
114
118
let con = connections. remove ( idx) ;
115
119
let timeout = self . inner . disconnect_timeout ;
116
- ntex_util:: spawn ( async move {
120
+ let f = ntex_util:: spawn ( async move {
117
121
let _ = con. disconnect ( ) . disconnect_timeout ( timeout) . await ;
118
122
} ) ;
123
+ drop ( f) ;
119
124
} else {
120
125
idx += 1 ;
121
126
}
@@ -151,7 +156,7 @@ impl Client {
151
156
}
152
157
}
153
158
154
- async fn create_connection ( & self ) -> Result < SimpleClient , ClientError > {
159
+ async fn create_connection ( & self ) -> Result < ( ) , ClientError > {
155
160
let ( tx, rx) = oneshot:: channel ( ) ;
156
161
157
162
let inner = self . inner . clone ( ) ;
@@ -173,11 +178,11 @@ impl Client {
173
178
inner. authority . clone ( ) ,
174
179
storage,
175
180
) ;
176
- inner. connections . borrow_mut ( ) . push ( client. clone ( ) ) ;
181
+ inner. connections . borrow_mut ( ) . push ( client) ;
177
182
inner
178
183
. total_connections
179
184
. set ( inner. total_connections . get ( ) + 1 ) ;
180
- Ok ( client )
185
+ Ok ( ( ) )
181
186
}
182
187
Ok ( Err ( err) ) => Err ( ClientError :: from ( err) ) ,
183
188
Err ( _) => Err ( ClientError :: HandshakeTimeout ) ,
0 commit comments