@@ -176,8 +176,9 @@ async fn cached_snapshot(
176176 . discovery
177177 . collections
178178 . clone ( )
179- . try_acquire_owned ( )
180- . map_err ( |_| Status :: resource_exhausted ( "too many discovery collections" ) ) ?;
179+ . acquire_owned ( )
180+ . await
181+ . map_err ( |_| Status :: unavailable ( "discovery collections closed" ) ) ?;
181182 let result = snapshot ( state, workspace) . await ;
182183 * cached = Some ( ( tokio:: time:: Instant :: now ( ) , result. clone ( ) ) ) ;
183184 result
@@ -961,10 +962,6 @@ mod tests {
961962 ) ;
962963 assert_eq ! ( a. unwrap( ) , first) ;
963964 assert_eq ! ( b. unwrap( ) , first) ;
964- assert_eq ! (
965- cached_snapshot( & state, "other" ) . await . unwrap_err( ) . code( ) ,
966- tonic:: Code :: ResourceExhausted
967- ) ;
968965 drop ( busy) ;
969966 state
970967 . store
@@ -981,6 +978,65 @@ mod tests {
981978 ) ;
982979 }
983980
981+ #[ tokio:: test]
982+ async fn collection_waits_for_a_slot_and_resumes_when_one_is_released ( ) {
983+ let ( state, owner, report) = installed_fixture ( ) . await ;
984+ accept ( & state, & owner, & report, current_time_ms ( ) + 10_000 )
985+ . await
986+ . unwrap ( ) ;
987+ let mut busy = state
988+ . discovery
989+ . collections
990+ . clone ( )
991+ . acquire_many_owned ( u32:: try_from ( MAX_COLLECTIONS ) . unwrap ( ) )
992+ . await
993+ . unwrap ( ) ;
994+ let pending = cached_snapshot ( & state, "default" ) ;
995+ tokio:: pin!( pending) ;
996+ assert ! (
997+ tokio:: time:: timeout( Duration :: from_millis( 20 ) , & mut pending)
998+ . await
999+ . is_err( ) ,
1000+ "collection must wait rather than fail when all slots are occupied"
1001+ ) ;
1002+ drop ( busy. split ( 1 ) . unwrap ( ) ) ;
1003+ let snapshot = tokio:: time:: timeout ( Duration :: from_secs ( 3 ) , pending)
1004+ . await
1005+ . unwrap ( )
1006+ . unwrap ( ) ;
1007+ assert_eq ! ( snapshot. routes. len( ) , 1 ) ;
1008+ assert_eq ! ( state. discovery. collections. available_permits( ) , 1 ) ;
1009+ drop ( busy) ;
1010+ assert_eq ! (
1011+ state. discovery. collections. available_permits( ) ,
1012+ MAX_COLLECTIONS
1013+ ) ;
1014+ }
1015+
1016+ #[ tokio:: test]
1017+ async fn collection_slot_wait_is_bounded_by_snapshot_timeout ( ) {
1018+ let ( state, _, _) = installed_fixture ( ) . await ;
1019+ let busy = state
1020+ . discovery
1021+ . collections
1022+ . clone ( )
1023+ . acquire_many_owned ( u32:: try_from ( MAX_COLLECTIONS ) . unwrap ( ) )
1024+ . await
1025+ . unwrap ( ) ;
1026+ tokio:: time:: pause ( ) ;
1027+ let start = tokio:: time:: Instant :: now ( ) ;
1028+ let error = cached_snapshot ( & state, "default" ) . await . unwrap_err ( ) ;
1029+ assert_eq ! ( error. code( ) , tonic:: Code :: Unavailable ) ;
1030+ assert_eq ! ( error. message( ) , "service discovery collection timed out" ) ;
1031+ assert ! ( start. elapsed( ) >= SNAPSHOT_TIMEOUT ) ;
1032+ assert ! ( start. elapsed( ) < SNAPSHOT_TIMEOUT + Duration :: from_secs( 1 ) ) ;
1033+ drop ( busy) ;
1034+ assert_eq ! (
1035+ state. discovery. collections. available_permits( ) ,
1036+ MAX_COLLECTIONS
1037+ ) ;
1038+ }
1039+
9841040 #[ tokio:: test]
9851041 async fn expired_cached_routes_are_never_returned ( ) {
9861042 let ( state, _, _) = installed_fixture ( ) . await ;
0 commit comments