@@ -159,11 +159,18 @@ pub trait MakeConnection: Send + Sync + 'static {
159159 semaphore : Arc < Semaphore > ,
160160 timeout : Option < Duration > ,
161161 max_total_response_size : u64 ,
162+ max_concurrent_requests : u64 ,
162163 ) -> MakeThrottledConnection < Self >
163164 where
164165 Self : Sized ,
165166 {
166- MakeThrottledConnection :: new ( semaphore, self , timeout, max_total_response_size)
167+ MakeThrottledConnection :: new (
168+ semaphore,
169+ self ,
170+ timeout,
171+ max_total_response_size,
172+ max_concurrent_requests,
173+ )
167174 }
168175}
169176
@@ -190,6 +197,7 @@ pub struct MakeThrottledConnection<F> {
190197 // will result in reducing concurrency to prevent out-of-memory errors.
191198 max_total_response_size : u64 ,
192199 waiters : AtomicUsize ,
200+ max_concurrent_requests : u64 ,
193201}
194202
195203impl < F > MakeThrottledConnection < F > {
@@ -198,13 +206,15 @@ impl<F> MakeThrottledConnection<F> {
198206 connection_maker : F ,
199207 timeout : Option < Duration > ,
200208 max_total_response_size : u64 ,
209+ max_concurrent_requests : u64 ,
201210 ) -> Self {
202211 Self {
203212 semaphore,
204213 connection_maker,
205214 timeout,
206215 max_total_response_size,
207216 waiters : AtomicUsize :: new ( 0 ) ,
217+ max_concurrent_requests,
208218 }
209219 }
210220
@@ -263,7 +273,7 @@ impl<F: MakeConnection> MakeConnection for MakeThrottledConnection<F> {
263273 ) ;
264274 let units = self . units_to_take ( ) ;
265275 let waiters_guard = WaitersGuard :: new ( & self . waiters ) ;
266- if waiters_guard. waiters . load ( Ordering :: Relaxed ) >= 128 {
276+ if ( waiters_guard. waiters . load ( Ordering :: Relaxed ) as u64 ) >= self . max_concurrent_requests {
267277 return Err ( Error :: TooManyRequests ) ;
268278 }
269279 let fut = self . semaphore . clone ( ) . acquire_many_owned ( units) ;
@@ -423,6 +433,7 @@ pub mod test {
423433 Arc :: new ( Semaphore :: new ( 10 ) ) ,
424434 Some ( Duration :: from_millis ( 100 ) ) ,
425435 u64:: MAX ,
436+ u64:: MAX ,
426437 ) ;
427438
428439 let mut conns = Vec :: with_capacity ( 10 ) ;
0 commit comments