@@ -209,15 +209,15 @@ impl IterableEndpoints {
209
209
self . start = self . next ;
210
210
}
211
211
212
- pub async fn next ( & mut self ) -> Option < EndpointRef < ' _ > > {
212
+ pub async fn next ( & mut self , max_delay : Duration ) -> Option < EndpointRef < ' _ > > {
213
213
let index = self . index ( ) ?;
214
- self . delay ( index) . await ;
214
+ self . delay ( index, max_delay ) . await ;
215
215
self . step ( ) ;
216
216
Some ( self . endpoints [ index. offset ] . to_ref ( ) )
217
217
}
218
218
219
- async fn delay ( & self , index : Index ) {
220
- let timeout = Self :: timeout ( index, self . endpoints . len ( ) ) ;
219
+ async fn delay ( & self , index : Index , max_delay : Duration ) {
220
+ let timeout = max_delay . min ( Self :: timeout ( index, self . endpoints . len ( ) ) ) ;
221
221
if timeout != Duration :: ZERO {
222
222
tokio:: time:: sleep ( timeout) . await ;
223
223
}
@@ -338,22 +338,24 @@ mod tests {
338
338
339
339
#[ tokio:: test]
340
340
async fn test_iterable_endpoints_next ( ) {
341
+ use std:: time:: Duration ;
342
+
341
343
use assertor:: * ;
342
344
343
345
use super :: { parse_connect_string, EndpointRef , Index , IterableEndpoints } ;
344
346
let ( endpoints, _) = parse_connect_string ( "host1:2181,tcp://host2,tcp+tls://host3:2182" , true ) . unwrap ( ) ;
345
347
let mut endpoints = IterableEndpoints :: from ( endpoints. as_slice ( ) ) ;
346
- assert_eq ! ( endpoints. next( ) . await , Some ( EndpointRef :: new( "host1" , 2181 , true ) ) ) ;
347
- assert_eq ! ( endpoints. next( ) . await , Some ( EndpointRef :: new( "host2" , 2181 , false ) ) ) ;
348
- assert_eq ! ( endpoints. next( ) . await , Some ( EndpointRef :: new( "host3" , 2182 , true ) ) ) ;
349
- assert_eq ! ( endpoints. next( ) . await , None ) ;
348
+ assert_eq ! ( endpoints. next( Duration :: MAX ) . await , Some ( EndpointRef :: new( "host1" , 2181 , true ) ) ) ;
349
+ assert_eq ! ( endpoints. next( Duration :: MAX ) . await , Some ( EndpointRef :: new( "host2" , 2181 , false ) ) ) ;
350
+ assert_eq ! ( endpoints. next( Duration :: MAX ) . await , Some ( EndpointRef :: new( "host3" , 2182 , true ) ) ) ;
351
+ assert_eq ! ( endpoints. next( Duration :: MAX ) . await , None ) ;
350
352
351
353
endpoints. cycle ( ) ;
352
354
let start = std:: time:: Instant :: now ( ) ;
353
- assert_eq ! ( endpoints. next( ) . await , Some ( EndpointRef :: new( "host1" , 2181 , true ) ) ) ;
354
- assert_eq ! ( endpoints. next( ) . await , Some ( EndpointRef :: new( "host2" , 2181 , false ) ) ) ;
355
- assert_eq ! ( endpoints. next( ) . await , Some ( EndpointRef :: new( "host3" , 2182 , true ) ) ) ;
356
- assert_eq ! ( endpoints. next( ) . await , Some ( EndpointRef :: new( "host1" , 2181 , true ) ) ) ;
355
+ assert_eq ! ( endpoints. next( Duration :: MAX ) . await , Some ( EndpointRef :: new( "host1" , 2181 , true ) ) ) ;
356
+ assert_eq ! ( endpoints. next( Duration :: MAX ) . await , Some ( EndpointRef :: new( "host2" , 2181 , false ) ) ) ;
357
+ assert_eq ! ( endpoints. next( Duration :: MAX ) . await , Some ( EndpointRef :: new( "host3" , 2182 , true ) ) ) ;
358
+ assert_eq ! ( endpoints. next( Duration :: MAX ) . await , Some ( EndpointRef :: new( "host1" , 2181 , true ) ) ) ;
357
359
let delay = IterableEndpoints :: timeout ( Index { offset : 0 , cycles : 1 } , 3 )
358
360
+ IterableEndpoints :: timeout ( Index { offset : 1 , cycles : 1 } , 3 ) ;
359
361
let now = std:: time:: Instant :: now ( ) ;
0 commit comments