| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/http/connection_manager.h> |
| 7 |
|
| 8 |
#include <aws/http/connection.h> |
| 9 |
#include <aws/http/private/connection_manager_system_vtable.h> |
| 10 |
#include <aws/http/private/connection_monitor.h> |
| 11 |
#include <aws/http/private/http_impl.h> |
| 12 |
#include <aws/http/private/proxy_impl.h> |
| 13 |
|
| 14 |
#include <aws/io/channel_bootstrap.h> |
| 15 |
#include <aws/io/event_loop.h> |
| 16 |
#include <aws/io/logging.h> |
| 17 |
#include <aws/io/socket.h> |
| 18 |
#include <aws/io/tls_channel_handler.h> |
| 19 |
#include <aws/io/uri.h> |
| 20 |
|
| 21 |
#include <aws/common/clock.h> |
| 22 |
#include <aws/common/hash_table.h> |
| 23 |
#include <aws/common/linked_list.h> |
| 24 |
#include <aws/common/mutex.h> |
| 25 |
#include <aws/common/ref_count.h> |
| 26 |
#include <aws/common/string.h> |
| 27 |
|
| 28 |
#ifdef _MSC_VER |
| 29 |
# pragma warning(disable : 4232) |
| 30 |
#endif |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
struct aws_idle_connection { |
| 36 |
struct aws_allocator *allocator; |
| 37 |
struct aws_linked_list_node node; |
| 38 |
uint64_t cull_timestamp; |
| 39 |
struct aws_http_connection *connection; |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
static struct aws_http_connection_manager_system_vtable s_default_system_vtable = { |
| 46 |
.aws_http_client_connect = aws_http_client_connect, |
| 47 |
.aws_http_connection_release = aws_http_connection_release, |
| 48 |
.aws_http_connection_close = aws_http_connection_close, |
| 49 |
.aws_http_connection_new_requests_allowed = aws_http_connection_new_requests_allowed, |
| 50 |
.aws_high_res_clock_get_ticks = aws_high_res_clock_get_ticks, |
| 51 |
.aws_channel_thread_is_callers_thread = aws_channel_thread_is_callers_thread, |
| 52 |
.aws_http_connection_get_channel = aws_http_connection_get_channel, |
| 53 |
.aws_http_connection_get_version = aws_http_connection_get_version, |
| 54 |
}; |
| 55 |
|
| 56 |
const struct aws_http_connection_manager_system_vtable *g_aws_http_connection_manager_default_system_vtable_ptr = |
| 57 |
&s_default_system_vtable; |
| 58 |
|
| 59 |
bool aws_http_connection_manager_system_vtable_is_valid(const struct aws_http_connection_manager_system_vtable *table) { |
| 60 |
return table->aws_http_client_connect && table->aws_http_connection_close && table->aws_http_connection_release && |
| 61 |
table->aws_http_connection_new_requests_allowed; |
| 62 |
} |
| 63 |
|
| 64 |
enum aws_http_connection_manager_state_type { AWS_HCMST_UNINITIALIZED, AWS_HCMST_READY, AWS_HCMST_SHUTTING_DOWN }; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
enum aws_http_connection_manager_count_type { |
| 76 |
AWS_HCMCT_VENDED_CONNECTION, |
| 77 |
AWS_HCMCT_PENDING_CONNECTIONS, |
| 78 |
AWS_HCMCT_OPEN_CONNECTION, |
| 79 |
AWS_HCMCT_COUNT, |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
struct aws_http_connection_manager { |
| 146 |
struct aws_allocator *allocator; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
const struct aws_http_connection_manager_system_vtable *system_vtable; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
aws_http_connection_manager_shutdown_complete_fn *shutdown_complete_callback; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
void *shutdown_complete_user_data; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
struct aws_mutex lock; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
enum aws_http_connection_manager_state_type state; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
size_t idle_connection_count; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
struct aws_linked_list idle_connections; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
struct aws_linked_list pending_acquisitions; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
size_t pending_acquisition_count; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
size_t internal_ref[AWS_HCMCT_COUNT]; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
size_t pending_settings_count; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
struct aws_client_bootstrap *bootstrap; |
| 234 |
size_t initial_window_size; |
| 235 |
struct aws_socket_options socket_options; |
| 236 |
struct aws_tls_connection_options *tls_connection_options; |
| 237 |
struct aws_http_proxy_config *proxy_config; |
| 238 |
struct aws_http_connection_monitoring_options monitoring_options; |
| 239 |
struct aws_string *host; |
| 240 |
struct proxy_env_var_settings proxy_ev_settings; |
| 241 |
struct aws_tls_connection_options *proxy_ev_tls_options; |
| 242 |
uint32_t port; |
| 243 |
uint64_t response_first_byte_timeout_ms; |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
bool http2_prior_knowledge; |
| 249 |
struct aws_array_list *initial_settings; |
| 250 |
size_t max_closed_streams; |
| 251 |
bool http2_conn_manual_window_management; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
size_t max_connections; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
size_t external_ref_count; |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
struct aws_ref_count internal_ref_count; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
bool enable_read_back_pressure; |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
uint64_t max_connection_idle_in_milliseconds; |
| 293 |
|
| 294 |
uint64_t connection_acquisition_timeout_ms; |
| 295 |
|
| 296 |
uint64_t max_pending_connection_acquisitions; |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
struct aws_task *cull_task; |
| 303 |
struct aws_event_loop *cull_event_loop; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
struct aws_array_list network_interface_names; |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
size_t network_interface_names_index; |
| 315 |
}; |
| 316 |
|
| 317 |
struct aws_http_connection_manager_snapshot { |
| 318 |
enum aws_http_connection_manager_state_type state; |
| 319 |
|
| 320 |
size_t idle_connection_count; |
| 321 |
size_t pending_acquisition_count; |
| 322 |
size_t pending_settings_count; |
| 323 |
|
| 324 |
|
| 325 |
size_t pending_connects_count; |
| 326 |
size_t vended_connection_count; |
| 327 |
size_t open_connection_count; |
| 328 |
|
| 329 |
size_t external_ref_count; |
| 330 |
}; |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
static void s_aws_http_connection_manager_get_snapshot( |
| 336 |
struct aws_http_connection_manager *manager, |
| 337 |
struct aws_http_connection_manager_snapshot *snapshot) { |
| 338 |
|
| 339 |
snapshot->state = manager->state; |
| 340 |
snapshot->idle_connection_count = manager->idle_connection_count; |
| 341 |
snapshot->pending_acquisition_count = manager->pending_acquisition_count; |
| 342 |
snapshot->pending_settings_count = manager->pending_settings_count; |
| 343 |
|
| 344 |
snapshot->pending_connects_count = manager->internal_ref[AWS_HCMCT_PENDING_CONNECTIONS]; |
| 345 |
snapshot->vended_connection_count = manager->internal_ref[AWS_HCMCT_VENDED_CONNECTION]; |
| 346 |
snapshot->open_connection_count = manager->internal_ref[AWS_HCMCT_OPEN_CONNECTION]; |
| 347 |
|
| 348 |
snapshot->external_ref_count = manager->external_ref_count; |
| 349 |
} |
| 350 |
|
| 351 |
static void s_aws_http_connection_manager_log_snapshot( |
| 352 |
struct aws_http_connection_manager *manager, |
| 353 |
struct aws_http_connection_manager_snapshot *snapshot) { |
| 354 |
if (snapshot->state != AWS_HCMST_UNINITIALIZED) { |
| 355 |
AWS_LOGF_DEBUG( |
| 356 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 357 |
"id=%p: snapshot - state=%d, idle_connection_count=%zu, pending_acquire_count=%zu, " |
| 358 |
"pending_settings_count=%zu, pending_connect_count=%zu, vended_connection_count=%zu, " |
| 359 |
"open_connection_count=%zu, ref_count=%zu", |
| 360 |
(void *)manager, |
| 361 |
(int)snapshot->state, |
| 362 |
snapshot->idle_connection_count, |
| 363 |
snapshot->pending_acquisition_count, |
| 364 |
snapshot->pending_settings_count, |
| 365 |
snapshot->pending_connects_count, |
| 366 |
snapshot->vended_connection_count, |
| 367 |
snapshot->open_connection_count, |
| 368 |
snapshot->external_ref_count); |
| 369 |
} else { |
| 370 |
AWS_LOGF_DEBUG( |
| 371 |
AWS_LS_HTTP_CONNECTION_MANAGER, "id=%p: snapshot not initialized by control flow", (void *)manager); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
void aws_http_connection_manager_set_system_vtable( |
| 376 |
struct aws_http_connection_manager *manager, |
| 377 |
const struct aws_http_connection_manager_system_vtable *system_vtable) { |
| 378 |
AWS_FATAL_ASSERT(aws_http_connection_manager_system_vtable_is_valid(system_vtable)); |
| 379 |
|
| 380 |
manager->system_vtable = system_vtable; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
struct aws_http_connection_acquisition { |
| 396 |
struct aws_allocator *allocator; |
| 397 |
struct aws_linked_list_node node; |
| 398 |
struct aws_http_connection_manager *manager; |
| 399 |
aws_http_connection_manager_on_connection_setup_fn *callback; |
| 400 |
void *user_data; |
| 401 |
struct aws_http_connection *connection; |
| 402 |
int error_code; |
| 403 |
struct aws_channel_task acquisition_task; |
| 404 |
uint64_t timeout_timestamp; |
| 405 |
}; |
| 406 |
|
| 407 |
static void s_connection_acquisition_task( |
| 408 |
struct aws_channel_task *channel_task, |
| 409 |
void *arg, |
| 410 |
enum aws_task_status status) { |
| 411 |
(void)channel_task; |
| 412 |
|
| 413 |
struct aws_http_connection_acquisition *pending_acquisition = arg; |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
if (status != AWS_TASK_STATUS_RUN_READY) { |
| 418 |
AWS_LOGF_WARN( |
| 419 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 420 |
"id=%p: Failed to complete connection acquisition because the connection was closed", |
| 421 |
(void *)pending_acquisition->manager); |
| 422 |
pending_acquisition->callback(NULL, AWS_ERROR_HTTP_CONNECTION_CLOSED, pending_acquisition->user_data); |
| 423 |
|
| 424 |
aws_http_connection_manager_release_connection(pending_acquisition->manager, pending_acquisition->connection); |
| 425 |
} else { |
| 426 |
AWS_LOGF_DEBUG( |
| 427 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 428 |
"id=%p: Successfully completed connection acquisition with connection id=%p", |
| 429 |
(void *)pending_acquisition->manager, |
| 430 |
(void *)pending_acquisition->connection); |
| 431 |
pending_acquisition->callback( |
| 432 |
pending_acquisition->connection, pending_acquisition->error_code, pending_acquisition->user_data); |
| 433 |
} |
| 434 |
|
| 435 |
aws_mem_release(pending_acquisition->allocator, pending_acquisition); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
static void s_aws_http_connection_manager_complete_acquisitions( |
| 447 |
struct aws_linked_list *acquisitions, |
| 448 |
struct aws_allocator *allocator) { |
| 449 |
|
| 450 |
while (!aws_linked_list_empty(acquisitions)) { |
| 451 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(acquisitions); |
| 452 |
struct aws_http_connection_acquisition *pending_acquisition = |
| 453 |
AWS_CONTAINER_OF(node, struct aws_http_connection_acquisition, node); |
| 454 |
|
| 455 |
if (pending_acquisition->error_code == AWS_OP_SUCCESS) { |
| 456 |
|
| 457 |
struct aws_channel *channel = pending_acquisition->manager->system_vtable->aws_http_connection_get_channel( |
| 458 |
pending_acquisition->connection); |
| 459 |
AWS_PRECONDITION(channel); |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
if (!pending_acquisition->manager->system_vtable->aws_channel_thread_is_callers_thread(channel)) { |
| 464 |
aws_channel_task_init( |
| 465 |
&pending_acquisition->acquisition_task, |
| 466 |
s_connection_acquisition_task, |
| 467 |
pending_acquisition, |
| 468 |
"s_connection_acquisition_task"); |
| 469 |
aws_channel_schedule_task_now(channel, &pending_acquisition->acquisition_task); |
| 470 |
return; |
| 471 |
} |
| 472 |
AWS_LOGF_DEBUG( |
| 473 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 474 |
"id=%p: Successfully completed connection acquisition with connection id=%p", |
| 475 |
(void *)pending_acquisition->manager, |
| 476 |
(void *)pending_acquisition->connection); |
| 477 |
|
| 478 |
} else { |
| 479 |
AWS_LOGF_WARN( |
| 480 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 481 |
"id=%p: Failed to complete connection acquisition with error_code %d(%s)", |
| 482 |
(void *)pending_acquisition->manager, |
| 483 |
pending_acquisition->error_code, |
| 484 |
aws_error_str(pending_acquisition->error_code)); |
| 485 |
} |
| 486 |
|
| 487 |
pending_acquisition->callback( |
| 488 |
pending_acquisition->connection, pending_acquisition->error_code, pending_acquisition->user_data); |
| 489 |
aws_mem_release(allocator, pending_acquisition); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
static void s_aws_http_connection_manager_move_front_acquisition( |
| 504 |
struct aws_http_connection_manager *manager, |
| 505 |
struct aws_http_connection *connection, |
| 506 |
int error_code, |
| 507 |
struct aws_linked_list *output_list) { |
| 508 |
|
| 509 |
AWS_FATAL_ASSERT(!aws_linked_list_empty(&manager->pending_acquisitions)); |
| 510 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(&manager->pending_acquisitions); |
| 511 |
|
| 512 |
AWS_FATAL_ASSERT(manager->pending_acquisition_count > 0); |
| 513 |
--manager->pending_acquisition_count; |
| 514 |
|
| 515 |
if (error_code == AWS_ERROR_SUCCESS && connection == NULL) { |
| 516 |
AWS_LOGF_FATAL( |
| 517 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 518 |
"id=%p: Connection acquisition completed with NULL connection and no error code. Investigate.", |
| 519 |
(void *)manager); |
| 520 |
error_code = AWS_ERROR_UNKNOWN; |
| 521 |
} |
| 522 |
|
| 523 |
struct aws_http_connection_acquisition *pending_acquisition = |
| 524 |
AWS_CONTAINER_OF(node, struct aws_http_connection_acquisition, node); |
| 525 |
pending_acquisition->connection = connection; |
| 526 |
pending_acquisition->error_code = error_code; |
| 527 |
|
| 528 |
aws_linked_list_push_back(output_list, node); |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
struct aws_connection_management_transaction { |
| 544 |
struct aws_http_connection_manager *manager; |
| 545 |
struct aws_allocator *allocator; |
| 546 |
struct aws_linked_list completions; |
| 547 |
struct aws_http_connection *connection_to_release; |
| 548 |
struct aws_linked_list connections_to_release; |
| 549 |
struct aws_http_connection_manager_snapshot snapshot; |
| 550 |
size_t new_connections; |
| 551 |
}; |
| 552 |
|
| 553 |
static void s_aws_connection_management_transaction_init( |
| 554 |
struct aws_connection_management_transaction *work, |
| 555 |
struct aws_http_connection_manager *manager) { |
| 556 |
AWS_ZERO_STRUCT(*work); |
| 557 |
|
| 558 |
aws_linked_list_init(&work->connections_to_release); |
| 559 |
aws_linked_list_init(&work->completions); |
| 560 |
work->manager = manager; |
| 561 |
work->allocator = manager->allocator; |
| 562 |
aws_ref_count_acquire(&manager->internal_ref_count); |
| 563 |
} |
| 564 |
|
| 565 |
static void s_aws_connection_management_transaction_clean_up(struct aws_connection_management_transaction *work) { |
| 566 |
AWS_FATAL_ASSERT(aws_linked_list_empty(&work->connections_to_release)); |
| 567 |
AWS_FATAL_ASSERT(aws_linked_list_empty(&work->completions)); |
| 568 |
AWS_ASSERT(work->manager); |
| 569 |
aws_ref_count_release(&work->manager->internal_ref_count); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
static void s_connection_manager_internal_ref_increase( |
| 574 |
struct aws_http_connection_manager *manager, |
| 575 |
enum aws_http_connection_manager_count_type count_type, |
| 576 |
size_t num) { |
| 577 |
|
| 578 |
manager->internal_ref[count_type] += num; |
| 579 |
for (size_t i = 0; i < num; i++) { |
| 580 |
aws_ref_count_acquire(&manager->internal_ref_count); |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
static void s_connection_manager_internal_ref_decrease( |
| 585 |
struct aws_http_connection_manager *manager, |
| 586 |
enum aws_http_connection_manager_count_type count_type, |
| 587 |
size_t num) { |
| 588 |
|
| 589 |
manager->internal_ref[count_type] -= num; |
| 590 |
for (size_t i = 0; i < num; i++) { |
| 591 |
|
| 592 |
|
| 593 |
aws_ref_count_release(&manager->internal_ref_count); |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
static void s_aws_http_connection_manager_build_transaction(struct aws_connection_management_transaction *work) { |
| 599 |
struct aws_http_connection_manager *manager = work->manager; |
| 600 |
|
| 601 |
if (manager->state == AWS_HCMST_READY) { |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
while (!aws_linked_list_empty(&manager->idle_connections) > 0 && manager->pending_acquisition_count > 0) { |
| 606 |
AWS_FATAL_ASSERT(manager->idle_connection_count >= 1); |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
struct aws_linked_list_node *node = aws_linked_list_pop_back(&manager->idle_connections); |
| 615 |
struct aws_idle_connection *idle_connection = AWS_CONTAINER_OF(node, struct aws_idle_connection, node); |
| 616 |
struct aws_http_connection *connection = idle_connection->connection; |
| 617 |
|
| 618 |
AWS_LOGF_DEBUG( |
| 619 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 620 |
"id=%p: Grabbing pooled connection (%p)", |
| 621 |
(void *)manager, |
| 622 |
(void *)connection); |
| 623 |
s_aws_http_connection_manager_move_front_acquisition( |
| 624 |
manager, connection, AWS_ERROR_SUCCESS, &work->completions); |
| 625 |
s_connection_manager_internal_ref_increase(manager, AWS_HCMCT_VENDED_CONNECTION, 1); |
| 626 |
--manager->idle_connection_count; |
| 627 |
aws_mem_release(idle_connection->allocator, idle_connection); |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
if (manager->pending_acquisition_count > |
| 634 |
manager->internal_ref[AWS_HCMCT_PENDING_CONNECTIONS] + manager->pending_settings_count) { |
| 635 |
AWS_FATAL_ASSERT( |
| 636 |
manager->max_connections >= manager->internal_ref[AWS_HCMCT_VENDED_CONNECTION] + |
| 637 |
manager->internal_ref[AWS_HCMCT_PENDING_CONNECTIONS] + |
| 638 |
manager->pending_settings_count); |
| 639 |
|
| 640 |
work->new_connections = manager->pending_acquisition_count - |
| 641 |
manager->internal_ref[AWS_HCMCT_PENDING_CONNECTIONS] - |
| 642 |
manager->pending_settings_count; |
| 643 |
size_t max_new_connections = |
| 644 |
manager->max_connections - |
| 645 |
(manager->internal_ref[AWS_HCMCT_VENDED_CONNECTION] + |
| 646 |
manager->internal_ref[AWS_HCMCT_PENDING_CONNECTIONS] + manager->pending_settings_count); |
| 647 |
|
| 648 |
if (work->new_connections > max_new_connections) { |
| 649 |
work->new_connections = max_new_connections; |
| 650 |
} |
| 651 |
s_connection_manager_internal_ref_increase(manager, AWS_HCMCT_PENDING_CONNECTIONS, work->new_connections); |
| 652 |
} |
| 653 |
} else { |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
AWS_FATAL_ASSERT(aws_linked_list_empty(&work->connections_to_release)); |
| 658 |
aws_linked_list_swap_contents(&manager->idle_connections, &work->connections_to_release); |
| 659 |
manager->idle_connection_count = 0; |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
while (!aws_linked_list_empty(&manager->pending_acquisitions)) { |
| 665 |
AWS_LOGF_DEBUG( |
| 666 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 667 |
"id=%p: Failing pending connection acquisition due to manager shut down", |
| 668 |
(void *)manager); |
| 669 |
s_aws_http_connection_manager_move_front_acquisition( |
| 670 |
manager, NULL, AWS_ERROR_HTTP_CONNECTION_MANAGER_SHUTTING_DOWN, &work->completions); |
| 671 |
} |
| 672 |
|
| 673 |
AWS_LOGF_INFO( |
| 674 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 675 |
"id=%p: manager release, failing %zu pending acquisitions", |
| 676 |
(void *)manager, |
| 677 |
manager->pending_acquisition_count); |
| 678 |
manager->pending_acquisition_count = 0; |
| 679 |
} |
| 680 |
|
| 681 |
s_aws_http_connection_manager_get_snapshot(manager, &work->snapshot); |
| 682 |
} |
| 683 |
|
| 684 |
static void s_aws_http_connection_manager_execute_transaction(struct aws_connection_management_transaction *work); |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
static void s_aws_http_connection_manager_finish_destroy(void *user_data) { |
| 696 |
struct aws_http_connection_manager *manager = user_data; |
| 697 |
if (manager == NULL) { |
| 698 |
return; |
| 699 |
} |
| 700 |
|
| 701 |
AWS_LOGF_INFO(AWS_LS_HTTP_CONNECTION_MANAGER, "id=%p: Destroying self", (void *)manager); |
| 702 |
|
| 703 |
AWS_FATAL_ASSERT(manager->internal_ref[AWS_HCMCT_PENDING_CONNECTIONS] == 0); |
| 704 |
AWS_FATAL_ASSERT(manager->pending_settings_count == 0); |
| 705 |
AWS_FATAL_ASSERT(manager->internal_ref[AWS_HCMCT_VENDED_CONNECTION] == 0); |
| 706 |
AWS_FATAL_ASSERT(manager->pending_acquisition_count == 0); |
| 707 |
AWS_FATAL_ASSERT(manager->internal_ref[AWS_HCMCT_OPEN_CONNECTION] == 0); |
| 708 |
AWS_FATAL_ASSERT(aws_linked_list_empty(&manager->pending_acquisitions)); |
| 709 |
AWS_FATAL_ASSERT(aws_linked_list_empty(&manager->idle_connections)); |
| 710 |
|
| 711 |
aws_string_destroy(manager->host); |
| 712 |
if (manager->initial_settings) { |
| 713 |
aws_array_list_clean_up(manager->initial_settings); |
| 714 |
aws_mem_release(manager->allocator, manager->initial_settings); |
| 715 |
} |
| 716 |
if (manager->tls_connection_options) { |
| 717 |
aws_tls_connection_options_clean_up(manager->tls_connection_options); |
| 718 |
aws_mem_release(manager->allocator, manager->tls_connection_options); |
| 719 |
} |
| 720 |
if (manager->proxy_ev_tls_options) { |
| 721 |
aws_tls_connection_options_clean_up(manager->proxy_ev_tls_options); |
| 722 |
aws_mem_release(manager->allocator, manager->proxy_ev_tls_options); |
| 723 |
} |
| 724 |
if (manager->proxy_config) { |
| 725 |
aws_http_proxy_config_destroy(manager->proxy_config); |
| 726 |
} |
| 727 |
|
| 728 |
for (size_t i = 0; i < aws_array_list_length(&manager->network_interface_names); i++) { |
| 729 |
struct aws_string *interface_name = NULL; |
| 730 |
aws_array_list_get_at(&manager->network_interface_names, &interface_name, i); |
| 731 |
aws_string_destroy(interface_name); |
| 732 |
} |
| 733 |
aws_array_list_clean_up(&manager->network_interface_names); |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
if (manager->cull_task) { |
| 741 |
aws_mem_release(manager->allocator, manager->cull_task); |
| 742 |
} |
| 743 |
|
| 744 |
aws_mutex_clean_up(&manager->lock); |
| 745 |
|
| 746 |
aws_client_bootstrap_release(manager->bootstrap); |
| 747 |
|
| 748 |
if (manager->shutdown_complete_callback) { |
| 749 |
manager->shutdown_complete_callback(manager->shutdown_complete_user_data); |
| 750 |
} |
| 751 |
|
| 752 |
aws_mem_release(manager->allocator, manager); |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
static void s_final_destruction_task(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 757 |
(void)status; |
| 758 |
struct aws_http_connection_manager *manager = arg; |
| 759 |
struct aws_allocator *allocator = manager->allocator; |
| 760 |
|
| 761 |
AWS_FATAL_ASSERT(manager->cull_task != NULL); |
| 762 |
AWS_FATAL_ASSERT(manager->cull_event_loop != NULL); |
| 763 |
|
| 764 |
aws_event_loop_cancel_task(manager->cull_event_loop, manager->cull_task); |
| 765 |
aws_mem_release(allocator, task); |
| 766 |
|
| 767 |
|
| 768 |
aws_ref_count_release(&manager->internal_ref_count); |
| 769 |
} |
| 770 |
|
| 771 |
static void s_cull_task(struct aws_task *task, void *arg, enum aws_task_status status); |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
static uint64_t s_calculate_idle_connection_cull_task_time_synced(struct aws_http_connection_manager *manager) { |
| 778 |
if (manager->max_connection_idle_in_milliseconds == 0) { |
| 779 |
return UINT64_MAX; |
| 780 |
} |
| 781 |
uint64_t cull_task_time = 0; |
| 782 |
|
| 783 |
const struct aws_linked_list_node *end = aws_linked_list_end(&manager->idle_connections); |
| 784 |
struct aws_linked_list_node *oldest_node = aws_linked_list_begin(&manager->idle_connections); |
| 785 |
if (oldest_node != end) { |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
struct aws_idle_connection *oldest_idle_connection = |
| 791 |
AWS_CONTAINER_OF(oldest_node, struct aws_idle_connection, node); |
| 792 |
cull_task_time = oldest_idle_connection->cull_timestamp; |
| 793 |
} else { |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
uint64_t now = 0; |
| 799 |
manager->system_vtable->aws_high_res_clock_get_ticks(&now); |
| 800 |
cull_task_time = |
| 801 |
now + aws_timestamp_convert( |
| 802 |
manager->max_connection_idle_in_milliseconds, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); |
| 803 |
} |
| 804 |
|
| 805 |
return cull_task_time; |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
static uint64_t s_calculate_pending_acquisition_cull_task_time_synced(struct aws_http_connection_manager *manager) { |
| 813 |
if (manager->connection_acquisition_timeout_ms == 0) { |
| 814 |
return UINT64_MAX; |
| 815 |
} |
| 816 |
|
| 817 |
uint64_t cull_task_time = 0; |
| 818 |
|
| 819 |
const struct aws_linked_list_node *end = aws_linked_list_end(&manager->pending_acquisitions); |
| 820 |
struct aws_linked_list_node *oldest_node = aws_linked_list_begin(&manager->pending_acquisitions); |
| 821 |
if (oldest_node != end) { |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
struct aws_http_connection_acquisition *oldest_pending_acquire = |
| 826 |
AWS_CONTAINER_OF(oldest_node, struct aws_http_connection_acquisition, node); |
| 827 |
cull_task_time = oldest_pending_acquire->timeout_timestamp; |
| 828 |
} else { |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
uint64_t now = 0; |
| 834 |
manager->system_vtable->aws_high_res_clock_get_ticks(&now); |
| 835 |
cull_task_time = |
| 836 |
now + aws_timestamp_convert( |
| 837 |
manager->connection_acquisition_timeout_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); |
| 838 |
} |
| 839 |
return cull_task_time; |
| 840 |
} |
| 841 |
|
| 842 |
static void s_schedule_culling(struct aws_http_connection_manager *manager) { |
| 843 |
if (manager->max_connection_idle_in_milliseconds == 0 && manager->connection_acquisition_timeout_ms == 0) { |
| 844 |
return; |
| 845 |
} |
| 846 |
|
| 847 |
if (manager->cull_task == NULL) { |
| 848 |
manager->cull_task = aws_mem_calloc(manager->allocator, 1, sizeof(struct aws_task)); |
| 849 |
aws_task_init(manager->cull_task, s_cull_task, manager, "cull_idle_connections"); |
| 850 |
|
| 851 |
aws_ref_count_acquire(&manager->internal_ref_count); |
| 852 |
} |
| 853 |
|
| 854 |
if (manager->cull_event_loop == NULL) { |
| 855 |
manager->cull_event_loop = aws_event_loop_group_get_next_loop(manager->bootstrap->event_loop_group); |
| 856 |
} |
| 857 |
AWS_FATAL_ASSERT(manager->cull_event_loop != NULL); |
| 858 |
|
| 859 |
aws_mutex_lock(&manager->lock); |
| 860 |
uint64_t idle_cull_time = s_calculate_idle_connection_cull_task_time_synced(manager); |
| 861 |
uint64_t acquisition_cull_time = s_calculate_pending_acquisition_cull_task_time_synced(manager); |
| 862 |
aws_mutex_unlock(&manager->lock); |
| 863 |
|
| 864 |
aws_event_loop_schedule_task_future( |
| 865 |
manager->cull_event_loop, manager->cull_task, aws_min_u64(idle_cull_time, acquisition_cull_time)); |
| 866 |
} |
| 867 |
|
| 868 |
struct aws_http_connection_manager *aws_http_connection_manager_new( |
| 869 |
struct aws_allocator *allocator, |
| 870 |
const struct aws_http_connection_manager_options *options) { |
| 871 |
|
| 872 |
aws_http_fatal_assert_library_initialized(); |
| 873 |
|
| 874 |
if (!options) { |
| 875 |
AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION_MANAGER, "Invalid options - options is null"); |
| 876 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 877 |
return NULL; |
| 878 |
} |
| 879 |
|
| 880 |
if (!options->socket_options) { |
| 881 |
AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION_MANAGER, "Invalid options - socket_options is null"); |
| 882 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 883 |
return NULL; |
| 884 |
} |
| 885 |
|
| 886 |
if (options->max_connections == 0) { |
| 887 |
AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION_MANAGER, "Invalid options - max_connections cannot be 0"); |
| 888 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 889 |
return NULL; |
| 890 |
} |
| 891 |
|
| 892 |
if (options->tls_connection_options && options->http2_prior_knowledge) { |
| 893 |
AWS_LOGF_ERROR( |
| 894 |
AWS_LS_HTTP_CONNECTION_MANAGER, "Invalid options - HTTP/2 prior knowledge cannot be set when TLS is used"); |
| 895 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 896 |
return NULL; |
| 897 |
} |
| 898 |
|
| 899 |
if (options->socket_options->network_interface_name[0] != '\0' && options->num_network_interface_names > 0) { |
| 900 |
AWS_LOGF_ERROR( |
| 901 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 902 |
"Invalid options - socket_options.network_interface_name and network_interface_names_array cannot be both " |
| 903 |
"set."); |
| 904 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 905 |
return NULL; |
| 906 |
} |
| 907 |
|
| 908 |
struct aws_http_connection_manager *manager = |
| 909 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http_connection_manager)); |
| 910 |
if (manager == NULL) { |
| 911 |
return NULL; |
| 912 |
} |
| 913 |
|
| 914 |
manager->allocator = allocator; |
| 915 |
|
| 916 |
if (aws_mutex_init(&manager->lock)) { |
| 917 |
goto on_error; |
| 918 |
} |
| 919 |
|
| 920 |
aws_ref_count_init( |
| 921 |
&manager->internal_ref_count, |
| 922 |
manager, |
| 923 |
(aws_simple_completion_callback *)s_aws_http_connection_manager_finish_destroy); |
| 924 |
|
| 925 |
aws_linked_list_init(&manager->idle_connections); |
| 926 |
aws_linked_list_init(&manager->pending_acquisitions); |
| 927 |
|
| 928 |
manager->host = aws_string_new_from_cursor(allocator, &options->host); |
| 929 |
if (manager->host == NULL) { |
| 930 |
goto on_error; |
| 931 |
} |
| 932 |
|
| 933 |
if (options->tls_connection_options) { |
| 934 |
manager->tls_connection_options = aws_mem_calloc(allocator, 1, sizeof(struct aws_tls_connection_options)); |
| 935 |
if (aws_tls_connection_options_copy(manager->tls_connection_options, options->tls_connection_options)) { |
| 936 |
goto on_error; |
| 937 |
} |
| 938 |
} |
| 939 |
if (options->proxy_options) { |
| 940 |
manager->proxy_config = aws_http_proxy_config_new_from_manager_options(allocator, options); |
| 941 |
if (manager->proxy_config == NULL) { |
| 942 |
goto on_error; |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
if (options->monitoring_options) { |
| 947 |
manager->monitoring_options = *options->monitoring_options; |
| 948 |
} |
| 949 |
|
| 950 |
manager->state = AWS_HCMST_READY; |
| 951 |
manager->initial_window_size = options->initial_window_size; |
| 952 |
manager->port = options->port; |
| 953 |
manager->max_connections = options->max_connections; |
| 954 |
manager->socket_options = *options->socket_options; |
| 955 |
manager->bootstrap = aws_client_bootstrap_acquire(options->bootstrap); |
| 956 |
manager->system_vtable = g_aws_http_connection_manager_default_system_vtable_ptr; |
| 957 |
manager->external_ref_count = 1; |
| 958 |
manager->shutdown_complete_callback = options->shutdown_complete_callback; |
| 959 |
manager->shutdown_complete_user_data = options->shutdown_complete_user_data; |
| 960 |
manager->enable_read_back_pressure = options->enable_read_back_pressure; |
| 961 |
manager->max_connection_idle_in_milliseconds = options->max_connection_idle_in_milliseconds; |
| 962 |
manager->connection_acquisition_timeout_ms = options->connection_acquisition_timeout_ms; |
| 963 |
manager->max_pending_connection_acquisitions = options->max_pending_connection_acquisitions; |
| 964 |
manager->response_first_byte_timeout_ms = options->response_first_byte_timeout_ms; |
| 965 |
|
| 966 |
if (options->proxy_ev_settings) { |
| 967 |
manager->proxy_ev_settings = *options->proxy_ev_settings; |
| 968 |
} |
| 969 |
if (manager->proxy_ev_settings.tls_options) { |
| 970 |
manager->proxy_ev_tls_options = aws_mem_calloc(allocator, 1, sizeof(struct aws_tls_connection_options)); |
| 971 |
if (aws_tls_connection_options_copy(manager->proxy_ev_tls_options, manager->proxy_ev_settings.tls_options)) { |
| 972 |
goto on_error; |
| 973 |
} |
| 974 |
manager->proxy_ev_settings.tls_options = manager->proxy_ev_tls_options; |
| 975 |
} |
| 976 |
manager->http2_prior_knowledge = options->http2_prior_knowledge; |
| 977 |
if (options->num_initial_settings > 0) { |
| 978 |
manager->initial_settings = aws_mem_calloc(allocator, 1, sizeof(struct aws_array_list)); |
| 979 |
aws_array_list_init_dynamic( |
| 980 |
manager->initial_settings, allocator, options->num_initial_settings, sizeof(struct aws_http2_setting)); |
| 981 |
|
| 982 |
memcpy( |
| 983 |
manager->initial_settings->data, |
| 984 |
options->initial_settings_array, |
| 985 |
options->num_initial_settings * sizeof(struct aws_http2_setting)); |
| 986 |
|
| 987 |
manager->initial_settings->length = options->num_initial_settings; |
| 988 |
} |
| 989 |
manager->max_closed_streams = options->max_closed_streams; |
| 990 |
manager->http2_conn_manual_window_management = options->http2_conn_manual_window_management; |
| 991 |
|
| 992 |
manager->network_interface_names_index = 0; |
| 993 |
if (options->num_network_interface_names > 0) { |
| 994 |
aws_array_list_init_dynamic( |
| 995 |
&manager->network_interface_names, |
| 996 |
allocator, |
| 997 |
options->num_network_interface_names, |
| 998 |
sizeof(struct aws_string *)); |
| 999 |
for (size_t i = 0; i < options->num_network_interface_names; i++) { |
| 1000 |
struct aws_byte_cursor interface_name = options->network_interface_names_array[i]; |
| 1001 |
struct aws_string *interface_name_str = aws_string_new_from_cursor(allocator, &interface_name); |
| 1002 |
aws_array_list_push_back(&manager->network_interface_names, &interface_name_str); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
s_schedule_culling(manager); |
| 1008 |
|
| 1009 |
AWS_LOGF_INFO(AWS_LS_HTTP_CONNECTION_MANAGER, "id=%p: Successfully created", (void *)manager); |
| 1010 |
|
| 1011 |
return manager; |
| 1012 |
|
| 1013 |
on_error: |
| 1014 |
|
| 1015 |
s_aws_http_connection_manager_finish_destroy(manager); |
| 1016 |
|
| 1017 |
return NULL; |
| 1018 |
} |
| 1019 |
|
| 1020 |
void aws_http_connection_manager_acquire(struct aws_http_connection_manager *manager) { |
| 1021 |
aws_mutex_lock(&manager->lock); |
| 1022 |
AWS_FATAL_ASSERT(manager->external_ref_count > 0); |
| 1023 |
manager->external_ref_count += 1; |
| 1024 |
aws_mutex_unlock(&manager->lock); |
| 1025 |
} |
| 1026 |
|
| 1027 |
void aws_http_connection_manager_release(struct aws_http_connection_manager *manager) { |
| 1028 |
struct aws_connection_management_transaction work; |
| 1029 |
s_aws_connection_management_transaction_init(&work, manager); |
| 1030 |
|
| 1031 |
AWS_LOGF_INFO(AWS_LS_HTTP_CONNECTION_MANAGER, "id=%p: release", (void *)manager); |
| 1032 |
|
| 1033 |
aws_mutex_lock(&manager->lock); |
| 1034 |
|
| 1035 |
if (manager->external_ref_count > 0) { |
| 1036 |
manager->external_ref_count -= 1; |
| 1037 |
|
| 1038 |
if (manager->external_ref_count == 0) { |
| 1039 |
AWS_LOGF_INFO( |
| 1040 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1041 |
"id=%p: ref count now zero, starting shut down process", |
| 1042 |
(void *)manager); |
| 1043 |
manager->state = AWS_HCMST_SHUTTING_DOWN; |
| 1044 |
s_aws_http_connection_manager_build_transaction(&work); |
| 1045 |
if (manager->cull_task != NULL) { |
| 1046 |
|
| 1047 |
AWS_FATAL_ASSERT(manager->cull_event_loop); |
| 1048 |
struct aws_task *final_destruction_task = |
| 1049 |
aws_mem_calloc(manager->allocator, 1, sizeof(struct aws_task)); |
| 1050 |
aws_task_init(final_destruction_task, s_final_destruction_task, manager, "final_scheduled_destruction"); |
| 1051 |
aws_event_loop_schedule_task_now(manager->cull_event_loop, final_destruction_task); |
| 1052 |
} |
| 1053 |
aws_ref_count_release(&manager->internal_ref_count); |
| 1054 |
} |
| 1055 |
} else { |
| 1056 |
AWS_LOGF_ERROR( |
| 1057 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1058 |
"id=%p: Connection manager release called with a zero reference count", |
| 1059 |
(void *)manager); |
| 1060 |
} |
| 1061 |
|
| 1062 |
aws_mutex_unlock(&manager->lock); |
| 1063 |
|
| 1064 |
s_aws_http_connection_manager_execute_transaction(&work); |
| 1065 |
} |
| 1066 |
|
| 1067 |
static void s_aws_http_connection_manager_on_connection_setup( |
| 1068 |
struct aws_http_connection *connection, |
| 1069 |
int error_code, |
| 1070 |
void *user_data); |
| 1071 |
|
| 1072 |
static void s_aws_http_connection_manager_on_connection_shutdown( |
| 1073 |
struct aws_http_connection *connection, |
| 1074 |
int error_code, |
| 1075 |
void *user_data); |
| 1076 |
|
| 1077 |
static void s_aws_http_connection_manager_h2_on_goaway_received( |
| 1078 |
struct aws_http_connection *http2_connection, |
| 1079 |
uint32_t last_stream_id, |
| 1080 |
uint32_t http2_error_code, |
| 1081 |
struct aws_byte_cursor debug_data, |
| 1082 |
void *user_data); |
| 1083 |
|
| 1084 |
static void s_aws_http_connection_manager_h2_on_initial_settings_completed( |
| 1085 |
struct aws_http_connection *http2_connection, |
| 1086 |
int error_code, |
| 1087 |
void *user_data); |
| 1088 |
|
| 1089 |
static int s_aws_http_connection_manager_new_connection(struct aws_http_connection_manager *manager) { |
| 1090 |
struct aws_http_client_connection_options options; |
| 1091 |
AWS_ZERO_STRUCT(options); |
| 1092 |
options.self_size = sizeof(struct aws_http_client_connection_options); |
| 1093 |
options.bootstrap = manager->bootstrap; |
| 1094 |
options.tls_options = manager->tls_connection_options; |
| 1095 |
options.allocator = manager->allocator; |
| 1096 |
options.user_data = manager; |
| 1097 |
options.host_name = aws_byte_cursor_from_string(manager->host); |
| 1098 |
options.port = manager->port; |
| 1099 |
options.initial_window_size = manager->initial_window_size; |
| 1100 |
options.response_first_byte_timeout_ms = manager->response_first_byte_timeout_ms; |
| 1101 |
struct aws_socket_options socket_options = manager->socket_options; |
| 1102 |
if (aws_array_list_length(&manager->network_interface_names)) { |
| 1103 |
struct aws_string *interface_name = NULL; |
| 1104 |
aws_array_list_get_at( |
| 1105 |
&manager->network_interface_names, &interface_name, manager->network_interface_names_index); |
| 1106 |
manager->network_interface_names_index = |
| 1107 |
(manager->network_interface_names_index + 1) % aws_array_list_length(&manager->network_interface_names); |
| 1108 |
#if defined(_MSC_VER) |
| 1109 |
# pragma warning(push) |
| 1110 |
# pragma warning(disable : 4996) |
| 1111 |
#endif |
| 1112 |
|
| 1113 |
|
| 1114 |
strncpy( |
| 1115 |
socket_options.network_interface_name, aws_string_c_str(interface_name), AWS_NETWORK_INTERFACE_NAME_MAX); |
| 1116 |
#if defined(_MSC_VER) |
| 1117 |
# pragma warning(pop) |
| 1118 |
#endif |
| 1119 |
} |
| 1120 |
options.socket_options = &socket_options; |
| 1121 |
options.on_setup = s_aws_http_connection_manager_on_connection_setup; |
| 1122 |
options.on_shutdown = s_aws_http_connection_manager_on_connection_shutdown; |
| 1123 |
options.manual_window_management = manager->enable_read_back_pressure; |
| 1124 |
options.proxy_ev_settings = &manager->proxy_ev_settings; |
| 1125 |
options.prior_knowledge_http2 = manager->http2_prior_knowledge; |
| 1126 |
|
| 1127 |
struct aws_http2_connection_options h2_options; |
| 1128 |
AWS_ZERO_STRUCT(h2_options); |
| 1129 |
if (manager->initial_settings) { |
| 1130 |
h2_options.initial_settings_array = manager->initial_settings->data; |
| 1131 |
h2_options.num_initial_settings = aws_array_list_length(manager->initial_settings); |
| 1132 |
} |
| 1133 |
h2_options.max_closed_streams = manager->max_closed_streams; |
| 1134 |
h2_options.conn_manual_window_management = manager->http2_conn_manual_window_management; |
| 1135 |
|
| 1136 |
|
| 1137 |
h2_options.on_initial_settings_completed = s_aws_http_connection_manager_h2_on_initial_settings_completed; |
| 1138 |
h2_options.on_goaway_received = s_aws_http_connection_manager_h2_on_goaway_received; |
| 1139 |
|
| 1140 |
options.http2_options = &h2_options; |
| 1141 |
|
| 1142 |
if (aws_http_connection_monitoring_options_is_valid(&manager->monitoring_options)) { |
| 1143 |
options.monitoring_options = &manager->monitoring_options; |
| 1144 |
} |
| 1145 |
|
| 1146 |
struct aws_http_proxy_options proxy_options; |
| 1147 |
AWS_ZERO_STRUCT(proxy_options); |
| 1148 |
|
| 1149 |
if (manager->proxy_config) { |
| 1150 |
aws_http_proxy_options_init_from_config(&proxy_options, manager->proxy_config); |
| 1151 |
options.proxy_options = &proxy_options; |
| 1152 |
} |
| 1153 |
|
| 1154 |
if (manager->system_vtable->aws_http_client_connect(&options)) { |
| 1155 |
AWS_LOGF_ERROR( |
| 1156 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1157 |
"id=%p: http connection creation failed with error code %d(%s)", |
| 1158 |
(void *)manager, |
| 1159 |
aws_last_error(), |
| 1160 |
aws_error_str(aws_last_error())); |
| 1161 |
return AWS_OP_ERR; |
| 1162 |
} |
| 1163 |
|
| 1164 |
return AWS_OP_SUCCESS; |
| 1165 |
} |
| 1166 |
|
| 1167 |
static void s_aws_http_connection_manager_execute_transaction(struct aws_connection_management_transaction *work) { |
| 1168 |
|
| 1169 |
struct aws_http_connection_manager *manager = work->manager; |
| 1170 |
|
| 1171 |
size_t new_connection_failures = 0; |
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
s_aws_http_connection_manager_log_snapshot(manager, &work->snapshot); |
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
while (!aws_linked_list_empty(&work->connections_to_release)) { |
| 1182 |
struct aws_linked_list_node *node = aws_linked_list_pop_back(&work->connections_to_release); |
| 1183 |
struct aws_idle_connection *idle_connection = AWS_CONTAINER_OF(node, struct aws_idle_connection, node); |
| 1184 |
|
| 1185 |
AWS_LOGF_INFO( |
| 1186 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1187 |
"id=%p: Releasing connection (id=%p)", |
| 1188 |
(void *)manager, |
| 1189 |
(void *)idle_connection->connection); |
| 1190 |
manager->system_vtable->aws_http_connection_release(idle_connection->connection); |
| 1191 |
aws_mem_release(idle_connection->allocator, idle_connection); |
| 1192 |
} |
| 1193 |
|
| 1194 |
if (work->connection_to_release) { |
| 1195 |
AWS_LOGF_INFO( |
| 1196 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1197 |
"id=%p: Releasing connection (id=%p)", |
| 1198 |
(void *)manager, |
| 1199 |
(void *)work->connection_to_release); |
| 1200 |
manager->system_vtable->aws_http_connection_release(work->connection_to_release); |
| 1201 |
} |
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
struct aws_array_list errors; |
| 1207 |
AWS_ZERO_STRUCT(errors); |
| 1208 |
|
| 1209 |
if (work->new_connections > 0) { |
| 1210 |
AWS_LOGF_INFO( |
| 1211 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1212 |
"id=%p: Requesting %zu new connections from http", |
| 1213 |
(void *)manager, |
| 1214 |
work->new_connections); |
| 1215 |
AWS_FATAL_ASSERT( |
| 1216 |
aws_array_list_init_dynamic(&errors, work->allocator, work->new_connections, sizeof(int)) == |
| 1217 |
AWS_OP_SUCCESS); |
| 1218 |
} |
| 1219 |
|
| 1220 |
for (size_t i = 0; i < work->new_connections; ++i) { |
| 1221 |
if (s_aws_http_connection_manager_new_connection(manager)) { |
| 1222 |
++new_connection_failures; |
| 1223 |
int error = aws_last_error(); |
| 1224 |
AWS_FATAL_ASSERT(aws_array_list_push_back(&errors, &error) == AWS_OP_SUCCESS); |
| 1225 |
} |
| 1226 |
} |
| 1227 |
|
| 1228 |
bool has_pending_acquisitions = false; |
| 1229 |
struct aws_connection_management_transaction pending_acquisitions_work; |
| 1230 |
if (new_connection_failures > 0) { |
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
aws_mutex_lock(&manager->lock); |
| 1236 |
|
| 1237 |
AWS_FATAL_ASSERT(manager->internal_ref[AWS_HCMCT_PENDING_CONNECTIONS] >= new_connection_failures); |
| 1238 |
s_connection_manager_internal_ref_decrease(manager, AWS_HCMCT_PENDING_CONNECTIONS, new_connection_failures); |
| 1239 |
|
| 1240 |
for (size_t i = 0; i < new_connection_failures && manager->pending_acquisition_count > 0; i++) { |
| 1241 |
int error; |
| 1242 |
aws_array_list_get_at(&errors, &error, i); |
| 1243 |
AWS_LOGF_DEBUG( |
| 1244 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1245 |
"id=%p: Failing connection acquisition with error code %d", |
| 1246 |
(void *)manager, |
| 1247 |
(int)error); |
| 1248 |
s_aws_http_connection_manager_move_front_acquisition(manager, NULL, error, &work->completions); |
| 1249 |
} |
| 1250 |
has_pending_acquisitions = |
| 1251 |
manager->internal_ref[AWS_HCMCT_PENDING_CONNECTIONS] + manager->pending_settings_count < |
| 1252 |
manager->pending_acquisition_count; |
| 1253 |
if (has_pending_acquisitions) { |
| 1254 |
|
| 1255 |
s_aws_connection_management_transaction_init(&pending_acquisitions_work, manager); |
| 1256 |
s_aws_http_connection_manager_build_transaction(&pending_acquisitions_work); |
| 1257 |
} |
| 1258 |
aws_mutex_unlock(&manager->lock); |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
s_aws_http_connection_manager_complete_acquisitions(&work->completions, work->allocator); |
| 1265 |
aws_array_list_clean_up(&errors); |
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
s_aws_connection_management_transaction_clean_up(work); |
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
if (has_pending_acquisitions) { |
| 1277 |
s_aws_http_connection_manager_execute_transaction(&pending_acquisitions_work); |
| 1278 |
} |
| 1279 |
} |
| 1280 |
|
| 1281 |
void aws_http_connection_manager_acquire_connection( |
| 1282 |
struct aws_http_connection_manager *manager, |
| 1283 |
aws_http_connection_manager_on_connection_setup_fn *callback, |
| 1284 |
void *user_data) { |
| 1285 |
|
| 1286 |
AWS_LOGF_DEBUG(AWS_LS_HTTP_CONNECTION_MANAGER, "id=%p: Acquire connection", (void *)manager); |
| 1287 |
|
| 1288 |
struct aws_http_connection_acquisition *request = |
| 1289 |
aws_mem_calloc(manager->allocator, 1, sizeof(struct aws_http_connection_acquisition)); |
| 1290 |
|
| 1291 |
request->allocator = manager->allocator; |
| 1292 |
request->callback = callback; |
| 1293 |
request->user_data = user_data; |
| 1294 |
request->manager = manager; |
| 1295 |
|
| 1296 |
if (manager->connection_acquisition_timeout_ms) { |
| 1297 |
uint64_t acquire_start_timestamp = 0; |
| 1298 |
if (manager->system_vtable->aws_high_res_clock_get_ticks(&acquire_start_timestamp) == AWS_OP_SUCCESS) { |
| 1299 |
request->timeout_timestamp = |
| 1300 |
acquire_start_timestamp + |
| 1301 |
aws_timestamp_convert( |
| 1302 |
manager->connection_acquisition_timeout_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); |
| 1303 |
} else { |
| 1304 |
AWS_LOGF_WARN( |
| 1305 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1306 |
"id=%p: Failed to get current timestamp using aws_high_res_clock_get_ticks function. Ignoring the " |
| 1307 |
"connection_acquisition_timeout_ms value. ", |
| 1308 |
(void *)manager); |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
struct aws_connection_management_transaction work; |
| 1313 |
s_aws_connection_management_transaction_init(&work, manager); |
| 1314 |
|
| 1315 |
aws_mutex_lock(&manager->lock); |
| 1316 |
|
| 1317 |
|
| 1318 |
AWS_FATAL_ASSERT(manager->state == AWS_HCMST_READY); |
| 1319 |
|
| 1320 |
if (manager->max_pending_connection_acquisitions == 0 || |
| 1321 |
manager->pending_acquisition_count + manager->internal_ref[AWS_HCMCT_VENDED_CONNECTION] < |
| 1322 |
manager->max_pending_connection_acquisitions + manager->max_connections) { |
| 1323 |
aws_linked_list_push_back(&manager->pending_acquisitions, &request->node); |
| 1324 |
++manager->pending_acquisition_count; |
| 1325 |
} else { |
| 1326 |
request->error_code = AWS_ERROR_HTTP_CONNECTION_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED; |
| 1327 |
aws_linked_list_push_back(&work.completions, &request->node); |
| 1328 |
} |
| 1329 |
|
| 1330 |
s_aws_http_connection_manager_build_transaction(&work); |
| 1331 |
|
| 1332 |
aws_mutex_unlock(&manager->lock); |
| 1333 |
|
| 1334 |
s_aws_http_connection_manager_execute_transaction(&work); |
| 1335 |
} |
| 1336 |
|
| 1337 |
|
| 1338 |
static int s_idle_connection(struct aws_http_connection_manager *manager, struct aws_http_connection *connection) { |
| 1339 |
struct aws_idle_connection *idle_connection = |
| 1340 |
aws_mem_calloc(manager->allocator, 1, sizeof(struct aws_idle_connection)); |
| 1341 |
|
| 1342 |
idle_connection->allocator = manager->allocator; |
| 1343 |
idle_connection->connection = connection; |
| 1344 |
|
| 1345 |
uint64_t idle_start_timestamp = 0; |
| 1346 |
if (manager->system_vtable->aws_high_res_clock_get_ticks(&idle_start_timestamp)) { |
| 1347 |
goto on_error; |
| 1348 |
} |
| 1349 |
|
| 1350 |
idle_connection->cull_timestamp = |
| 1351 |
idle_start_timestamp + |
| 1352 |
aws_timestamp_convert( |
| 1353 |
manager->max_connection_idle_in_milliseconds, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); |
| 1354 |
|
| 1355 |
aws_linked_list_push_back(&manager->idle_connections, &idle_connection->node); |
| 1356 |
++manager->idle_connection_count; |
| 1357 |
|
| 1358 |
return AWS_OP_SUCCESS; |
| 1359 |
|
| 1360 |
on_error: |
| 1361 |
|
| 1362 |
aws_mem_release(idle_connection->allocator, idle_connection); |
| 1363 |
|
| 1364 |
return AWS_OP_ERR; |
| 1365 |
} |
| 1366 |
|
| 1367 |
int aws_http_connection_manager_release_connection( |
| 1368 |
struct aws_http_connection_manager *manager, |
| 1369 |
struct aws_http_connection *connection) { |
| 1370 |
|
| 1371 |
struct aws_connection_management_transaction work; |
| 1372 |
s_aws_connection_management_transaction_init(&work, manager); |
| 1373 |
|
| 1374 |
int result = AWS_OP_ERR; |
| 1375 |
bool should_release_connection = !manager->system_vtable->aws_http_connection_new_requests_allowed(connection); |
| 1376 |
|
| 1377 |
AWS_LOGF_DEBUG( |
| 1378 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1379 |
"id=%p: User releasing connection (id=%p)", |
| 1380 |
(void *)manager, |
| 1381 |
(void *)connection); |
| 1382 |
|
| 1383 |
aws_mutex_lock(&manager->lock); |
| 1384 |
|
| 1385 |
|
| 1386 |
if (manager->internal_ref[AWS_HCMCT_VENDED_CONNECTION] == 0) { |
| 1387 |
AWS_LOGF_FATAL( |
| 1388 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1389 |
"id=%p: Connection released when vended connection count is zero", |
| 1390 |
(void *)manager); |
| 1391 |
aws_raise_error(AWS_ERROR_HTTP_CONNECTION_MANAGER_VENDED_CONNECTION_UNDERFLOW); |
| 1392 |
goto release; |
| 1393 |
} |
| 1394 |
|
| 1395 |
result = AWS_OP_SUCCESS; |
| 1396 |
|
| 1397 |
s_connection_manager_internal_ref_decrease(manager, AWS_HCMCT_VENDED_CONNECTION, 1); |
| 1398 |
|
| 1399 |
if (!should_release_connection) { |
| 1400 |
if (s_idle_connection(manager, connection)) { |
| 1401 |
should_release_connection = true; |
| 1402 |
} |
| 1403 |
} |
| 1404 |
|
| 1405 |
s_aws_http_connection_manager_build_transaction(&work); |
| 1406 |
if (should_release_connection) { |
| 1407 |
work.connection_to_release = connection; |
| 1408 |
} |
| 1409 |
|
| 1410 |
release: |
| 1411 |
|
| 1412 |
aws_mutex_unlock(&manager->lock); |
| 1413 |
|
| 1414 |
s_aws_http_connection_manager_execute_transaction(&work); |
| 1415 |
|
| 1416 |
return result; |
| 1417 |
} |
| 1418 |
|
| 1419 |
static void s_aws_http_connection_manager_h2_on_goaway_received( |
| 1420 |
struct aws_http_connection *http2_connection, |
| 1421 |
uint32_t last_stream_id, |
| 1422 |
uint32_t http2_error_code, |
| 1423 |
struct aws_byte_cursor debug_data, |
| 1424 |
void *user_data) { |
| 1425 |
struct aws_http_connection_manager *manager = user_data; |
| 1426 |
|
| 1427 |
AWS_LOGF_DEBUG( |
| 1428 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1429 |
"id=%p: HTTP/2 connection (id=%p) received GOAWAY with: last stream id - %u, error code - %u, debug data - " |
| 1430 |
"\"%.*s\"", |
| 1431 |
(void *)manager, |
| 1432 |
(void *)http2_connection, |
| 1433 |
last_stream_id, |
| 1434 |
http2_error_code, |
| 1435 |
(int)debug_data.len, |
| 1436 |
debug_data.ptr); |
| 1437 |
|
| 1438 |
struct aws_connection_management_transaction work; |
| 1439 |
s_aws_connection_management_transaction_init(&work, manager); |
| 1440 |
|
| 1441 |
aws_mutex_lock(&manager->lock); |
| 1442 |
|
| 1443 |
|
| 1444 |
const struct aws_linked_list_node *end = aws_linked_list_end(&manager->idle_connections); |
| 1445 |
for (struct aws_linked_list_node *node = aws_linked_list_begin(&manager->idle_connections); node != end; |
| 1446 |
node = aws_linked_list_next(node)) { |
| 1447 |
struct aws_idle_connection *current_idle_connection = AWS_CONTAINER_OF(node, struct aws_idle_connection, node); |
| 1448 |
if (current_idle_connection->connection == http2_connection) { |
| 1449 |
aws_linked_list_remove(node); |
| 1450 |
work.connection_to_release = http2_connection; |
| 1451 |
aws_mem_release(current_idle_connection->allocator, current_idle_connection); |
| 1452 |
--manager->idle_connection_count; |
| 1453 |
break; |
| 1454 |
} |
| 1455 |
} |
| 1456 |
s_aws_http_connection_manager_build_transaction(&work); |
| 1457 |
|
| 1458 |
aws_mutex_unlock(&manager->lock); |
| 1459 |
|
| 1460 |
s_aws_http_connection_manager_execute_transaction(&work); |
| 1461 |
} |
| 1462 |
|
| 1463 |
|
| 1464 |
static void s_cm_on_connection_ready_or_failed( |
| 1465 |
struct aws_http_connection_manager *manager, |
| 1466 |
int error_code, |
| 1467 |
struct aws_http_connection *connection, |
| 1468 |
struct aws_connection_management_transaction *work) { |
| 1469 |
|
| 1470 |
bool is_shutting_down = manager->state == AWS_HCMST_SHUTTING_DOWN; |
| 1471 |
|
| 1472 |
if (!error_code) { |
| 1473 |
if (is_shutting_down || s_idle_connection(manager, connection)) { |
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
AWS_LOGF_DEBUG( |
| 1478 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1479 |
"id=%p: New connection (id=%p) releasing immediately", |
| 1480 |
(void *)manager, |
| 1481 |
(void *)connection); |
| 1482 |
work->connection_to_release = connection; |
| 1483 |
} |
| 1484 |
} else { |
| 1485 |
if (manager->pending_acquisition_count > 0) { |
| 1486 |
|
| 1487 |
AWS_LOGF_DEBUG( |
| 1488 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1489 |
"id=%p: Failing connection acquisition with error code %d", |
| 1490 |
(void *)manager, |
| 1491 |
(int)error_code); |
| 1492 |
s_aws_http_connection_manager_move_front_acquisition(manager, NULL, error_code, &work->completions); |
| 1493 |
} |
| 1494 |
|
| 1495 |
if (connection) { |
| 1496 |
work->connection_to_release = connection; |
| 1497 |
} |
| 1498 |
} |
| 1499 |
} |
| 1500 |
|
| 1501 |
static void s_aws_http_connection_manager_h2_on_initial_settings_completed( |
| 1502 |
struct aws_http_connection *http2_connection, |
| 1503 |
int error_code, |
| 1504 |
void *user_data) { |
| 1505 |
struct aws_http_connection_manager *manager = user_data; |
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
struct aws_connection_management_transaction work; |
| 1510 |
s_aws_connection_management_transaction_init(&work, manager); |
| 1511 |
|
| 1512 |
AWS_LOGF_DEBUG( |
| 1513 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1514 |
"id=%p: HTTP/2 connection (id=%p) completed initial settings", |
| 1515 |
(void *)manager, |
| 1516 |
(void *)http2_connection); |
| 1517 |
|
| 1518 |
aws_mutex_lock(&manager->lock); |
| 1519 |
|
| 1520 |
AWS_FATAL_ASSERT(manager->pending_settings_count > 0); |
| 1521 |
--manager->pending_settings_count; |
| 1522 |
s_cm_on_connection_ready_or_failed(manager, error_code, http2_connection, &work); |
| 1523 |
|
| 1524 |
s_aws_http_connection_manager_build_transaction(&work); |
| 1525 |
|
| 1526 |
aws_mutex_unlock(&manager->lock); |
| 1527 |
|
| 1528 |
s_aws_http_connection_manager_execute_transaction(&work); |
| 1529 |
} |
| 1530 |
|
| 1531 |
static void s_aws_http_connection_manager_on_connection_setup( |
| 1532 |
struct aws_http_connection *connection, |
| 1533 |
int error_code, |
| 1534 |
void *user_data) { |
| 1535 |
struct aws_http_connection_manager *manager = user_data; |
| 1536 |
|
| 1537 |
struct aws_connection_management_transaction work; |
| 1538 |
s_aws_connection_management_transaction_init(&work, manager); |
| 1539 |
|
| 1540 |
if (connection != NULL) { |
| 1541 |
AWS_LOGF_DEBUG( |
| 1542 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1543 |
"id=%p: Received new connection (id=%p) from http layer", |
| 1544 |
(void *)manager, |
| 1545 |
(void *)connection); |
| 1546 |
} else { |
| 1547 |
AWS_LOGF_WARN( |
| 1548 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1549 |
"id=%p: Failed to obtain new connection from http layer, error %d(%s)", |
| 1550 |
(void *)manager, |
| 1551 |
error_code, |
| 1552 |
aws_error_str(error_code)); |
| 1553 |
} |
| 1554 |
|
| 1555 |
aws_mutex_lock(&manager->lock); |
| 1556 |
|
| 1557 |
AWS_FATAL_ASSERT(manager->internal_ref[AWS_HCMCT_PENDING_CONNECTIONS] > 0); |
| 1558 |
s_connection_manager_internal_ref_decrease(manager, AWS_HCMCT_PENDING_CONNECTIONS, 1); |
| 1559 |
if (!error_code) { |
| 1560 |
|
| 1561 |
s_connection_manager_internal_ref_increase(manager, AWS_HCMCT_OPEN_CONNECTION, 1); |
| 1562 |
} |
| 1563 |
|
| 1564 |
if (connection != NULL && |
| 1565 |
manager->system_vtable->aws_http_connection_get_version(connection) == AWS_HTTP_VERSION_2) { |
| 1566 |
|
| 1567 |
|
| 1568 |
++manager->pending_settings_count; |
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
AWS_LOGF_TRACE( |
| 1574 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1575 |
"id=%p: New HTTP/2 connection (id=%p) set up, waiting for initial settings to complete", |
| 1576 |
(void *)manager, |
| 1577 |
(void *)connection); |
| 1578 |
} else { |
| 1579 |
|
| 1580 |
AWS_ASSERT(connection || error_code); |
| 1581 |
s_cm_on_connection_ready_or_failed(manager, error_code, connection, &work); |
| 1582 |
} |
| 1583 |
|
| 1584 |
s_aws_http_connection_manager_build_transaction(&work); |
| 1585 |
|
| 1586 |
aws_mutex_unlock(&manager->lock); |
| 1587 |
|
| 1588 |
s_aws_http_connection_manager_execute_transaction(&work); |
| 1589 |
} |
| 1590 |
|
| 1591 |
static void s_aws_http_connection_manager_on_connection_shutdown( |
| 1592 |
struct aws_http_connection *connection, |
| 1593 |
int error_code, |
| 1594 |
void *user_data) { |
| 1595 |
(void)error_code; |
| 1596 |
|
| 1597 |
struct aws_http_connection_manager *manager = user_data; |
| 1598 |
|
| 1599 |
AWS_LOGF_DEBUG( |
| 1600 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1601 |
"id=%p: shutdown received for connection (id=%p)", |
| 1602 |
(void *)manager, |
| 1603 |
(void *)connection); |
| 1604 |
|
| 1605 |
struct aws_connection_management_transaction work; |
| 1606 |
s_aws_connection_management_transaction_init(&work, manager); |
| 1607 |
|
| 1608 |
aws_mutex_lock(&manager->lock); |
| 1609 |
|
| 1610 |
AWS_FATAL_ASSERT(manager->internal_ref[AWS_HCMCT_OPEN_CONNECTION] > 0); |
| 1611 |
s_connection_manager_internal_ref_decrease(manager, AWS_HCMCT_OPEN_CONNECTION, 1); |
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
const struct aws_linked_list_node *end = aws_linked_list_end(&manager->idle_connections); |
| 1617 |
for (struct aws_linked_list_node *node = aws_linked_list_begin(&manager->idle_connections); node != end; |
| 1618 |
node = aws_linked_list_next(node)) { |
| 1619 |
struct aws_idle_connection *current_idle_connection = AWS_CONTAINER_OF(node, struct aws_idle_connection, node); |
| 1620 |
if (current_idle_connection->connection == connection) { |
| 1621 |
aws_linked_list_remove(node); |
| 1622 |
work.connection_to_release = connection; |
| 1623 |
aws_mem_release(current_idle_connection->allocator, current_idle_connection); |
| 1624 |
--manager->idle_connection_count; |
| 1625 |
break; |
| 1626 |
} |
| 1627 |
} |
| 1628 |
|
| 1629 |
s_aws_http_connection_manager_build_transaction(&work); |
| 1630 |
|
| 1631 |
aws_mutex_unlock(&manager->lock); |
| 1632 |
|
| 1633 |
s_aws_http_connection_manager_execute_transaction(&work); |
| 1634 |
} |
| 1635 |
|
| 1636 |
static void s_cull_task_impl(struct aws_http_connection_manager *manager) { |
| 1637 |
AWS_LOGF_INFO( |
| 1638 |
AWS_LS_HTTP_CONNECTION_MANAGER, "id=%p: culling idle connections and pending acquisitions", (void *)manager); |
| 1639 |
|
| 1640 |
if (manager == NULL) { |
| 1641 |
return; |
| 1642 |
} |
| 1643 |
|
| 1644 |
uint64_t now = 0; |
| 1645 |
if (manager->system_vtable->aws_high_res_clock_get_ticks(&now)) { |
| 1646 |
return; |
| 1647 |
} |
| 1648 |
|
| 1649 |
struct aws_connection_management_transaction work; |
| 1650 |
s_aws_connection_management_transaction_init(&work, manager); |
| 1651 |
|
| 1652 |
aws_mutex_lock(&manager->lock); |
| 1653 |
|
| 1654 |
|
| 1655 |
if (manager->state == AWS_HCMST_READY) { |
| 1656 |
|
| 1657 |
if (manager->max_connection_idle_in_milliseconds != 0) { |
| 1658 |
const struct aws_linked_list_node *idle_connections_end = aws_linked_list_end(&manager->idle_connections); |
| 1659 |
struct aws_linked_list_node *idle_connections_current = aws_linked_list_begin(&manager->idle_connections); |
| 1660 |
while (idle_connections_current != idle_connections_end) { |
| 1661 |
struct aws_linked_list_node *node = idle_connections_current; |
| 1662 |
struct aws_idle_connection *current_idle_connection = |
| 1663 |
AWS_CONTAINER_OF(node, struct aws_idle_connection, node); |
| 1664 |
if (current_idle_connection->cull_timestamp > now) { |
| 1665 |
break; |
| 1666 |
} |
| 1667 |
|
| 1668 |
idle_connections_current = aws_linked_list_next(idle_connections_current); |
| 1669 |
aws_linked_list_remove(node); |
| 1670 |
aws_linked_list_push_back(&work.connections_to_release, node); |
| 1671 |
--manager->idle_connection_count; |
| 1672 |
|
| 1673 |
AWS_LOGF_DEBUG( |
| 1674 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1675 |
"id=%p: culling idle connection (%p)", |
| 1676 |
(void *)manager, |
| 1677 |
(void *)current_idle_connection->connection); |
| 1678 |
} |
| 1679 |
} |
| 1680 |
|
| 1681 |
|
| 1682 |
if (manager->connection_acquisition_timeout_ms != 0) { |
| 1683 |
const struct aws_linked_list_node *pending_acquisitions_end = |
| 1684 |
aws_linked_list_end(&manager->pending_acquisitions); |
| 1685 |
struct aws_linked_list_node *pending_acquisitions_current = |
| 1686 |
aws_linked_list_begin(&manager->pending_acquisitions); |
| 1687 |
while (pending_acquisitions_current != pending_acquisitions_end) { |
| 1688 |
struct aws_linked_list_node *node = pending_acquisitions_current; |
| 1689 |
struct aws_http_connection_acquisition *current_pending_acquire = |
| 1690 |
AWS_CONTAINER_OF(node, struct aws_http_connection_acquisition, node); |
| 1691 |
if (current_pending_acquire->timeout_timestamp > now) { |
| 1692 |
break; |
| 1693 |
} |
| 1694 |
|
| 1695 |
pending_acquisitions_current = aws_linked_list_next(pending_acquisitions_current); |
| 1696 |
s_aws_http_connection_manager_move_front_acquisition( |
| 1697 |
manager, NULL, AWS_ERROR_HTTP_CONNECTION_MANAGER_ACQUISITION_TIMEOUT, &work.completions); |
| 1698 |
AWS_LOGF_DEBUG( |
| 1699 |
AWS_LS_HTTP_CONNECTION_MANAGER, |
| 1700 |
"id=%p: Failing pending acquisition due to timeout", |
| 1701 |
(void *)manager); |
| 1702 |
} |
| 1703 |
} |
| 1704 |
} |
| 1705 |
|
| 1706 |
s_aws_http_connection_manager_get_snapshot(manager, &work.snapshot); |
| 1707 |
|
| 1708 |
aws_mutex_unlock(&manager->lock); |
| 1709 |
|
| 1710 |
s_aws_http_connection_manager_execute_transaction(&work); |
| 1711 |
} |
| 1712 |
|
| 1713 |
static void s_cull_task(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 1714 |
(void)task; |
| 1715 |
if (status != AWS_TASK_STATUS_RUN_READY) { |
| 1716 |
return; |
| 1717 |
} |
| 1718 |
|
| 1719 |
struct aws_http_connection_manager *manager = arg; |
| 1720 |
s_cull_task_impl(manager); |
| 1721 |
|
| 1722 |
s_schedule_culling(manager); |
| 1723 |
} |
| 1724 |
|
| 1725 |
void aws_http_connection_manager_fetch_metrics( |
| 1726 |
const struct aws_http_connection_manager *manager, |
| 1727 |
struct aws_http_manager_metrics *out_metrics) { |
| 1728 |
AWS_PRECONDITION(manager); |
| 1729 |
AWS_PRECONDITION(out_metrics); |
| 1730 |
|
| 1731 |
AWS_FATAL_ASSERT(aws_mutex_lock((struct aws_mutex *)(void *)&manager->lock) == AWS_OP_SUCCESS); |
| 1732 |
out_metrics->available_concurrency = manager->idle_connection_count; |
| 1733 |
out_metrics->pending_concurrency_acquires = manager->pending_acquisition_count; |
| 1734 |
out_metrics->leased_concurrency = manager->internal_ref[AWS_HCMCT_VENDED_CONNECTION]; |
| 1735 |
AWS_FATAL_ASSERT(aws_mutex_unlock((struct aws_mutex *)(void *)&manager->lock) == AWS_OP_SUCCESS); |
| 1736 |
} |