| 1 | #ifndef AWS_HTTP_CONNECTION_MANAGER_H |
| 2 | #define AWS_HTTP_CONNECTION_MANAGER_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0. |
| 7 | */ |
| 8 | |
| 9 | #include <aws/http/http.h> |
| 10 | |
| 11 | #include <aws/common/byte_buf.h> |
| 12 | |
| 13 | AWS_PUSH_SANE_WARNING_LEVEL |
| 14 | |
| 15 | struct aws_client_bootstrap; |
| 16 | struct aws_http_connection; |
| 17 | struct aws_http_connection_manager; |
| 18 | struct aws_socket_options; |
| 19 | struct aws_tls_connection_options; |
| 20 | struct proxy_env_var_settings; |
| 21 | struct aws_http2_setting; |
| 22 | |
| 23 | typedef void(aws_http_connection_manager_on_connection_setup_fn)( |
| 24 | struct aws_http_connection *connection, |
| 25 | int error_code, |
| 26 | void *user_data); |
| 27 | |
| 28 | typedef void(aws_http_connection_manager_shutdown_complete_fn)(void *user_data); |
| 29 | |
| 30 | /** |
| 31 | * Metrics for logging and debugging purpose. |
| 32 | */ |
| 33 | struct aws_http_manager_metrics { |
| 34 | /** |
| 35 | * The number of additional concurrent requests that can be supported by the HTTP manager without needing to |
| 36 | * establish additional connections to the target server. |
| 37 | * |
| 38 | * For connection manager, it equals to connections that's idle. |
| 39 | * For stream manager, it equals to the number of streams that are possible to be made without creating new |
| 40 | * connection, although the implementation can create new connection without fully filling it. |
| 41 | */ |
| 42 | size_t available_concurrency; |
| 43 | /* The number of requests that are awaiting concurrency to be made available from the HTTP manager. */ |
| 44 | size_t pending_concurrency_acquires; |
| 45 | /* The number of connections (http/1.1) or streams (for h2 via. stream manager) currently vended to user. */ |
| 46 | size_t leased_concurrency; |
| 47 | }; |
| 48 | |
| 49 | /* |
| 50 | * Connection manager configuration struct. |
| 51 | * |
| 52 | * Contains all of the configuration needed to create an http connection as well as |
| 53 | * the maximum number of connections to ever have in existence. |
| 54 | */ |
| 55 | struct aws_http_connection_manager_options { |
| 56 | /* |
| 57 | * http connection configuration, check `struct aws_http_client_connection_options` for details of each config |
| 58 | */ |
| 59 | struct aws_client_bootstrap *bootstrap; |
| 60 | size_t initial_window_size; |
| 61 | const struct aws_socket_options *socket_options; |
| 62 | /** |
| 63 | * Optional (ignored if 0). |
| 64 | * After a request is fully sent, if the server does not begin responding within N milliseconds, |
| 65 | * then fail with AWS_ERROR_HTTP_RESPONSE_FIRST_BYTE_TIMEOUT. |
| 66 | * This can be overridden per-request by aws_http_make_request_options.response_first_byte_timeout_ms. |
| 67 | * TODO: Only supported in HTTP/1.1 now, support it in HTTP/2 |
| 68 | */ |
| 69 | uint64_t response_first_byte_timeout_ms; |
| 70 | |
| 71 | /** |
| 72 | * Options to create secure (HTTPS) connections. |
| 73 | * For secure connections, set "h2" in the ALPN string for HTTP/2, otherwise HTTP/1.1 is used. |
| 74 | * |
| 75 | * Leave NULL to create cleartext (HTTP) connections. |
| 76 | * For cleartext connections, use `http2_prior_knowledge` (RFC-7540 3.4) |
| 77 | * to control whether that are treated as HTTP/1.1 or HTTP/2. |
| 78 | */ |
| 79 | const struct aws_tls_connection_options *tls_connection_options; |
| 80 | |
| 81 | /** |
| 82 | * Specify whether you have prior knowledge that cleartext (HTTP) connections are HTTP/2 (RFC-7540 3.4). |
| 83 | * If false, then cleartext connections are treated as HTTP/1.1. |
| 84 | * It is illegal to set this true when secure connections are being used. |
| 85 | * Note that upgrading from HTTP/1.1 to HTTP/2 is not supported (RFC-7540 3.2). |
| 86 | */ |
| 87 | bool http2_prior_knowledge; |
| 88 | |
| 89 | const struct aws_http_connection_monitoring_options *monitoring_options; |
| 90 | struct aws_byte_cursor host; |
| 91 | uint32_t port; |
| 92 | |
| 93 | /** |
| 94 | * Optional. |
| 95 | * HTTP/2 specific configuration. Check `struct aws_http2_connection_options` for details of each config |
| 96 | */ |
| 97 | const struct aws_http2_setting *initial_settings_array; |
| 98 | size_t num_initial_settings; |
| 99 | size_t max_closed_streams; |
| 100 | bool http2_conn_manual_window_management; |
| 101 | |
| 102 | /* Proxy configuration for http connection */ |
| 103 | const struct aws_http_proxy_options *proxy_options; |
| 104 | |
| 105 | /* |
| 106 | * Optional. |
| 107 | * Configuration for using proxy from environment variable. |
| 108 | * Only works when proxy_options is not set. |
| 109 | */ |
| 110 | const struct proxy_env_var_settings *proxy_ev_settings; |
| 111 | |
| 112 | /* |
| 113 | * Maximum number of connections this manager is allowed to contain |
| 114 | */ |
| 115 | size_t max_connections; |
| 116 | |
| 117 | /* |
| 118 | * Callback and associated user data to invoke when the connection manager has |
| 119 | * completely shutdown and has finished deleting itself. |
| 120 | * Technically optional, but correctness may be impossible without it. |
| 121 | */ |
| 122 | void *shutdown_complete_user_data; |
| 123 | aws_http_connection_manager_shutdown_complete_fn *shutdown_complete_callback; |
| 124 | |
| 125 | /** |
| 126 | * If set to true, the read back pressure mechanism will be enabled. |
| 127 | */ |
| 128 | bool enable_read_back_pressure; |
| 129 | |
| 130 | /** |
| 131 | * If set to a non-zero value, then connections that stay in the pool longer than the specified |
| 132 | * timeout will be closed automatically. |
| 133 | */ |
| 134 | uint64_t max_connection_idle_in_milliseconds; |
| 135 | |
| 136 | /** |
| 137 | * If set to a non-zero value, aws_http_connection_manager_acquire_connection() calls |
| 138 | * will give up after waiting this long for a connection from the pool, |
| 139 | * failing with error AWS_ERROR_HTTP_CONNECTION_MANAGER_ACQUISITION_TIMEOUT. |
| 140 | */ |
| 141 | uint64_t connection_acquisition_timeout_ms; |
| 142 | |
| 143 | /* |
| 144 | * If set to a non-zero value, aws_http_connection_manager_acquire_connection() calls will fail with |
| 145 | * AWS_ERROR_HTTP_CONNECTION_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED if the number of pending acquisitions |
| 146 | * reaches `max_pending_connection_acquisitions` after the connection pool has reached its capacity (i.e., all |
| 147 | * `num_connections` have been vended). |
| 148 | */ |
| 149 | uint64_t max_pending_connection_acquisitions; |
| 150 | |
| 151 | /** |
| 152 | * THIS IS AN EXPERIMENTAL AND UNSTABLE API |
| 153 | * (Optional) |
| 154 | * An array of network interface names. The manager will distribute the |
| 155 | * connections across network interface names provided in this array. If any interface name is invalid, goes down, |
| 156 | * or has any issues like network access, you will see connection failures. If |
| 157 | * `socket_options.network_interface_name` is also set, an `AWS_ERROR_INVALID_ARGUMENT` error will be raised. |
| 158 | * |
| 159 | * This option is only supported on Linux, MacOS, and platforms that have either SO_BINDTODEVICE or IP_BOUND_IF. It |
| 160 | * is not supported on Windows. `AWS_ERROR_PLATFORM_NOT_SUPPORTED` will be raised on unsupported platforms. |
| 161 | */ |
| 162 | const struct aws_byte_cursor *network_interface_names_array; |
| 163 | size_t num_network_interface_names; |
| 164 | }; |
| 165 | |
| 166 | AWS_EXTERN_C_BEGIN |
| 167 | |
| 168 | /* |
| 169 | * Connection managers are ref counted. Adds one external ref to the manager. |
| 170 | */ |
| 171 | AWS_HTTP_API |
| 172 | void aws_http_connection_manager_acquire(struct aws_http_connection_manager *manager); |
| 173 | |
| 174 | /* |
| 175 | * Connection managers are ref counted. Removes one external ref from the manager. |
| 176 | * |
| 177 | * When the ref count goes to zero, the connection manager begins its shut down |
| 178 | * process. All pending connection acquisitions are failed (with callbacks |
| 179 | * invoked) and any (erroneous) subsequent attempts to acquire a connection |
| 180 | * fail immediately. The connection manager destroys itself once all pending |
| 181 | * asynchronous activities have resolved. |
| 182 | */ |
| 183 | AWS_HTTP_API |
| 184 | void aws_http_connection_manager_release(struct aws_http_connection_manager *manager); |
| 185 | |
| 186 | /* |
| 187 | * Creates a new connection manager with the supplied configuration options. |
| 188 | * |
| 189 | * The returned connection manager begins with a ref count of 1. |
| 190 | */ |
| 191 | AWS_HTTP_API |
| 192 | struct aws_http_connection_manager *aws_http_connection_manager_new( |
| 193 | struct aws_allocator *allocator, |
| 194 | const struct aws_http_connection_manager_options *options); |
| 195 | |
| 196 | /* |
| 197 | * Requests a connection from the manager. The requester is notified of |
| 198 | * an acquired connection (or failure to acquire) via the supplied callback. |
| 199 | * |
| 200 | * For HTTP/2 connections, the callback will not fire until the server's settings have been received. |
| 201 | * |
| 202 | * Once a connection has been successfully acquired from the manager it |
| 203 | * must be released back (via aws_http_connection_manager_release_connection) |
| 204 | * at some point. Failure to do so will cause a resource leak. |
| 205 | */ |
| 206 | AWS_HTTP_API |
| 207 | void aws_http_connection_manager_acquire_connection( |
| 208 | struct aws_http_connection_manager *manager, |
| 209 | aws_http_connection_manager_on_connection_setup_fn *callback, |
| 210 | void *user_data); |
| 211 | |
| 212 | /* |
| 213 | * Returns a connection back to the manager. All acquired connections must |
| 214 | * eventually be released back to the manager in order to avoid a resource leak. |
| 215 | * |
| 216 | * Note: it can lead to another acquired callback to be invoked within the thread. |
| 217 | */ |
| 218 | AWS_HTTP_API |
| 219 | int aws_http_connection_manager_release_connection( |
| 220 | struct aws_http_connection_manager *manager, |
| 221 | struct aws_http_connection *connection); |
| 222 | |
| 223 | /** |
| 224 | * Fetch the current manager metrics from connection manager. |
| 225 | */ |
| 226 | AWS_HTTP_API |
| 227 | void aws_http_connection_manager_fetch_metrics( |
| 228 | const struct aws_http_connection_manager *manager, |
| 229 | struct aws_http_manager_metrics *out_metrics); |
| 230 | |
| 231 | AWS_EXTERN_C_END |
| 232 | AWS_POP_SANE_WARNING_LEVEL |
| 233 | |
| 234 | #endif /* AWS_HTTP_CONNECTION_MANAGER_H */ |