@@ -93,6 +93,20 @@ static void s_sm_log_stats_synced(struct aws_http2_stream_manager *stream_manage
9393 stream_manager -> synced_data .holding_connections_count );
9494}
9595
96+ /* Check against the max concurrent streams limit, return true if more streams are allowed. */
97+ static bool s_sm_allows_more_concurrent_streams (struct aws_http2_stream_manager * stream_manager ) {
98+ if (stream_manager -> max_concurrent_streams > 0 ) {
99+ size_t total_active_streams =
100+ stream_manager -> synced_data .internal_refcount_stats [AWS_SMCT_OPEN_STREAM ] +
101+ stream_manager -> synced_data .internal_refcount_stats [AWS_SMCT_PENDING_MAKE_REQUESTS ];
102+ if (total_active_streams >= stream_manager -> max_concurrent_streams ) {
103+ /* total active streams are equal or more than the setting */
104+ return false;
105+ }
106+ }
107+ return true;
108+ }
109+
96110/* The count acquire and release all needs to be invoked helding the lock */
97111static void s_sm_count_increase_synced (
98112 struct aws_http2_stream_manager * stream_manager ,
@@ -152,6 +166,18 @@ static void s_sm_try_assign_connection_to_pending_stream_acquisition_synced(
152166
153167 AWS_ASSERT (pending_stream_acquisition -> sm_connection == NULL );
154168
169+ /* Check if we've reached the max_concurrent_streams limit */
170+
171+ if (!s_sm_allows_more_concurrent_streams (stream_manager )) {
172+ /* We've reached the limit, cannot assign a connection yet */
173+ STREAM_MANAGER_LOGF (
174+ DEBUG ,
175+ stream_manager ,
176+ "acquisition:%p waiting - max_concurrent_streams limit reached (%" PRIu64 ")" ,
177+ (void * )pending_stream_acquisition ,
178+ (uint64_t )stream_manager -> max_concurrent_streams );
179+ return ;
180+ }
155181 int errored = 0 ;
156182 if (aws_random_access_set_get_size (& stream_manager -> synced_data .ideal_available_set )) {
157183 /**
@@ -303,10 +329,7 @@ static void s_check_new_connections_needed_synced(struct aws_http2_stream_manage
303329
304330 if (stream_manager -> max_concurrent_streams > 0 ) {
305331 /* avoid acquiring for connection when no more streams are allowed */
306- size_t total_active_streams =
307- stream_manager -> synced_data .internal_refcount_stats [AWS_SMCT_OPEN_STREAM ] +
308- stream_manager -> synced_data .internal_refcount_stats [AWS_SMCT_PENDING_MAKE_REQUESTS ];
309- if (total_active_streams >= stream_manager -> max_concurrent_streams ) {
332+ if (!s_sm_allows_more_concurrent_streams (stream_manager )) {
310333 /* We've reached the limit, avoid creating new connection in case of having connection not being tracked by
311334 * streams. */
312335 return ;
@@ -357,16 +380,12 @@ static void s_aws_http2_stream_manager_build_transaction_synced(struct aws_http2
357380
358381 /* Steps 0: Check if we've reached the max_concurrent_streams limit */
359382 if (stream_manager -> max_concurrent_streams > 0 ) {
360- size_t total_active_streams =
361- stream_manager -> synced_data .internal_refcount_stats [AWS_SMCT_OPEN_STREAM ] +
362- stream_manager -> synced_data .internal_refcount_stats [AWS_SMCT_PENDING_MAKE_REQUESTS ];
363- if (total_active_streams >= stream_manager -> max_concurrent_streams ) {
383+ if (!s_sm_allows_more_concurrent_streams (stream_manager )) {
364384 /* We've reached the limit, skip building the transactions */
365385 STREAM_MANAGER_LOGF (
366386 DEBUG ,
367387 stream_manager ,
368- "stream manager waiting - max_concurrent_streams limit reached (%" PRIu64 "/%" PRIu64 ")" ,
369- (uint64_t )total_active_streams ,
388+ "stream manager waiting - max_concurrent_streams limit reached (%" PRIu64 ")" ,
370389 (uint64_t )stream_manager -> max_concurrent_streams );
371390 return ;
372391 }
0 commit comments