Debian Codegraph @

crt/aws-c-http/source/connection_manager.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
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) /* function pointer to dll symbol */
30 #endif
31
32 /*
33 * Established connections not currently in use are tracked via this structure.
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 * System vtable to use under normal circumstances
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 * AWS_HCMCT_VENDED_CONNECTION: The number of connections currently being used by external users.
68 * AWS_HCMCT_PENDING_CONNECTIONS: The number of pending new connection requests we have outstanding to the http
69 * layer.
70 * AWS_HCMCT_OPEN_CONNECTION: Always equal to # of connection shutdown callbacks not yet invoked
71 * or equivalently:
72 *
73 * # of connections ever created by the manager - # shutdown callbacks received
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 * Vocabulary
84 * Acquisition - a request by a user for a connection
85 * Pending Acquisition - a request by a user for a new connection that has not been completed. It may be
86 * waiting on http, a release by another user, or the manager itself.
87 * Pending Connect - a request to the http layer for a new connection that has not been resolved yet
88 * Vended Connection - a successfully established connection that is currently in use by something; must
89 * be released (through the connection manager) by the user before anyone else can use it. The connection
90 * manager does not explicitly track vended connections.
91 * Task Set - A set of operations that should be attempted once the lock is released. A task set includes
92 * completion callbacks (which can't fail) and connection attempts (which can fail either immediately or
93 * asynchronously).
94 *
95 * Requirements/Assumptions
96 * (1) Don't invoke user callbacks while holding the internal state lock
97 * (2) Don't invoke downstream http calls while holding the internal state lock
98 * (3) Only log unusual or rare events while the lock is held. Common-path logging should be while it is
99 * not held.
100 * (4) Don't crash or do awful things (leaking resources is ok though) if the interface contract
101 * (ref counting + balanced acquire/release of connections) is violated by the user
102 *
103 * In order to fulfill (1) and (2), all side-effecting operations within the connection manager follow a pattern:
104 *
105 * (1) Lock
106 * (2) Make state changes based on the operation
107 * (3) Build a set of work (completions, connect calls, releases, self-destruction) as appropriate to the operation
108 * (4) Unlock
109 * (5) Execute the task set
110 *
111 * Asynchronous work order failures are handled in the async callback, but immediate failures require
112 * us to relock and update the internal state. When there's an immediate connect failure, we use a
113 * conservative policy to fail all excess (beyond the # of pending connects) acquisitions; this allows us
114 * to avoid a possible recursive invocation (and potential failures) to connect again.
115 *
116 * Lifecycle
117 * Our connection manager implementation has a reasonably complex lifecycle.
118 *
119 * All state around the life cycle is protected by a lock. It seemed too risky and error-prone
120 * to try and mix an atomic ref count with the internal tracking counters we need.
121 *
122 * Over the course of its lifetime, a connection manager moves through two states:
123 *
124 * READY - connections may be acquired and released. When the external ref count for the manager
125 * drops to zero, the manager moves to:
126 *
127 * TODO: Seems like connections can still be release while shutting down.
128 * SHUTTING_DOWN - connections may no longer be acquired and released (how could they if the external
129 * ref count was accurate?) but in case of user ref errors, we simply fail attempts to do so rather
130 * than crash or underflow. While in this state, we wait for a set of tracking counters to all fall to zero:
131 *
132 * pending_connect_count - the # of unresolved calls to the http layer's connect logic
133 * open_connection_count - the # of connections for whom the shutdown callback (from http) has not been invoked
134 * vended_connection_count - the # of connections held by external users that haven't been released. Under correct
135 * usage this should be zero before SHUTTING_DOWN is entered, but we attempt to handle incorrect usage gracefully.
136 *
137 * While all the counter fall to zero and no outlife transition, connection manager will detroy itself.
138 *
139 * While shutting down, as pending connects resolve, we immediately release new incoming (from http) connections
140 *
141 * During the transition from READY to SHUTTING_DOWN, we flush the pending acquisition queue (with failure callbacks)
142 * and since we disallow new acquires, pending_acquisition_count should always be zero after the transition.
143 *
144 */
145 struct aws_http_connection_manager {
146 struct aws_allocator *allocator;
147
148 /*
149 * A union of external downstream dependencies (primarily global http API functions) and
150 * internal implementation references. Selectively overridden by tests in order to
151 * enable strong coverage of internal implementation details.
152 */
153 const struct aws_http_connection_manager_system_vtable *system_vtable;
154
155 /*
156 * Callback to invoke when shutdown has completed and all resources have been cleaned up.
157 */
158 aws_http_connection_manager_shutdown_complete_fn *shutdown_complete_callback;
159
160 /*
161 * User data to pass to the shutdown completion callback.
162 */
163 void *shutdown_complete_user_data;
164
165 /*
166 * Controls access to all mutable state on the connection manager
167 */
168 struct aws_mutex lock;
169
170 /*
171 * A manager can be in one of two states, READY or SHUTTING_DOWN. The state transition
172 * takes place when ref_count drops to zero.
173 */
174 enum aws_http_connection_manager_state_type state;
175
176 /*
177 * The number of all established, idle connections. So
178 * that we don't have compute the size of a linked list every time.
179 * It doesn't contribute to internal refcount as AWS_HCMCT_OPEN_CONNECTION includes all idle connections as well.
180 */
181 size_t idle_connection_count;
182
183 /*
184 * The set of all available, ready-to-be-used connections, as aws_idle_connection structs.
185 *
186 * This must be a LIFO stack. When connections are released by the user, they must be added on to the back.
187 * When we vend connections to the user, they must be removed from the back first.
188 * In this way, the list will always be sorted from oldest (in terms of time spent idle) to newest. This means
189 * we can always use the cull timestamp of the front connection as the next scheduled time for culling.
190 * It also means that when we cull connections, we can quit the loop as soon as we find a connection
191 * whose timestamp is greater than the current timestamp.
192 */
193 struct aws_linked_list idle_connections;
194
195 /*
196 * The set of all incomplete connection acquisition requests.
197 * This must be a FIFO list. When connections are requested by the user, they are added to the back. When we need to
198 * complete the acquisition, we pop from the front. In this way, the list is always sorted from the oldest (in terms
199 * of timeout timestamp) to the newest and we can cull it similar to idle_connections.
200 */
201 struct aws_linked_list pending_acquisitions;
202
203 /*
204 * The number of all incomplete connection acquisition requests. So
205 * that we don't have compute the size of a linked list every time.
206 */
207 size_t pending_acquisition_count;
208
209 /*
210 * Counts that contributes to the internal refcount.
211 * When the value changes, s_connection_manager_internal_ref_increase/decrease needed.
212 *
213 * AWS_HCMCT_VENDED_CONNECTION: The number of connections currently being used by external users.
214 * AWS_HCMCT_PENDING_CONNECTIONS: The number of pending new connection requests we have outstanding to the http
215 * layer.
216 * AWS_HCMCT_OPEN_CONNECTION: Always equal to # of connection shutdown callbacks not yet invoked
217 * or equivalently:
218 *
219 * # of connections ever created by the manager - # shutdown callbacks received
220 */
221 size_t internal_ref[AWS_HCMCT_COUNT];
222
223 /*
224 * The number of established new HTTP/2 connections we have waiting for SETTINGS from the http layer
225 * It doesn't contribute to internal refcount as AWS_HCMCT_OPEN_CONNECTION inclues all connections waiting for
226 * settings as well.
227 */
228 size_t pending_settings_count;
229
230 /*
231 * All the options needed to create an http connection
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 * HTTP/2 specific.
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 * The maximum number of connections this manager should ever have at once.
255 */
256 size_t max_connections;
257
258 /*
259 * Lifecycle tracking for the connection manager. Starts at 1.
260 *
261 * Once this drops to zero, the manager state transitions to shutting down
262 *
263 * The manager is deleted when all other tracking counters have returned to zero.
264 *
265 * We don't use an atomic here because the shutdown phase wants to check many different
266 * values. You could argue that we could use a sum of everything, but we still need the
267 * individual values for proper behavior and error checking during the ready state. Also,
268 * a hybrid atomic/lock solution felt excessively complicated and delicate.
269 */
270 size_t external_ref_count;
271
272 /*
273 * Internal refcount that keeps connection manager alive.
274 *
275 * It's a sum of all internal_ref, the `struct aws_connection_management_transaction` alive and one for any external
276 * usage.
277 *
278 * Once this refcount drops to zero, connection manager should either be cleaned up all the memory all waiting for
279 * the last task to clean un the memory and do nothing else.
280 */
281 struct aws_ref_count internal_ref_count;
282
283 /*
284 * if set to true, read back pressure mechanism will be enabled.
285 */
286 bool enable_read_back_pressure;
287
288 /**
289 * If set to a non-zero value, then connections that stay in the pool longer than the specified
290 * timeout will be closed automatically.
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 * Task to cull idle connections. This task is run periodically on the cull_event_loop if a non-zero
300 * culling time interval is specified.
301 */
302 struct aws_task *cull_task;
303 struct aws_event_loop *cull_event_loop;
304
305 /*
306 * An aws_array_list<struct aws_string *> of network interface names to distribute the connections using the
307 * round-robin algorithm. We picked round-robin because it is trivial to implement and good enough. We can later
308 * update to a more complex distribution algorithm if required.
309 */
310 struct aws_array_list network_interface_names;
311 /*
312 * Current index in the network_interface_names array_list.
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 /* From internal_ref */
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 * Correct usage requires AWS_ZERO_STRUCT to have been called beforehand.
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 * A struct that functions as both the pending acquisition tracker and the about-to-complete data.
385 *
386 * The list in the connection manager (pending_acquisitions) is the set of all acquisition requests that we
387 * haven't yet resolved.
388 *
389 * In order to make sure we never invoke callbacks while holding the manager's lock, in a number of places
390 * we build a list of one or more acquisitions to complete. Once the lock is released
391 * we complete all the acquisitions in the list using the data within the struct (hence why we have
392 * "result-oriented" members like connection and error_code). This means we can fail an acquisition
393 * simply by setting the error_code and moving it to the current transaction's completion list.
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; /* Only used by logging */
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 /* this is a channel task. If it is canceled, that means the channel shutdown. In that case, that's equivalent
416 * to a closed connection. */
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 /* release it back to prevent a leak of the connection count. */
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 * Invokes a set of connection acquisition completion callbacks.
440 *
441 * Soft Requirement: The manager's lock must not be held in the callstack.
442 *
443 * Assumes that internal state (like pending_acquisition_count, vended_connection_count, etc...) have already been
444 * updated according to the list's contents.
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 /* For some workloads, going ahead and moving the connection callback to the connection's thread is a
462 * substantial performance improvement so let's do that */
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 * Moves the first pending connection acquisition into a (task set) list. Call this while holding the lock to
495 * build the set of callbacks to be completed once the lock is released.
496 *
497 * Hard Requirement: Manager's lock must held somewhere in the call stack
498 *
499 * If this was a successful acquisition then connection is non-null
500 * If this was a failed acquisition then connection is null and error_code is hopefully a useful diagnostic (extreme
501 * edge cases exist where it may not be though)
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 * Encompasses all of the external operations that need to be done for various
533 * events:
534 * manager release
535 * connection release
536 * connection acquire
537 * connection_setup
538 * connection_shutdown
539 *
540 * The transaction is built under the manager's lock (and the internal state is updated optimistically),
541 * but then executed outside of it.
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; /* <struct aws_idle_connection> */
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 /* The count acquire and release all needs to be invoked helding the lock */
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 /* This only happens between transcation init and transcation clean up. As transcation always has a internal
592 * refcount, this will never bring the refcount to zero */
593 aws_ref_count_release(&manager->internal_ref_count);
594 }
595 }
596
597 /* Only invoked with the lock held */
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 * Step 1 - If there's free connections, complete acquisition requests
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 * It is absolutely critical that this is pop_back and not front. By making the idle connections
609 * a LIFO stack, the list will always be sorted from oldest (in terms of idle time) to newest. This means
610 * we can always use the cull timestamp of the first connection as the next scheduled time for culling.
611 * It also means that when we cull connections, we can quit the loop as soon as we find a connection
612 * whose timestamp is greater than the current timestamp.
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 * Step 2 - if there's excess pending acquisitions and we have room to make more, make more
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 * swap our internal connection set with the empty work set
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 * Move all manager pending acquisitions to the work completion list
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 * The final last gasp of a connection manager where memory is cleaned up. Destruction is split up into two parts,
688 * a begin and a finish. Idle connection culling requires a scheduled task on an arbitrary event loop. If idle
689 * connection culling is on then this task must be cancelled before destruction can finish, but you can only cancel
690 * a task from the same event loop that it is scheduled on. To resolve this, when using idle connection culling,
691 * we schedule a finish destruction task on the event loop that the culling task is on. This finish task
692 * cancels the culling task and then calls this function. If we are not using idle connection culling, we can
693 * call this function immediately from the start of destruction.
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 * If this task exists then we are actually in the corresponding event loop running the final destruction task.
737 * In that case, we've already cancelled this task and when you cancel, it runs synchronously. So in that
738 * case the task has run as cancelled, it was not rescheduled, and so we can safely release the memory.
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 /* This is scheduled to run on the cull task's event loop. Should only be scheduled to run if we have one */
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 /* release the refcount on manager as the culling task will not run again */
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 * Calculates the next timestamp the idle connections should be culled. Manager lock must be held somewhere in the call
775 * stack. Returns UINT64_MAX if max_connection_idle_in_milliseconds is not set.
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 * Since the connections are in LIFO order in the list, the front of the list has the closest
788 * cull time.
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 * There are no connections in the list, so the absolute minimum anything could be culled is the full
796 * culling interval from now.
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 * Calculates the next timestamp the pending acquisitions should be culled. Manager lock must be held somewhere in the
810 * call stack. Returns UINT64_MAX if connection_acquisition_timeout_ms is not set.
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 * front of the list has the closest cull time
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 * There are no acquisition in the list, so the absolute minimum anything could be culled is the full
831 * culling interval from now.
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 /* For the task to properly run and cancel, we need to keep manager alive */
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 /* Copy the initial_settings_array to the internal structure */
982 memcpy(
983 manager->initial_settings->data,
984 options->initial_settings_array,
985 options->num_initial_settings * sizeof(struct aws_http2_setting));
986 /* Update the length of the array as the memcpy only copies the data, but the length remains 0. */
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 /* NOTHING can fail after here */
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 /* When manager shutting down, schedule the task to cancel the cull task if exist. */
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) /* allow strncpy() */
1111 #endif
1112 /* If the interface_name is too long or not null terminated, it will be caught in the `aws_socket_init` function
1113 * so we don't need to worry about that here.*/
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 /* The initial_settings_completed invoked after the other side acknowledges it, and will always be invoked if the
1136 * connection set up */
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 * Step 1 - Logging
1175 */
1176 s_aws_http_connection_manager_log_snapshot(manager, &work->snapshot);
1177
1178 /*
1179 * Step 2 - Perform any requested connection releases
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 * Step 3 - Make new connections
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 * We failed and aren't going to receive a callback, but the current state assumes we will receive
1233 * a callback. So we need to re-lock and update the state ourselves.
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 /* If there are pending acquisitions, schedule work again to try acquiring them */
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 * Step 4 - Perform acquisition callbacks
1263 */
1264 s_aws_http_connection_manager_complete_acquisitions(&work->completions, work->allocator);
1265 aws_array_list_clean_up(&errors);
1266
1267 /*
1268 * Step 5 - Clean up work. Do this here rather than at the end of every caller. Destroy the manager if necessary
1269 */
1270 s_aws_connection_management_transaction_clean_up(work);
1271
1272 /*
1273 * Step 6 - If some connection acquisition requests failed and we still have more pending acquisitions, try to
1274 * acquire them.
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 /* It's a use after free crime, we don't want to handle */
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 /* Only invoke with lock held */
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 /* We're probably hosed in this case, but let's not underflow */
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 /* We don't offer user the details, but we can still log it out for debugging */
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 /* Goaway received, remove the connection from idle and release it, if it's there. But, not decrease the
1443 * open_connection_count as the shutdown callback will be invoked, we still need the manager to be alive */
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 /* Only invoke with lock held */
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 * release it immediately
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 /* fail acquisition as connection acquire failed */
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 /* Since the connection never being idle, we need to release the connection here. */
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 /* The other side acknowledge about the settings which also means we received the settings from other side at this
1507 * point, because the settings should be the fist frame to be sent */
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 /* Shutdown will not be invoked if setup completed with error */
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 /* If the manager is shutting down, we will still wait for the settings, since we don't have map for connections
1567 */
1568 ++manager->pending_settings_count;
1569 /* For http/2 connection, we vent the connection after the initial settings completed for the user to make
1570 * sure the connection is really ready to use. So, we can revert the counting and act like nothing happens
1571 * here and wait for the on_initial_settings_completed, which will ALWAYS be invoked before shutdown. BUT,
1572 * we increase the open_connection_count, as the shutdown will be invoked no matter what happens. */
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 /* If there is no connection, error code cannot be zero */
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 * Find and, if found, remove it from idle connections
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 /* Only if we're not shutting down */
1655 if (manager->state == AWS_HCMST_READY) {
1656 /* cull idle connections */
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 /* cull pending acquisitions */
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 }