| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/array_list.h> |
| 7 |
#include <aws/common/clock.h> |
| 8 |
#include <aws/common/hash_table.h> |
| 9 |
#include <aws/common/logging.h> |
| 10 |
#include <aws/http/connection.h> |
| 11 |
#include <aws/http/connection_manager.h> |
| 12 |
#include <aws/http/request_response.h> |
| 13 |
#include <aws/io/channel.h> |
| 14 |
#include <aws/io/channel_bootstrap.h> |
| 15 |
#include <aws/io/event_loop.h> |
| 16 |
|
| 17 |
#include <aws/http/http2_stream_manager.h> |
| 18 |
#include <aws/http/private/http2_stream_manager_impl.h> |
| 19 |
#include <aws/http/private/request_response_impl.h> |
| 20 |
#include <aws/http/status_code.h> |
| 21 |
|
| 22 |
#include <inttypes.h> |
| 23 |
|
| 24 |
#ifdef _MSC_VER |
| 25 |
# pragma warning(disable : 4204) |
| 26 |
#endif |
| 27 |
|
| 28 |
|
| 29 |
#undef DEBUG |
| 30 |
|
| 31 |
#define STREAM_MANAGER_LOGF(level, stream_manager, text, ...) \ |
| 32 |
AWS_LOGF_##level(AWS_LS_HTTP_STREAM_MANAGER, "id=%p: " text, (void *)(stream_manager), __VA_ARGS__) |
| 33 |
#define STREAM_MANAGER_LOG(level, stream_manager, text) STREAM_MANAGER_LOGF(level, stream_manager, "%s", text) |
| 34 |
|
| 35 |
|
| 36 |
static const size_t s_default_ping_timeout_ms = 3000; |
| 37 |
|
| 38 |
static void s_aws_http2_stream_manager_build_transaction_synced(struct aws_http2_stream_management_transaction *work); |
| 39 |
static void s_aws_http2_stream_manager_execute_transaction(struct aws_http2_stream_management_transaction *work); |
| 40 |
|
| 41 |
static struct aws_h2_sm_pending_stream_acquisition *s_new_pending_stream_acquisition( |
| 42 |
struct aws_allocator *allocator, |
| 43 |
const struct aws_http_make_request_options *options, |
| 44 |
aws_http2_stream_manager_on_stream_acquired_fn *callback, |
| 45 |
void *user_data) { |
| 46 |
struct aws_h2_sm_pending_stream_acquisition *pending_stream_acquisition = |
| 47 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_h2_sm_pending_stream_acquisition)); |
| 48 |
|
| 49 |
|
| 50 |
pending_stream_acquisition->options = *options; |
| 51 |
pending_stream_acquisition->request = options->request; |
| 52 |
aws_http_message_acquire(pending_stream_acquisition->request); |
| 53 |
pending_stream_acquisition->callback = callback; |
| 54 |
pending_stream_acquisition->user_data = user_data; |
| 55 |
pending_stream_acquisition->allocator = allocator; |
| 56 |
return pending_stream_acquisition; |
| 57 |
} |
| 58 |
|
| 59 |
static void s_pending_stream_acquisition_destroy( |
| 60 |
struct aws_h2_sm_pending_stream_acquisition *pending_stream_acquisition) { |
| 61 |
if (pending_stream_acquisition == NULL) { |
| 62 |
return; |
| 63 |
} |
| 64 |
if (pending_stream_acquisition->request) { |
| 65 |
aws_http_message_release(pending_stream_acquisition->request); |
| 66 |
} |
| 67 |
aws_mem_release(pending_stream_acquisition->allocator, pending_stream_acquisition); |
| 68 |
} |
| 69 |
|
| 70 |
static void s_lock_synced_data(struct aws_http2_stream_manager *stream_manager) { |
| 71 |
int err = aws_mutex_lock(&stream_manager->synced_data.lock); |
| 72 |
AWS_ASSERT(!err && "lock failed"); |
| 73 |
(void)err; |
| 74 |
} |
| 75 |
|
| 76 |
static void s_unlock_synced_data(struct aws_http2_stream_manager *stream_manager) { |
| 77 |
int err = aws_mutex_unlock(&stream_manager->synced_data.lock); |
| 78 |
AWS_ASSERT(!err && "unlock failed"); |
| 79 |
(void)err; |
| 80 |
} |
| 81 |
|
| 82 |
static void s_sm_log_stats_synced(struct aws_http2_stream_manager *stream_manager) { |
| 83 |
STREAM_MANAGER_LOGF( |
| 84 |
TRACE, |
| 85 |
stream_manager, |
| 86 |
"Stream manager internal counts status: " |
| 87 |
"connection acquiring=%zu, streams opening=%zu, pending make request count=%zu, pending acquisition count=%zu, " |
| 88 |
"holding connections count=%zu", |
| 89 |
stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_CONNECTIONS_ACQUIRING], |
| 90 |
stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_OPEN_STREAM], |
| 91 |
stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_PENDING_MAKE_REQUESTS], |
| 92 |
stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_PENDING_ACQUISITION], |
| 93 |
stream_manager->synced_data.holding_connections_count); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 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 |
|
| 104 |
return false; |
| 105 |
} |
| 106 |
} |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
static void s_sm_count_increase_synced( |
| 112 |
struct aws_http2_stream_manager *stream_manager, |
| 113 |
enum aws_sm_count_type count_type, |
| 114 |
size_t num) { |
| 115 |
stream_manager->synced_data.internal_refcount_stats[count_type] += num; |
| 116 |
for (size_t i = 0; i < num; i++) { |
| 117 |
aws_ref_count_acquire(&stream_manager->internal_ref_count); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
static void s_sm_count_decrease_synced( |
| 122 |
struct aws_http2_stream_manager *stream_manager, |
| 123 |
enum aws_sm_count_type count_type, |
| 124 |
size_t num) { |
| 125 |
stream_manager->synced_data.internal_refcount_stats[count_type] -= num; |
| 126 |
for (size_t i = 0; i < num; i++) { |
| 127 |
aws_ref_count_release(&stream_manager->internal_ref_count); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
static void s_aws_stream_management_transaction_init( |
| 132 |
struct aws_http2_stream_management_transaction *work, |
| 133 |
struct aws_http2_stream_manager *stream_manager) { |
| 134 |
AWS_ZERO_STRUCT(*work); |
| 135 |
aws_linked_list_init(&work->pending_make_requests); |
| 136 |
work->stream_manager = stream_manager; |
| 137 |
work->allocator = stream_manager->allocator; |
| 138 |
aws_ref_count_acquire(&stream_manager->internal_ref_count); |
| 139 |
} |
| 140 |
|
| 141 |
static void s_aws_stream_management_transaction_clean_up(struct aws_http2_stream_management_transaction *work) { |
| 142 |
(void)work; |
| 143 |
AWS_ASSERT(aws_linked_list_empty(&work->pending_make_requests)); |
| 144 |
aws_ref_count_release(&work->stream_manager->internal_ref_count); |
| 145 |
} |
| 146 |
|
| 147 |
static struct aws_h2_sm_connection *s_get_best_sm_connection_from_set(struct aws_random_access_set *set) { |
| 148 |
|
| 149 |
int errored = AWS_ERROR_SUCCESS; |
| 150 |
struct aws_h2_sm_connection *sm_connection_a = NULL; |
| 151 |
errored = aws_random_access_set_random_get_ptr(set, (void **)&sm_connection_a); |
| 152 |
struct aws_h2_sm_connection *sm_connection_b = NULL; |
| 153 |
errored |= aws_random_access_set_random_get_ptr(set, (void **)&sm_connection_b); |
| 154 |
struct aws_h2_sm_connection *chosen_connection = |
| 155 |
sm_connection_a->num_streams_assigned > sm_connection_b->num_streams_assigned ? sm_connection_b |
| 156 |
: sm_connection_a; |
| 157 |
return errored == AWS_ERROR_SUCCESS ? chosen_connection : NULL; |
| 158 |
(void)errored; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
static void s_sm_try_assign_connection_to_pending_stream_acquisition_synced( |
| 164 |
struct aws_http2_stream_manager *stream_manager, |
| 165 |
struct aws_h2_sm_pending_stream_acquisition *pending_stream_acquisition) { |
| 166 |
|
| 167 |
AWS_ASSERT(pending_stream_acquisition->sm_connection == NULL); |
| 168 |
|
| 169 |
|
| 170 |
if (!s_sm_allows_more_concurrent_streams(stream_manager)) { |
| 171 |
|
| 172 |
STREAM_MANAGER_LOGF( |
| 173 |
DEBUG, |
| 174 |
stream_manager, |
| 175 |
"acquisition:%p waiting - max_concurrent_streams limit reached (%" PRIu64 ")", |
| 176 |
(void *)pending_stream_acquisition, |
| 177 |
(uint64_t)stream_manager->max_concurrent_streams); |
| 178 |
return; |
| 179 |
} |
| 180 |
int errored = 0; |
| 181 |
if (aws_random_access_set_get_size(&stream_manager->synced_data.ideal_available_set)) { |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
struct aws_h2_sm_connection *chosen_connection = |
| 186 |
s_get_best_sm_connection_from_set(&stream_manager->synced_data.ideal_available_set); |
| 187 |
AWS_ASSERT(chosen_connection); |
| 188 |
pending_stream_acquisition->sm_connection = chosen_connection; |
| 189 |
if (chosen_connection->num_streams_assigned == 0) { |
| 190 |
|
| 191 |
aws_ref_count_acquire(&chosen_connection->ref_count); |
| 192 |
} |
| 193 |
chosen_connection->num_streams_assigned++; |
| 194 |
|
| 195 |
STREAM_MANAGER_LOGF( |
| 196 |
DEBUG, |
| 197 |
stream_manager, |
| 198 |
"Picking connection:%p for acquisition:%p. Streams assigned to the connection=%" PRIu32 "", |
| 199 |
(void *)chosen_connection->connection, |
| 200 |
(void *)pending_stream_acquisition, |
| 201 |
chosen_connection->num_streams_assigned); |
| 202 |
|
| 203 |
if (chosen_connection->num_streams_assigned >= chosen_connection->max_concurrent_streams) { |
| 204 |
|
| 205 |
|
| 206 |
chosen_connection->state = AWS_H2SMCST_FULL; |
| 207 |
errored |= |
| 208 |
aws_random_access_set_remove(&stream_manager->synced_data.ideal_available_set, chosen_connection); |
| 209 |
STREAM_MANAGER_LOGF( |
| 210 |
DEBUG, |
| 211 |
stream_manager, |
| 212 |
"connection:%p reaches max concurrent streams limits. " |
| 213 |
"Connection max limits=%" PRIu32 ". Moving it out of available connections.", |
| 214 |
(void *)chosen_connection->connection, |
| 215 |
chosen_connection->max_concurrent_streams); |
| 216 |
} else if (chosen_connection->num_streams_assigned >= stream_manager->ideal_concurrent_streams_per_connection) { |
| 217 |
|
| 218 |
errored |= |
| 219 |
aws_random_access_set_remove(&stream_manager->synced_data.ideal_available_set, chosen_connection); |
| 220 |
bool added = false; |
| 221 |
errored |= aws_random_access_set_add( |
| 222 |
&stream_manager->synced_data.nonideal_available_set, chosen_connection, &added); |
| 223 |
errored |= !added; |
| 224 |
chosen_connection->state = AWS_H2SMCST_NEARLY_FULL; |
| 225 |
STREAM_MANAGER_LOGF( |
| 226 |
DEBUG, |
| 227 |
stream_manager, |
| 228 |
"connection:%p reaches ideal concurrent streams limits. Ideal limits=%zu. Moving it to nonlimited set.", |
| 229 |
(void *)chosen_connection->connection, |
| 230 |
stream_manager->ideal_concurrent_streams_per_connection); |
| 231 |
} |
| 232 |
} else if (stream_manager->synced_data.holding_connections_count == stream_manager->max_connections) { |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
if (aws_random_access_set_get_size(&stream_manager->synced_data.nonideal_available_set)) { |
| 241 |
struct aws_h2_sm_connection *chosen_connection = |
| 242 |
s_get_best_sm_connection_from_set(&stream_manager->synced_data.nonideal_available_set); |
| 243 |
AWS_ASSERT(chosen_connection); |
| 244 |
pending_stream_acquisition->sm_connection = chosen_connection; |
| 245 |
if (chosen_connection->num_streams_assigned == 0) { |
| 246 |
|
| 247 |
aws_ref_count_acquire(&chosen_connection->ref_count); |
| 248 |
} |
| 249 |
chosen_connection->num_streams_assigned++; |
| 250 |
|
| 251 |
STREAM_MANAGER_LOGF( |
| 252 |
DEBUG, |
| 253 |
stream_manager, |
| 254 |
"Picking connection:%p for acquisition:%p. Streams assigned to the connection=%" PRIu32 "", |
| 255 |
(void *)chosen_connection->connection, |
| 256 |
(void *)pending_stream_acquisition, |
| 257 |
chosen_connection->num_streams_assigned); |
| 258 |
|
| 259 |
if (chosen_connection->num_streams_assigned >= chosen_connection->max_concurrent_streams) { |
| 260 |
|
| 261 |
|
| 262 |
chosen_connection->state = AWS_H2SMCST_FULL; |
| 263 |
errored |= aws_random_access_set_remove( |
| 264 |
&stream_manager->synced_data.nonideal_available_set, chosen_connection); |
| 265 |
STREAM_MANAGER_LOGF( |
| 266 |
DEBUG, |
| 267 |
stream_manager, |
| 268 |
"connection %p reaches max concurrent streams limits. " |
| 269 |
"Connection max limits=%" PRIu32 ". Moving it out of available connections.", |
| 270 |
(void *)chosen_connection->connection, |
| 271 |
chosen_connection->max_concurrent_streams); |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
AWS_ASSERT(errored == 0 && "random access set went wrong"); |
| 276 |
(void)errored; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
static void s_finish_pending_stream_acquisitions_list_helper( |
| 281 |
struct aws_http2_stream_manager *stream_manager, |
| 282 |
struct aws_linked_list *pending_stream_acquisitions, |
| 283 |
int error_code) { |
| 284 |
while (!aws_linked_list_empty(pending_stream_acquisitions)) { |
| 285 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(pending_stream_acquisitions); |
| 286 |
struct aws_h2_sm_pending_stream_acquisition *pending_stream_acquisition = |
| 287 |
AWS_CONTAINER_OF(node, struct aws_h2_sm_pending_stream_acquisition, node); |
| 288 |
|
| 289 |
AWS_ASSERT(pending_stream_acquisition->sm_connection == NULL); |
| 290 |
if (pending_stream_acquisition->callback) { |
| 291 |
pending_stream_acquisition->callback(NULL, error_code, pending_stream_acquisition->user_data); |
| 292 |
} |
| 293 |
STREAM_MANAGER_LOGF( |
| 294 |
DEBUG, |
| 295 |
stream_manager, |
| 296 |
"acquisition:%p failed with error: %d(%s)", |
| 297 |
(void *)pending_stream_acquisition, |
| 298 |
error_code, |
| 299 |
aws_error_str(error_code)); |
| 300 |
s_pending_stream_acquisition_destroy(pending_stream_acquisition); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
static void s_finish_pending_stream_acquisitions_task(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 306 |
(void)status; |
| 307 |
struct aws_http2_stream_manager *stream_manager = arg; |
| 308 |
STREAM_MANAGER_LOG(TRACE, stream_manager, "Stream Manager final task runs"); |
| 309 |
struct aws_http2_stream_management_transaction work; |
| 310 |
struct aws_linked_list pending_stream_acquisitions; |
| 311 |
aws_linked_list_init(&pending_stream_acquisitions); |
| 312 |
s_aws_stream_management_transaction_init(&work, stream_manager); |
| 313 |
{ |
| 314 |
s_lock_synced_data(stream_manager); |
| 315 |
AWS_ASSERT(stream_manager->synced_data.state == AWS_H2SMST_DESTROYING); |
| 316 |
|
| 317 |
aws_linked_list_swap_contents( |
| 318 |
&pending_stream_acquisitions, &stream_manager->synced_data.pending_stream_acquisitions); |
| 319 |
|
| 320 |
s_sm_count_decrease_synced( |
| 321 |
stream_manager, |
| 322 |
AWS_SMCT_PENDING_ACQUISITION, |
| 323 |
stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_PENDING_ACQUISITION]); |
| 324 |
s_aws_http2_stream_manager_build_transaction_synced(&work); |
| 325 |
s_unlock_synced_data(stream_manager); |
| 326 |
} |
| 327 |
s_finish_pending_stream_acquisitions_list_helper( |
| 328 |
stream_manager, &pending_stream_acquisitions, AWS_ERROR_HTTP_STREAM_MANAGER_SHUTTING_DOWN); |
| 329 |
aws_mem_release(stream_manager->allocator, task); |
| 330 |
s_aws_http2_stream_manager_execute_transaction(&work); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
static void s_check_new_connections_needed_synced(struct aws_http2_stream_management_transaction *work) { |
| 335 |
struct aws_http2_stream_manager *stream_manager = work->stream_manager; |
| 336 |
|
| 337 |
|
| 338 |
if (!s_sm_allows_more_concurrent_streams(stream_manager)) { |
| 339 |
|
| 340 |
|
| 341 |
return; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
size_t ideal_new_connection_count = |
| 346 |
stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_PENDING_ACQUISITION] / |
| 347 |
stream_manager->ideal_concurrent_streams_per_connection; |
| 348 |
|
| 349 |
if (stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_PENDING_ACQUISITION] % |
| 350 |
stream_manager->ideal_concurrent_streams_per_connection) { |
| 351 |
++ideal_new_connection_count; |
| 352 |
} |
| 353 |
|
| 354 |
work->new_connections = aws_sub_size_saturating( |
| 355 |
ideal_new_connection_count, |
| 356 |
stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_CONNECTIONS_ACQUIRING]); |
| 357 |
|
| 358 |
size_t new_connections_available = |
| 359 |
stream_manager->max_connections - stream_manager->synced_data.holding_connections_count - |
| 360 |
stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_CONNECTIONS_ACQUIRING]; |
| 361 |
work->new_connections = aws_min_size(new_connections_available, work->new_connections); |
| 362 |
|
| 363 |
s_sm_count_increase_synced(stream_manager, AWS_SMCT_CONNECTIONS_ACQUIRING, work->new_connections); |
| 364 |
STREAM_MANAGER_LOGF( |
| 365 |
DEBUG, |
| 366 |
stream_manager, |
| 367 |
"number of acquisition that waiting for connections to use=%zu. connection acquiring=%zu, connection held=%zu, " |
| 368 |
"max connection=%zu", |
| 369 |
stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_PENDING_ACQUISITION], |
| 370 |
stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_CONNECTIONS_ACQUIRING], |
| 371 |
stream_manager->synced_data.holding_connections_count, |
| 372 |
stream_manager->max_connections); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
static void s_aws_http2_stream_manager_build_transaction_synced(struct aws_http2_stream_management_transaction *work) { |
| 384 |
struct aws_http2_stream_manager *stream_manager = work->stream_manager; |
| 385 |
if (stream_manager->synced_data.state == AWS_H2SMST_READY) { |
| 386 |
|
| 387 |
|
| 388 |
if (!s_sm_allows_more_concurrent_streams(stream_manager)) { |
| 389 |
|
| 390 |
STREAM_MANAGER_LOGF( |
| 391 |
DEBUG, |
| 392 |
stream_manager, |
| 393 |
"stream manager waiting - max_concurrent_streams limit reached (%" PRIu64 ")", |
| 394 |
(uint64_t)stream_manager->max_concurrent_streams); |
| 395 |
return; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
while (!aws_linked_list_empty(&stream_manager->synced_data.pending_stream_acquisitions)) { |
| 400 |
struct aws_linked_list_node *node = |
| 401 |
aws_linked_list_pop_front(&stream_manager->synced_data.pending_stream_acquisitions); |
| 402 |
struct aws_h2_sm_pending_stream_acquisition *pending_stream_acquisition = |
| 403 |
AWS_CONTAINER_OF(node, struct aws_h2_sm_pending_stream_acquisition, node); |
| 404 |
s_sm_try_assign_connection_to_pending_stream_acquisition_synced(stream_manager, pending_stream_acquisition); |
| 405 |
if (pending_stream_acquisition->sm_connection == NULL) { |
| 406 |
|
| 407 |
aws_linked_list_push_front(&stream_manager->synced_data.pending_stream_acquisitions, node); |
| 408 |
STREAM_MANAGER_LOGF( |
| 409 |
DEBUG, |
| 410 |
stream_manager, |
| 411 |
"acquisition:%p cannot find any connection to use.", |
| 412 |
(void *)pending_stream_acquisition); |
| 413 |
break; |
| 414 |
} else { |
| 415 |
|
| 416 |
aws_linked_list_push_back(&work->pending_make_requests, node); |
| 417 |
s_sm_count_decrease_synced(stream_manager, AWS_SMCT_PENDING_ACQUISITION, 1); |
| 418 |
s_sm_count_increase_synced(stream_manager, AWS_SMCT_PENDING_MAKE_REQUESTS, 1); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
if (stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_PENDING_ACQUISITION]) { |
| 424 |
s_check_new_connections_needed_synced(work); |
| 425 |
} |
| 426 |
|
| 427 |
} else { |
| 428 |
|
| 429 |
if (stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_PENDING_ACQUISITION] && |
| 430 |
!stream_manager->synced_data.finish_pending_stream_acquisitions_task_scheduled) { |
| 431 |
|
| 432 |
stream_manager->finish_pending_stream_acquisitions_task_event_loop = |
| 433 |
aws_event_loop_group_get_next_loop(stream_manager->bootstrap->event_loop_group); |
| 434 |
struct aws_task *finish_pending_stream_acquisitions_task = |
| 435 |
aws_mem_calloc(stream_manager->allocator, 1, sizeof(struct aws_task)); |
| 436 |
aws_task_init( |
| 437 |
finish_pending_stream_acquisitions_task, |
| 438 |
s_finish_pending_stream_acquisitions_task, |
| 439 |
stream_manager, |
| 440 |
"sm_finish_pending_stream_acquisitions"); |
| 441 |
aws_event_loop_schedule_task_now( |
| 442 |
stream_manager->finish_pending_stream_acquisitions_task_event_loop, |
| 443 |
finish_pending_stream_acquisitions_task); |
| 444 |
stream_manager->synced_data.finish_pending_stream_acquisitions_task_scheduled = true; |
| 445 |
} |
| 446 |
} |
| 447 |
s_sm_log_stats_synced(stream_manager); |
| 448 |
} |
| 449 |
|
| 450 |
static void s_on_ping_complete( |
| 451 |
struct aws_http_connection *http2_connection, |
| 452 |
uint64_t round_trip_time_ns, |
| 453 |
int error_code, |
| 454 |
void *user_data) { |
| 455 |
|
| 456 |
(void)http2_connection; |
| 457 |
struct aws_h2_sm_connection *sm_connection = user_data; |
| 458 |
if (error_code) { |
| 459 |
goto done; |
| 460 |
} |
| 461 |
if (!sm_connection->connection) { |
| 462 |
goto done; |
| 463 |
} |
| 464 |
AWS_ASSERT(aws_channel_thread_is_callers_thread(aws_http_connection_get_channel(sm_connection->connection))); |
| 465 |
STREAM_MANAGER_LOGF( |
| 466 |
TRACE, |
| 467 |
sm_connection->stream_manager, |
| 468 |
"PING ACK received for connection: %p. Round trip time in ns is: %" PRIu64 ".", |
| 469 |
(void *)sm_connection->connection, |
| 470 |
round_trip_time_ns); |
| 471 |
sm_connection->thread_data.ping_received = true; |
| 472 |
|
| 473 |
done: |
| 474 |
|
| 475 |
aws_ref_count_release(&sm_connection->ref_count); |
| 476 |
} |
| 477 |
|
| 478 |
static void s_connection_ping_timeout_task(struct aws_channel_task *task, void *arg, enum aws_task_status status) { |
| 479 |
(void)task; |
| 480 |
(void)status; |
| 481 |
struct aws_h2_sm_connection *sm_connection = arg; |
| 482 |
if (status != AWS_TASK_STATUS_RUN_READY) { |
| 483 |
goto done; |
| 484 |
} |
| 485 |
if (!sm_connection->connection) { |
| 486 |
|
| 487 |
goto done; |
| 488 |
} |
| 489 |
AWS_ASSERT(aws_channel_thread_is_callers_thread(aws_http_connection_get_channel(sm_connection->connection))); |
| 490 |
if (!sm_connection->thread_data.ping_received) { |
| 491 |
|
| 492 |
STREAM_MANAGER_LOGF( |
| 493 |
ERROR, |
| 494 |
sm_connection->stream_manager, |
| 495 |
"ping timeout detected for connection: %p, closing connection.", |
| 496 |
(void *)sm_connection->connection); |
| 497 |
|
| 498 |
aws_http_connection_close(sm_connection->connection); |
| 499 |
} else { |
| 500 |
struct aws_channel *channel = aws_http_connection_get_channel(sm_connection->connection); |
| 501 |
|
| 502 |
aws_ref_count_acquire(&sm_connection->ref_count); |
| 503 |
aws_channel_schedule_task_future( |
| 504 |
channel, &sm_connection->ping_task, sm_connection->thread_data.next_ping_task_time); |
| 505 |
} |
| 506 |
done: |
| 507 |
|
| 508 |
aws_ref_count_release(&sm_connection->ref_count); |
| 509 |
} |
| 510 |
|
| 511 |
static void s_connection_ping_task(struct aws_channel_task *task, void *arg, enum aws_task_status status) { |
| 512 |
(void)task; |
| 513 |
(void)status; |
| 514 |
struct aws_h2_sm_connection *sm_connection = arg; |
| 515 |
if (status != AWS_TASK_STATUS_RUN_READY) { |
| 516 |
aws_ref_count_release(&sm_connection->ref_count); |
| 517 |
return; |
| 518 |
} |
| 519 |
if (!sm_connection->connection) { |
| 520 |
|
| 521 |
aws_ref_count_release(&sm_connection->ref_count); |
| 522 |
return; |
| 523 |
} |
| 524 |
AWS_ASSERT(aws_channel_thread_is_callers_thread(aws_http_connection_get_channel(sm_connection->connection))); |
| 525 |
|
| 526 |
STREAM_MANAGER_LOGF( |
| 527 |
TRACE, sm_connection->stream_manager, "Sending PING for connection: %p.", (void *)sm_connection->connection); |
| 528 |
aws_http2_connection_ping(sm_connection->connection, NULL, s_on_ping_complete, sm_connection); |
| 529 |
|
| 530 |
aws_ref_count_acquire(&sm_connection->ref_count); |
| 531 |
sm_connection->thread_data.ping_received = false; |
| 532 |
|
| 533 |
|
| 534 |
struct aws_channel *channel = aws_http_connection_get_channel(sm_connection->connection); |
| 535 |
uint64_t current_time = 0; |
| 536 |
aws_channel_current_clock_time(channel, ¤t_time); |
| 537 |
sm_connection->thread_data.next_ping_task_time = |
| 538 |
current_time + sm_connection->stream_manager->connection_ping_period_ns; |
| 539 |
uint64_t timeout_time = current_time + sm_connection->stream_manager->connection_ping_timeout_ns; |
| 540 |
aws_channel_task_init( |
| 541 |
&sm_connection->ping_timeout_task, |
| 542 |
s_connection_ping_timeout_task, |
| 543 |
sm_connection, |
| 544 |
"Stream manager connection ping timeout task"); |
| 545 |
|
| 546 |
aws_channel_schedule_task_future(channel, &sm_connection->ping_timeout_task, timeout_time); |
| 547 |
} |
| 548 |
|
| 549 |
static void s_sm_connection_destroy(void *user_data) { |
| 550 |
struct aws_h2_sm_connection *sm_connection = user_data; |
| 551 |
|
| 552 |
aws_mem_release(sm_connection->allocator, sm_connection); |
| 553 |
} |
| 554 |
|
| 555 |
static struct aws_h2_sm_connection *s_sm_connection_new( |
| 556 |
struct aws_http2_stream_manager *stream_manager, |
| 557 |
struct aws_http_connection *connection) { |
| 558 |
struct aws_h2_sm_connection *sm_connection = |
| 559 |
aws_mem_calloc(stream_manager->allocator, 1, sizeof(struct aws_h2_sm_connection)); |
| 560 |
sm_connection->allocator = stream_manager->allocator; |
| 561 |
|
| 562 |
struct aws_http2_setting out_settings[AWS_HTTP2_SETTINGS_COUNT]; |
| 563 |
|
| 564 |
aws_http2_connection_get_remote_settings(connection, out_settings); |
| 565 |
uint32_t remote_max_con_streams = out_settings[AWS_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS - 1].value; |
| 566 |
sm_connection->max_concurrent_streams = |
| 567 |
aws_min_u32((uint32_t)stream_manager->max_concurrent_streams_per_connection, remote_max_con_streams); |
| 568 |
sm_connection->connection = connection; |
| 569 |
sm_connection->stream_manager = stream_manager; |
| 570 |
sm_connection->state = AWS_H2SMCST_IDEAL; |
| 571 |
aws_ref_count_init(&sm_connection->ref_count, sm_connection, s_sm_connection_destroy); |
| 572 |
|
| 573 |
if (stream_manager->connection_ping_period_ns) { |
| 574 |
struct aws_channel *channel = aws_http_connection_get_channel(connection); |
| 575 |
uint64_t schedule_time = 0; |
| 576 |
aws_channel_current_clock_time(channel, &schedule_time); |
| 577 |
schedule_time += stream_manager->connection_ping_period_ns; |
| 578 |
aws_channel_task_init( |
| 579 |
&sm_connection->ping_task, s_connection_ping_task, sm_connection, "Stream manager connection ping task"); |
| 580 |
|
| 581 |
aws_ref_count_acquire(&sm_connection->ref_count); |
| 582 |
aws_channel_schedule_task_future(channel, &sm_connection->ping_task, schedule_time); |
| 583 |
} |
| 584 |
return sm_connection; |
| 585 |
} |
| 586 |
|
| 587 |
static void s_sm_connection_release_connection(struct aws_h2_sm_connection *sm_connection) { |
| 588 |
AWS_ASSERT(sm_connection->num_streams_assigned == 0); |
| 589 |
AWS_ASSERT(sm_connection->connection != NULL); |
| 590 |
|
| 591 |
AWS_ASSERT(aws_channel_thread_is_callers_thread(aws_http_connection_get_channel(sm_connection->connection))); |
| 592 |
|
| 593 |
s_lock_synced_data(sm_connection->stream_manager); |
| 594 |
|
| 595 |
aws_linked_list_remove(&sm_connection->node); |
| 596 |
s_unlock_synced_data(sm_connection->stream_manager); |
| 597 |
|
| 598 |
int error = aws_http_connection_manager_release_connection( |
| 599 |
sm_connection->stream_manager->connection_manager, sm_connection->connection); |
| 600 |
AWS_ASSERT(!error); |
| 601 |
(void)error; |
| 602 |
sm_connection->connection = NULL; |
| 603 |
aws_ref_count_release(&sm_connection->ref_count); |
| 604 |
} |
| 605 |
|
| 606 |
static void s_sm_on_connection_acquired_failed_synced( |
| 607 |
struct aws_http2_stream_manager *stream_manager, |
| 608 |
struct aws_linked_list *stream_acquisitions_to_fail) { |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
size_t num_can_fit = aws_mul_size_saturating( |
| 613 |
stream_manager->ideal_concurrent_streams_per_connection, |
| 614 |
stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_CONNECTIONS_ACQUIRING]); |
| 615 |
size_t num_to_fail = aws_sub_size_saturating( |
| 616 |
stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_PENDING_ACQUISITION], num_can_fit); |
| 617 |
|
| 618 |
for (size_t i = 0; i < num_to_fail; i++) { |
| 619 |
struct aws_linked_list_node *node = |
| 620 |
aws_linked_list_pop_front(&stream_manager->synced_data.pending_stream_acquisitions); |
| 621 |
aws_linked_list_push_back(stream_acquisitions_to_fail, node); |
| 622 |
} |
| 623 |
s_sm_count_decrease_synced(stream_manager, AWS_SMCT_PENDING_ACQUISITION, num_to_fail); |
| 624 |
} |
| 625 |
|
| 626 |
static void s_sm_on_connection_acquired(struct aws_http_connection *connection, int error_code, void *user_data) { |
| 627 |
struct aws_http2_stream_manager *stream_manager = user_data; |
| 628 |
struct aws_http2_stream_management_transaction work; |
| 629 |
STREAM_MANAGER_LOGF(TRACE, stream_manager, "connection=%p acquired from connection manager", (void *)connection); |
| 630 |
int re_error = 0; |
| 631 |
int stream_fail_error_code = AWS_ERROR_SUCCESS; |
| 632 |
bool should_release_connection = false; |
| 633 |
struct aws_linked_list stream_acquisitions_to_fail; |
| 634 |
aws_linked_list_init(&stream_acquisitions_to_fail); |
| 635 |
s_aws_stream_management_transaction_init(&work, stream_manager); |
| 636 |
{ |
| 637 |
s_lock_synced_data(stream_manager); |
| 638 |
s_sm_count_decrease_synced(stream_manager, AWS_SMCT_CONNECTIONS_ACQUIRING, 1); |
| 639 |
if (error_code || !connection) { |
| 640 |
STREAM_MANAGER_LOGF( |
| 641 |
ERROR, |
| 642 |
stream_manager, |
| 643 |
"connection acquired from connection manager failed, with error: %d(%s)", |
| 644 |
error_code, |
| 645 |
aws_error_str(error_code)); |
| 646 |
s_sm_on_connection_acquired_failed_synced(stream_manager, &stream_acquisitions_to_fail); |
| 647 |
stream_fail_error_code = AWS_ERROR_HTTP_STREAM_MANAGER_CONNECTION_ACQUIRE_FAILURE; |
| 648 |
} else if (aws_http_connection_get_version(connection) != AWS_HTTP_VERSION_2) { |
| 649 |
STREAM_MANAGER_LOGF( |
| 650 |
ERROR, |
| 651 |
stream_manager, |
| 652 |
"Unexpected HTTP version acquired, release the connection=%p acquired immediately", |
| 653 |
(void *)connection); |
| 654 |
should_release_connection = true; |
| 655 |
s_sm_on_connection_acquired_failed_synced(stream_manager, &stream_acquisitions_to_fail); |
| 656 |
stream_fail_error_code = AWS_ERROR_HTTP_STREAM_MANAGER_UNEXPECTED_HTTP_VERSION; |
| 657 |
} else if (stream_manager->synced_data.state != AWS_H2SMST_READY) { |
| 658 |
STREAM_MANAGER_LOGF( |
| 659 |
DEBUG, |
| 660 |
stream_manager, |
| 661 |
"shutting down, release the connection=%p acquired immediately", |
| 662 |
(void *)connection); |
| 663 |
|
| 664 |
should_release_connection = true; |
| 665 |
} else if (stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_PENDING_ACQUISITION] == 0) { |
| 666 |
STREAM_MANAGER_LOGF( |
| 667 |
DEBUG, |
| 668 |
stream_manager, |
| 669 |
"No pending acquisition, release the connection=%p acquired immediately", |
| 670 |
(void *)connection); |
| 671 |
|
| 672 |
should_release_connection = true; |
| 673 |
} else { |
| 674 |
struct aws_h2_sm_connection *sm_connection = s_sm_connection_new(stream_manager, connection); |
| 675 |
|
| 676 |
aws_linked_list_push_back(&stream_manager->synced_data.all_held_connections, &sm_connection->node); |
| 677 |
bool added = false; |
| 678 |
re_error |= |
| 679 |
aws_random_access_set_add(&stream_manager->synced_data.ideal_available_set, sm_connection, &added); |
| 680 |
re_error |= !added; |
| 681 |
++stream_manager->synced_data.holding_connections_count; |
| 682 |
} |
| 683 |
s_aws_http2_stream_manager_build_transaction_synced(&work); |
| 684 |
s_unlock_synced_data(stream_manager); |
| 685 |
} |
| 686 |
|
| 687 |
if (should_release_connection) { |
| 688 |
STREAM_MANAGER_LOGF(DEBUG, stream_manager, "Releasing connection: %p", (void *)connection); |
| 689 |
re_error |= aws_http_connection_manager_release_connection(stream_manager->connection_manager, connection); |
| 690 |
} |
| 691 |
|
| 692 |
AWS_ASSERT(!re_error && "connection acquired callback fails with programming errors"); |
| 693 |
(void)re_error; |
| 694 |
|
| 695 |
|
| 696 |
s_finish_pending_stream_acquisitions_list_helper( |
| 697 |
stream_manager, &stream_acquisitions_to_fail, stream_fail_error_code); |
| 698 |
s_aws_http2_stream_manager_execute_transaction(&work); |
| 699 |
} |
| 700 |
|
| 701 |
static int s_on_incoming_headers( |
| 702 |
struct aws_http_stream *stream, |
| 703 |
enum aws_http_header_block header_block, |
| 704 |
const struct aws_http_header *header_array, |
| 705 |
size_t num_headers, |
| 706 |
void *user_data) { |
| 707 |
struct aws_h2_sm_pending_stream_acquisition *pending_stream_acquisition = user_data; |
| 708 |
struct aws_h2_sm_connection *sm_connection = pending_stream_acquisition->sm_connection; |
| 709 |
struct aws_http2_stream_manager *stream_manager = sm_connection->stream_manager; |
| 710 |
|
| 711 |
if (pending_stream_acquisition->options.on_response_headers) { |
| 712 |
return pending_stream_acquisition->options.on_response_headers( |
| 713 |
stream, header_block, header_array, num_headers, pending_stream_acquisition->options.user_data); |
| 714 |
} |
| 715 |
if (stream_manager->close_connection_on_server_error) { |
| 716 |
|
| 717 |
int status_code = 0; |
| 718 |
aws_http_stream_get_incoming_response_status(stream, &status_code); |
| 719 |
AWS_ASSERT(status_code != 0); |
| 720 |
switch (status_code) { |
| 721 |
case AWS_HTTP_STATUS_CODE_500_INTERNAL_SERVER_ERROR: |
| 722 |
case AWS_HTTP_STATUS_CODE_502_BAD_GATEWAY: |
| 723 |
case AWS_HTTP_STATUS_CODE_503_SERVICE_UNAVAILABLE: |
| 724 |
case AWS_HTTP_STATUS_CODE_504_GATEWAY_TIMEOUT: |
| 725 |
|
| 726 |
if (!sm_connection->thread_data.stopped_new_requests) { |
| 727 |
STREAM_MANAGER_LOGF( |
| 728 |
DEBUG, |
| 729 |
stream_manager, |
| 730 |
"no longer using connection: %p due to receiving %d server error status code for stream: %p", |
| 731 |
(void *)sm_connection->connection, |
| 732 |
status_code, |
| 733 |
(void *)stream); |
| 734 |
aws_http_connection_stop_new_requests(sm_connection->connection); |
| 735 |
sm_connection->thread_data.stopped_new_requests = true; |
| 736 |
} |
| 737 |
break; |
| 738 |
default: |
| 739 |
break; |
| 740 |
} |
| 741 |
} |
| 742 |
return AWS_OP_SUCCESS; |
| 743 |
} |
| 744 |
|
| 745 |
static int s_on_incoming_header_block_done( |
| 746 |
struct aws_http_stream *stream, |
| 747 |
enum aws_http_header_block header_block, |
| 748 |
void *user_data) { |
| 749 |
struct aws_h2_sm_pending_stream_acquisition *pending_stream_acquisition = user_data; |
| 750 |
if (pending_stream_acquisition->options.on_response_header_block_done) { |
| 751 |
return pending_stream_acquisition->options.on_response_header_block_done( |
| 752 |
stream, header_block, pending_stream_acquisition->options.user_data); |
| 753 |
} |
| 754 |
return AWS_OP_SUCCESS; |
| 755 |
} |
| 756 |
|
| 757 |
static int s_on_incoming_body(struct aws_http_stream *stream, const struct aws_byte_cursor *data, void *user_data) { |
| 758 |
struct aws_h2_sm_pending_stream_acquisition *pending_stream_acquisition = user_data; |
| 759 |
if (pending_stream_acquisition->options.on_response_body) { |
| 760 |
return pending_stream_acquisition->options.on_response_body( |
| 761 |
stream, data, pending_stream_acquisition->options.user_data); |
| 762 |
} |
| 763 |
return AWS_OP_SUCCESS; |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
static void s_update_sm_connection_set_on_stream_finishes_synced( |
| 768 |
struct aws_h2_sm_connection *sm_connection, |
| 769 |
struct aws_http2_stream_manager *stream_manager) { |
| 770 |
|
| 771 |
int re_error = 0; |
| 772 |
size_t cur_num = sm_connection->num_streams_assigned; |
| 773 |
size_t ideal_num = stream_manager->ideal_concurrent_streams_per_connection; |
| 774 |
size_t max_num = sm_connection->max_concurrent_streams; |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
if (sm_connection->state == AWS_H2SMCST_NEARLY_FULL && cur_num < ideal_num) { |
| 782 |
|
| 783 |
bool exist = false; |
| 784 |
(void)exist; |
| 785 |
AWS_ASSERT( |
| 786 |
aws_random_access_set_exist(&stream_manager->synced_data.nonideal_available_set, sm_connection, &exist) == |
| 787 |
AWS_OP_SUCCESS && |
| 788 |
exist); |
| 789 |
re_error |= aws_random_access_set_remove(&stream_manager->synced_data.nonideal_available_set, sm_connection); |
| 790 |
bool added = false; |
| 791 |
re_error |= aws_random_access_set_add(&stream_manager->synced_data.ideal_available_set, sm_connection, &added); |
| 792 |
re_error |= !added; |
| 793 |
sm_connection->state = AWS_H2SMCST_IDEAL; |
| 794 |
} else if (sm_connection->state == AWS_H2SMCST_FULL && cur_num < max_num) { |
| 795 |
|
| 796 |
STREAM_MANAGER_LOGF( |
| 797 |
DEBUG, |
| 798 |
stream_manager, |
| 799 |
"connection:%p back to available, assigned stream=%zu, max concurrent streams=%" PRIu32 "", |
| 800 |
(void *)sm_connection->connection, |
| 801 |
cur_num, |
| 802 |
sm_connection->max_concurrent_streams); |
| 803 |
bool added = false; |
| 804 |
if (cur_num >= ideal_num) { |
| 805 |
sm_connection->state = AWS_H2SMCST_NEARLY_FULL; |
| 806 |
STREAM_MANAGER_LOGF( |
| 807 |
TRACE, stream_manager, "connection:%p added to soft limited set", (void *)sm_connection->connection); |
| 808 |
re_error |= |
| 809 |
aws_random_access_set_add(&stream_manager->synced_data.nonideal_available_set, sm_connection, &added); |
| 810 |
} else { |
| 811 |
sm_connection->state = AWS_H2SMCST_IDEAL; |
| 812 |
STREAM_MANAGER_LOGF( |
| 813 |
TRACE, stream_manager, "connection:%p added to ideal set", (void *)sm_connection->connection); |
| 814 |
re_error |= |
| 815 |
aws_random_access_set_add(&stream_manager->synced_data.ideal_available_set, sm_connection, &added); |
| 816 |
} |
| 817 |
re_error |= !added; |
| 818 |
} |
| 819 |
AWS_ASSERT(re_error == AWS_OP_SUCCESS); |
| 820 |
(void)re_error; |
| 821 |
} |
| 822 |
|
| 823 |
static void s_sm_connection_on_scheduled_stream_finishes( |
| 824 |
struct aws_h2_sm_connection *sm_connection, |
| 825 |
struct aws_http2_stream_manager *stream_manager) { |
| 826 |
|
| 827 |
bool connection_available = aws_http_connection_new_requests_allowed(sm_connection->connection); |
| 828 |
struct aws_http2_stream_management_transaction work; |
| 829 |
s_aws_stream_management_transaction_init(&work, stream_manager); |
| 830 |
{ |
| 831 |
s_lock_synced_data(stream_manager); |
| 832 |
s_sm_count_decrease_synced(stream_manager, AWS_SMCT_OPEN_STREAM, 1); |
| 833 |
--sm_connection->num_streams_assigned; |
| 834 |
if (sm_connection->num_streams_assigned == 0) { |
| 835 |
|
| 836 |
aws_ref_count_release(&sm_connection->ref_count); |
| 837 |
} |
| 838 |
if (!connection_available) { |
| 839 |
|
| 840 |
aws_random_access_set_remove(&stream_manager->synced_data.ideal_available_set, sm_connection); |
| 841 |
aws_random_access_set_remove(&stream_manager->synced_data.nonideal_available_set, sm_connection); |
| 842 |
} else { |
| 843 |
s_update_sm_connection_set_on_stream_finishes_synced(sm_connection, stream_manager); |
| 844 |
} |
| 845 |
s_aws_http2_stream_manager_build_transaction_synced(&work); |
| 846 |
|
| 847 |
|
| 848 |
if (sm_connection->num_streams_assigned == 0) { |
| 849 |
|
| 850 |
aws_random_access_set_remove(&stream_manager->synced_data.ideal_available_set, sm_connection); |
| 851 |
work.sm_connection_to_release = sm_connection; |
| 852 |
--stream_manager->synced_data.holding_connections_count; |
| 853 |
|
| 854 |
if (stream_manager->synced_data.state == AWS_H2SMST_READY && |
| 855 |
stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_PENDING_ACQUISITION]) { |
| 856 |
s_check_new_connections_needed_synced(&work); |
| 857 |
} |
| 858 |
} |
| 859 |
s_unlock_synced_data(stream_manager); |
| 860 |
} |
| 861 |
s_aws_http2_stream_manager_execute_transaction(&work); |
| 862 |
} |
| 863 |
|
| 864 |
static void s_on_stream_complete(struct aws_http_stream *stream, int error_code, void *user_data) { |
| 865 |
struct aws_h2_sm_pending_stream_acquisition *pending_stream_acquisition = user_data; |
| 866 |
struct aws_h2_sm_connection *sm_connection = pending_stream_acquisition->sm_connection; |
| 867 |
struct aws_http2_stream_manager *stream_manager = sm_connection->stream_manager; |
| 868 |
if (pending_stream_acquisition->options.on_complete) { |
| 869 |
pending_stream_acquisition->options.on_complete( |
| 870 |
stream, error_code, pending_stream_acquisition->options.user_data); |
| 871 |
} |
| 872 |
s_sm_connection_on_scheduled_stream_finishes(sm_connection, stream_manager); |
| 873 |
} |
| 874 |
|
| 875 |
static void s_on_stream_destroy(void *user_data) { |
| 876 |
struct aws_h2_sm_pending_stream_acquisition *pending_stream_acquisition = user_data; |
| 877 |
if (pending_stream_acquisition->options.on_destroy) { |
| 878 |
pending_stream_acquisition->options.on_destroy(pending_stream_acquisition->options.user_data); |
| 879 |
} |
| 880 |
s_pending_stream_acquisition_destroy(pending_stream_acquisition); |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
static void s_make_request_task(struct aws_channel_task *task, void *arg, enum aws_task_status status) { |
| 885 |
(void)task; |
| 886 |
struct aws_h2_sm_pending_stream_acquisition *pending_stream_acquisition = arg; |
| 887 |
struct aws_h2_sm_connection *sm_connection = pending_stream_acquisition->sm_connection; |
| 888 |
struct aws_http2_stream_manager *stream_manager = sm_connection->stream_manager; |
| 889 |
int error_code = AWS_ERROR_SUCCESS; |
| 890 |
|
| 891 |
STREAM_MANAGER_LOGF( |
| 892 |
TRACE, |
| 893 |
stream_manager, |
| 894 |
"Make request task running for acquisition:%p from connection:%p thread", |
| 895 |
(void *)pending_stream_acquisition, |
| 896 |
(void *)sm_connection->connection); |
| 897 |
bool is_shutting_down = false; |
| 898 |
{ |
| 899 |
s_lock_synced_data(stream_manager); |
| 900 |
is_shutting_down = stream_manager->synced_data.state != AWS_H2SMST_READY; |
| 901 |
s_sm_count_decrease_synced(stream_manager, AWS_SMCT_PENDING_MAKE_REQUESTS, 1); |
| 902 |
|
| 903 |
|
| 904 |
s_sm_count_increase_synced(stream_manager, AWS_SMCT_OPEN_STREAM, 1); |
| 905 |
AWS_ASSERT( |
| 906 |
sm_connection->max_concurrent_streams >= sm_connection->num_streams_assigned && |
| 907 |
"The max concurrent streams exceed"); |
| 908 |
s_unlock_synced_data(stream_manager); |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
if (status != AWS_TASK_STATUS_RUN_READY) { |
| 913 |
STREAM_MANAGER_LOGF( |
| 914 |
ERROR, |
| 915 |
stream_manager, |
| 916 |
"acquisition:%p failed as the task is cancelled.", |
| 917 |
(void *)pending_stream_acquisition); |
| 918 |
error_code = AWS_ERROR_HTTP_CONNECTION_CLOSED; |
| 919 |
goto error; |
| 920 |
} |
| 921 |
if (is_shutting_down) { |
| 922 |
STREAM_MANAGER_LOGF( |
| 923 |
ERROR, |
| 924 |
stream_manager, |
| 925 |
"acquisition:%p failed as stream manager is shutting down before task runs.", |
| 926 |
(void *)pending_stream_acquisition); |
| 927 |
error_code = AWS_ERROR_HTTP_STREAM_MANAGER_SHUTTING_DOWN; |
| 928 |
goto error; |
| 929 |
} |
| 930 |
struct aws_http_make_request_options request_options = { |
| 931 |
.self_size = sizeof(request_options), |
| 932 |
.request = pending_stream_acquisition->request, |
| 933 |
.on_response_headers = s_on_incoming_headers, |
| 934 |
.on_response_header_block_done = s_on_incoming_header_block_done, |
| 935 |
.on_response_body = s_on_incoming_body, |
| 936 |
.on_complete = s_on_stream_complete, |
| 937 |
.on_destroy = s_on_stream_destroy, |
| 938 |
.user_data = pending_stream_acquisition, |
| 939 |
.http2_use_manual_data_writes = pending_stream_acquisition->options.http2_use_manual_data_writes, |
| 940 |
}; |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
struct aws_http_stream *stream = aws_http_connection_make_request(sm_connection->connection, &request_options); |
| 945 |
if (!stream) { |
| 946 |
error_code = aws_last_error(); |
| 947 |
STREAM_MANAGER_LOGF( |
| 948 |
ERROR, |
| 949 |
stream_manager, |
| 950 |
"acquisition:%p failed as HTTP level make request failed with error: %d(%s).", |
| 951 |
(void *)pending_stream_acquisition, |
| 952 |
error_code, |
| 953 |
aws_error_str(error_code)); |
| 954 |
goto error; |
| 955 |
} |
| 956 |
|
| 957 |
if (aws_http_stream_activate(stream)) { |
| 958 |
|
| 959 |
|
| 960 |
error_code = aws_last_error(); |
| 961 |
STREAM_MANAGER_LOGF( |
| 962 |
ERROR, |
| 963 |
stream_manager, |
| 964 |
"acquisition:%p failed as stream activate failed with error: %d(%s).", |
| 965 |
(void *)pending_stream_acquisition, |
| 966 |
error_code, |
| 967 |
aws_error_str(error_code)); |
| 968 |
goto error; |
| 969 |
} |
| 970 |
if (pending_stream_acquisition->callback) { |
| 971 |
pending_stream_acquisition->callback(stream, 0, pending_stream_acquisition->user_data); |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
aws_http_message_release(pending_stream_acquisition->request); |
| 977 |
pending_stream_acquisition->request = NULL; |
| 978 |
return; |
| 979 |
error: |
| 980 |
if (pending_stream_acquisition->callback) { |
| 981 |
pending_stream_acquisition->callback(NULL, error_code, pending_stream_acquisition->user_data); |
| 982 |
} |
| 983 |
s_pending_stream_acquisition_destroy(pending_stream_acquisition); |
| 984 |
|
| 985 |
s_sm_connection_on_scheduled_stream_finishes(sm_connection, stream_manager); |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
static void s_aws_http2_stream_manager_execute_transaction(struct aws_http2_stream_management_transaction *work) { |
| 990 |
|
| 991 |
struct aws_http2_stream_manager *stream_manager = work->stream_manager; |
| 992 |
|
| 993 |
|
| 994 |
if (work->sm_connection_to_release) { |
| 995 |
AWS_ASSERT(work->sm_connection_to_release->num_streams_assigned == 0); |
| 996 |
STREAM_MANAGER_LOGF( |
| 997 |
DEBUG, |
| 998 |
stream_manager, |
| 999 |
"Release connection:%p back to connection manager as no outstanding streams", |
| 1000 |
(void *)work->sm_connection_to_release->connection); |
| 1001 |
s_sm_connection_release_connection(work->sm_connection_to_release); |
| 1002 |
} |
| 1003 |
|
| 1004 |
|
| 1005 |
while (!aws_linked_list_empty(&work->pending_make_requests)) { |
| 1006 |
|
| 1007 |
|
| 1008 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(&work->pending_make_requests); |
| 1009 |
struct aws_h2_sm_pending_stream_acquisition *pending_stream_acquisition = |
| 1010 |
AWS_CONTAINER_OF(node, struct aws_h2_sm_pending_stream_acquisition, node); |
| 1011 |
|
| 1012 |
AWS_ASSERT( |
| 1013 |
pending_stream_acquisition->sm_connection && |
| 1014 |
"Stream manager internal bug: connection is not decided before execute transaction"); |
| 1015 |
|
| 1016 |
STREAM_MANAGER_LOGF( |
| 1017 |
TRACE, |
| 1018 |
stream_manager, |
| 1019 |
"acquisition:%p is scheduled to be made request from connection:%p thread", |
| 1020 |
(void *)pending_stream_acquisition, |
| 1021 |
(void *)pending_stream_acquisition->sm_connection->connection); |
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
struct aws_channel *channel = |
| 1029 |
aws_http_connection_get_channel(pending_stream_acquisition->sm_connection->connection); |
| 1030 |
aws_channel_task_init( |
| 1031 |
&pending_stream_acquisition->make_request_task, |
| 1032 |
s_make_request_task, |
| 1033 |
pending_stream_acquisition, |
| 1034 |
"Stream manager make request task"); |
| 1035 |
aws_channel_schedule_task_now(channel, &pending_stream_acquisition->make_request_task); |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
if (work->new_connections) { |
| 1040 |
STREAM_MANAGER_LOGF(DEBUG, stream_manager, "acquiring %zu new connections", work->new_connections); |
| 1041 |
} |
| 1042 |
for (size_t i = 0; i < work->new_connections; ++i) { |
| 1043 |
aws_http_connection_manager_acquire_connection( |
| 1044 |
stream_manager->connection_manager, s_sm_on_connection_acquired, stream_manager); |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
s_aws_stream_management_transaction_clean_up(work); |
| 1051 |
} |
| 1052 |
|
| 1053 |
void s_stream_manager_destroy_final(struct aws_http2_stream_manager *stream_manager) { |
| 1054 |
if (!stream_manager) { |
| 1055 |
return; |
| 1056 |
} |
| 1057 |
|
| 1058 |
STREAM_MANAGER_LOG(TRACE, stream_manager, "Stream Manager finishes destroying self"); |
| 1059 |
|
| 1060 |
AWS_FATAL_ASSERT(stream_manager->connection_manager == NULL); |
| 1061 |
AWS_FATAL_ASSERT(aws_linked_list_empty(&stream_manager->synced_data.pending_stream_acquisitions)); |
| 1062 |
aws_mutex_clean_up(&stream_manager->synced_data.lock); |
| 1063 |
aws_random_access_set_clean_up(&stream_manager->synced_data.ideal_available_set); |
| 1064 |
aws_random_access_set_clean_up(&stream_manager->synced_data.nonideal_available_set); |
| 1065 |
aws_client_bootstrap_release(stream_manager->bootstrap); |
| 1066 |
|
| 1067 |
if (stream_manager->shutdown_complete_callback) { |
| 1068 |
stream_manager->shutdown_complete_callback(stream_manager->shutdown_complete_user_data); |
| 1069 |
} |
| 1070 |
aws_mem_release(stream_manager->allocator, stream_manager); |
| 1071 |
} |
| 1072 |
|
| 1073 |
void s_stream_manager_on_cm_shutdown_complete(void *user_data) { |
| 1074 |
struct aws_http2_stream_manager *stream_manager = (struct aws_http2_stream_manager *)user_data; |
| 1075 |
STREAM_MANAGER_LOGF( |
| 1076 |
TRACE, |
| 1077 |
stream_manager, |
| 1078 |
"Underlying connection manager (ip=%p) finished shutdown, stream manager can finish destroying now", |
| 1079 |
(void *)stream_manager->connection_manager); |
| 1080 |
stream_manager->connection_manager = NULL; |
| 1081 |
s_stream_manager_destroy_final(stream_manager); |
| 1082 |
} |
| 1083 |
|
| 1084 |
static void s_stream_manager_start_destroy(void *user_data) { |
| 1085 |
struct aws_http2_stream_manager *stream_manager = user_data; |
| 1086 |
STREAM_MANAGER_LOG(TRACE, stream_manager, "Stream Manager reaches the condition to destroy, start to destroy"); |
| 1087 |
|
| 1088 |
AWS_ASSERT(stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_CONNECTIONS_ACQUIRING] == 0); |
| 1089 |
AWS_ASSERT(stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_OPEN_STREAM] == 0); |
| 1090 |
AWS_ASSERT(stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_PENDING_MAKE_REQUESTS] == 0); |
| 1091 |
AWS_ASSERT(stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_PENDING_ACQUISITION] == 0); |
| 1092 |
|
| 1093 |
|
| 1094 |
struct aws_linked_list_node *node = aws_linked_list_begin(&stream_manager->synced_data.all_held_connections); |
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
const struct aws_linked_list_node *end_node = |
| 1099 |
aws_linked_list_end(&stream_manager->synced_data.all_held_connections); |
| 1100 |
while (node != end_node) { |
| 1101 |
struct aws_h2_sm_connection *sm_connection = AWS_CONTAINER_OF(node, struct aws_h2_sm_connection, node); |
| 1102 |
|
| 1103 |
node = aws_linked_list_next(node); |
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
AWS_ASSERT(sm_connection->num_streams_assigned == 0); |
| 1108 |
|
| 1109 |
if (sm_connection->connection) { |
| 1110 |
aws_http_connection_manager_release_connection( |
| 1111 |
stream_manager->connection_manager, sm_connection->connection); |
| 1112 |
sm_connection->connection = NULL; |
| 1113 |
--stream_manager->synced_data.holding_connections_count; |
| 1114 |
} |
| 1115 |
|
| 1116 |
|
| 1117 |
aws_linked_list_remove(&sm_connection->node); |
| 1118 |
aws_ref_count_release(&sm_connection->ref_count); |
| 1119 |
} |
| 1120 |
|
| 1121 |
|
| 1122 |
AWS_ASSERT(aws_linked_list_empty(&stream_manager->synced_data.all_held_connections)); |
| 1123 |
AWS_ASSERT(stream_manager->synced_data.holding_connections_count == 0); |
| 1124 |
|
| 1125 |
AWS_ASSERT(stream_manager->connection_manager); |
| 1126 |
struct aws_http_connection_manager *cm = stream_manager->connection_manager; |
| 1127 |
stream_manager->connection_manager = NULL; |
| 1128 |
aws_http_connection_manager_release(cm); |
| 1129 |
} |
| 1130 |
|
| 1131 |
void s_stream_manager_on_zero_external_ref(void *user_data) { |
| 1132 |
struct aws_http2_stream_manager *stream_manager = user_data; |
| 1133 |
STREAM_MANAGER_LOG( |
| 1134 |
TRACE, |
| 1135 |
stream_manager, |
| 1136 |
"Last refcount released, manager stop accepting new stream request and will start to clean up when not " |
| 1137 |
"outstanding tasks remaining."); |
| 1138 |
struct aws_http2_stream_management_transaction work; |
| 1139 |
s_aws_stream_management_transaction_init(&work, stream_manager); |
| 1140 |
{ |
| 1141 |
s_lock_synced_data(stream_manager); |
| 1142 |
stream_manager->synced_data.state = AWS_H2SMST_DESTROYING; |
| 1143 |
s_aws_http2_stream_manager_build_transaction_synced(&work); |
| 1144 |
|
| 1145 |
aws_ref_count_release(&stream_manager->internal_ref_count); |
| 1146 |
s_unlock_synced_data(stream_manager); |
| 1147 |
} |
| 1148 |
s_aws_http2_stream_manager_execute_transaction(&work); |
| 1149 |
} |
| 1150 |
|
| 1151 |
struct aws_http2_stream_manager *aws_http2_stream_manager_new( |
| 1152 |
struct aws_allocator *allocator, |
| 1153 |
const struct aws_http2_stream_manager_options *options) { |
| 1154 |
|
| 1155 |
AWS_PRECONDITION(allocator); |
| 1156 |
|
| 1157 |
if (!options->http2_prior_knowledge && !options->tls_connection_options) { |
| 1158 |
AWS_LOGF_ERROR( |
| 1159 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1160 |
"Invalid options - Prior knowledge must be used for cleartext HTTP/2 connections." |
| 1161 |
" Upgrade from HTTP/1.1 is not supported."); |
| 1162 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 1163 |
return NULL; |
| 1164 |
} |
| 1165 |
struct aws_http2_stream_manager *stream_manager = |
| 1166 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http2_stream_manager)); |
| 1167 |
stream_manager->allocator = allocator; |
| 1168 |
aws_linked_list_init(&stream_manager->synced_data.pending_stream_acquisitions); |
| 1169 |
aws_linked_list_init(&stream_manager->synced_data.all_held_connections); |
| 1170 |
|
| 1171 |
if (aws_mutex_init(&stream_manager->synced_data.lock)) { |
| 1172 |
goto on_error; |
| 1173 |
} |
| 1174 |
if (aws_random_access_set_init( |
| 1175 |
&stream_manager->synced_data.ideal_available_set, |
| 1176 |
allocator, |
| 1177 |
aws_hash_ptr, |
| 1178 |
aws_ptr_eq, |
| 1179 |
NULL , |
| 1180 |
2)) { |
| 1181 |
goto on_error; |
| 1182 |
} |
| 1183 |
if (aws_random_access_set_init( |
| 1184 |
&stream_manager->synced_data.nonideal_available_set, |
| 1185 |
allocator, |
| 1186 |
aws_hash_ptr, |
| 1187 |
aws_ptr_eq, |
| 1188 |
NULL , |
| 1189 |
2)) { |
| 1190 |
goto on_error; |
| 1191 |
} |
| 1192 |
aws_ref_count_init( |
| 1193 |
&stream_manager->external_ref_count, |
| 1194 |
stream_manager, |
| 1195 |
(aws_simple_completion_callback *)s_stream_manager_on_zero_external_ref); |
| 1196 |
aws_ref_count_init( |
| 1197 |
&stream_manager->internal_ref_count, |
| 1198 |
stream_manager, |
| 1199 |
(aws_simple_completion_callback *)s_stream_manager_start_destroy); |
| 1200 |
|
| 1201 |
if (options->connection_ping_period_ms) { |
| 1202 |
stream_manager->connection_ping_period_ns = |
| 1203 |
aws_timestamp_convert(options->connection_ping_period_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); |
| 1204 |
size_t connection_ping_timeout_ms = |
| 1205 |
options->connection_ping_timeout_ms ? options->connection_ping_timeout_ms : s_default_ping_timeout_ms; |
| 1206 |
stream_manager->connection_ping_timeout_ns = |
| 1207 |
aws_timestamp_convert(connection_ping_timeout_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); |
| 1208 |
if (stream_manager->connection_ping_period_ns < stream_manager->connection_ping_timeout_ns) { |
| 1209 |
STREAM_MANAGER_LOGF( |
| 1210 |
WARN, |
| 1211 |
stream_manager, |
| 1212 |
"connection_ping_period_ms: %zu is shorter than connection_ping_timeout_ms: %zu. Clapping " |
| 1213 |
"connection_ping_timeout_ms to %zu", |
| 1214 |
options->connection_ping_period_ms, |
| 1215 |
connection_ping_timeout_ms, |
| 1216 |
options->connection_ping_period_ms); |
| 1217 |
stream_manager->connection_ping_timeout_ns = stream_manager->connection_ping_period_ns; |
| 1218 |
} |
| 1219 |
} |
| 1220 |
|
| 1221 |
stream_manager->bootstrap = aws_client_bootstrap_acquire(options->bootstrap); |
| 1222 |
struct aws_http_connection_manager_options cm_options = { |
| 1223 |
.bootstrap = options->bootstrap, |
| 1224 |
.socket_options = options->socket_options, |
| 1225 |
.tls_connection_options = options->tls_connection_options, |
| 1226 |
.http2_prior_knowledge = options->http2_prior_knowledge, |
| 1227 |
.host = options->host, |
| 1228 |
.port = options->port, |
| 1229 |
.enable_read_back_pressure = options->enable_read_back_pressure, |
| 1230 |
.initial_window_size = options->initial_window_size, |
| 1231 |
.monitoring_options = options->monitoring_options, |
| 1232 |
.proxy_options = options->proxy_options, |
| 1233 |
.proxy_ev_settings = options->proxy_ev_settings, |
| 1234 |
.max_connections = options->max_connections, |
| 1235 |
.shutdown_complete_user_data = stream_manager, |
| 1236 |
.shutdown_complete_callback = s_stream_manager_on_cm_shutdown_complete, |
| 1237 |
.initial_settings_array = options->initial_settings_array, |
| 1238 |
.num_initial_settings = options->num_initial_settings, |
| 1239 |
.max_closed_streams = options->max_closed_streams, |
| 1240 |
.http2_conn_manual_window_management = options->conn_manual_window_management, |
| 1241 |
}; |
| 1242 |
|
| 1243 |
stream_manager->connection_manager = aws_http_connection_manager_new(allocator, &cm_options); |
| 1244 |
if (!stream_manager->connection_manager) { |
| 1245 |
goto on_error; |
| 1246 |
} |
| 1247 |
|
| 1248 |
stream_manager->synced_data.state = AWS_H2SMST_READY; |
| 1249 |
stream_manager->shutdown_complete_callback = options->shutdown_complete_callback; |
| 1250 |
stream_manager->shutdown_complete_user_data = options->shutdown_complete_user_data; |
| 1251 |
stream_manager->ideal_concurrent_streams_per_connection = options->ideal_concurrent_streams_per_connection |
| 1252 |
? options->ideal_concurrent_streams_per_connection |
| 1253 |
: UINT32_MAX; |
| 1254 |
stream_manager->max_concurrent_streams_per_connection = |
| 1255 |
options->max_concurrent_streams_per_connection ? options->max_concurrent_streams_per_connection : UINT32_MAX; |
| 1256 |
stream_manager->max_connections = options->max_connections; |
| 1257 |
stream_manager->max_concurrent_streams = options->max_concurrent_streams; |
| 1258 |
stream_manager->close_connection_on_server_error = options->close_connection_on_server_error; |
| 1259 |
|
| 1260 |
return stream_manager; |
| 1261 |
on_error: |
| 1262 |
s_stream_manager_destroy_final(stream_manager); |
| 1263 |
return NULL; |
| 1264 |
} |
| 1265 |
|
| 1266 |
struct aws_http2_stream_manager *aws_http2_stream_manager_acquire(struct aws_http2_stream_manager *stream_manager) { |
| 1267 |
if (stream_manager) { |
| 1268 |
aws_ref_count_acquire(&stream_manager->external_ref_count); |
| 1269 |
} |
| 1270 |
return stream_manager; |
| 1271 |
} |
| 1272 |
|
| 1273 |
struct aws_http2_stream_manager *aws_http2_stream_manager_release(struct aws_http2_stream_manager *stream_manager) { |
| 1274 |
if (stream_manager) { |
| 1275 |
aws_ref_count_release(&stream_manager->external_ref_count); |
| 1276 |
} |
| 1277 |
return NULL; |
| 1278 |
} |
| 1279 |
|
| 1280 |
void aws_http2_stream_manager_acquire_stream( |
| 1281 |
struct aws_http2_stream_manager *stream_manager, |
| 1282 |
const struct aws_http2_stream_manager_acquire_stream_options *acquire_stream_option) { |
| 1283 |
AWS_PRECONDITION(stream_manager); |
| 1284 |
AWS_PRECONDITION(acquire_stream_option); |
| 1285 |
AWS_PRECONDITION(acquire_stream_option->callback); |
| 1286 |
AWS_PRECONDITION(acquire_stream_option->options); |
| 1287 |
struct aws_http2_stream_management_transaction work; |
| 1288 |
struct aws_h2_sm_pending_stream_acquisition *pending_stream_acquisition = s_new_pending_stream_acquisition( |
| 1289 |
stream_manager->allocator, |
| 1290 |
acquire_stream_option->options, |
| 1291 |
acquire_stream_option->callback, |
| 1292 |
acquire_stream_option->user_data); |
| 1293 |
STREAM_MANAGER_LOGF( |
| 1294 |
TRACE, stream_manager, "Stream Manager creates acquisition:%p for user", (void *)pending_stream_acquisition); |
| 1295 |
s_aws_stream_management_transaction_init(&work, stream_manager); |
| 1296 |
{ |
| 1297 |
s_lock_synced_data(stream_manager); |
| 1298 |
|
| 1299 |
AWS_FATAL_ASSERT(stream_manager->synced_data.state != AWS_H2SMST_DESTROYING); |
| 1300 |
aws_linked_list_push_back( |
| 1301 |
&stream_manager->synced_data.pending_stream_acquisitions, &pending_stream_acquisition->node); |
| 1302 |
s_sm_count_increase_synced(stream_manager, AWS_SMCT_PENDING_ACQUISITION, 1); |
| 1303 |
s_aws_http2_stream_manager_build_transaction_synced(&work); |
| 1304 |
s_unlock_synced_data(stream_manager); |
| 1305 |
} |
| 1306 |
s_aws_http2_stream_manager_execute_transaction(&work); |
| 1307 |
} |
| 1308 |
|
| 1309 |
static size_t s_get_available_streams_num_from_connection_set(const struct aws_random_access_set *set) { |
| 1310 |
size_t all_available_streams_num = 0; |
| 1311 |
size_t ideal_connection_num = aws_random_access_set_get_size(set); |
| 1312 |
for (size_t i = 0; i < ideal_connection_num; i++) { |
| 1313 |
struct aws_h2_sm_connection *sm_connection = NULL; |
| 1314 |
AWS_FATAL_ASSERT(aws_random_access_set_random_get_ptr_index(set, (void **)&sm_connection, i) == AWS_OP_SUCCESS); |
| 1315 |
uint32_t available_streams = sm_connection->max_concurrent_streams - sm_connection->num_streams_assigned; |
| 1316 |
all_available_streams_num += (size_t)available_streams; |
| 1317 |
} |
| 1318 |
return all_available_streams_num; |
| 1319 |
} |
| 1320 |
|
| 1321 |
void aws_http2_stream_manager_fetch_metrics( |
| 1322 |
const struct aws_http2_stream_manager *stream_manager, |
| 1323 |
struct aws_http_manager_metrics *out_metrics) { |
| 1324 |
AWS_PRECONDITION(stream_manager); |
| 1325 |
AWS_PRECONDITION(out_metrics); |
| 1326 |
{ |
| 1327 |
s_lock_synced_data((struct aws_http2_stream_manager *)(void *)stream_manager); |
| 1328 |
size_t all_available_streams_num = 0; |
| 1329 |
all_available_streams_num += |
| 1330 |
s_get_available_streams_num_from_connection_set(&stream_manager->synced_data.ideal_available_set); |
| 1331 |
all_available_streams_num += |
| 1332 |
s_get_available_streams_num_from_connection_set(&stream_manager->synced_data.nonideal_available_set); |
| 1333 |
out_metrics->pending_concurrency_acquires = |
| 1334 |
stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_PENDING_ACQUISITION]; |
| 1335 |
out_metrics->available_concurrency = all_available_streams_num; |
| 1336 |
out_metrics->leased_concurrency = stream_manager->synced_data.internal_refcount_stats[AWS_SMCT_OPEN_STREAM]; |
| 1337 |
s_unlock_synced_data((struct aws_http2_stream_manager *)(void *)stream_manager); |
| 1338 |
} |
| 1339 |
} |