| 1 | #ifndef AWS_HTTP2_STREAM_MANAGER_H |
| 2 | #define AWS_HTTP2_STREAM_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 | AWS_PUSH_SANE_WARNING_LEVEL |
| 12 | |
| 13 | struct aws_http2_stream_manager; |
| 14 | struct aws_client_bootstrap; |
| 15 | struct aws_http_connection; |
| 16 | struct aws_http_connection_manager; |
| 17 | struct aws_socket_options; |
| 18 | struct aws_tls_connection_options; |
| 19 | struct proxy_env_var_settings; |
| 20 | struct aws_http2_setting; |
| 21 | struct aws_http_make_request_options; |
| 22 | struct aws_http_stream; |
| 23 | struct aws_http_manager_metrics; |
| 24 | |
| 25 | /** |
| 26 | * Always invoked asynchronously when the stream was created, successfully or not. |
| 27 | * When stream is NULL, error code will be set to indicate what happened. |
| 28 | * If there is a stream returned, you own the stream completely. |
| 29 | * Invoked on the same thread as other callback of the stream, which will be the thread of the connection, ideally. |
| 30 | * If there is no connection made, the callback will be invoked from a sperate thread. |
| 31 | */ |
| 32 | typedef void( |
| 33 | aws_http2_stream_manager_on_stream_acquired_fn)(struct aws_http_stream *stream, int error_code, void *user_data); |
| 34 | |
| 35 | /** |
| 36 | * Invoked asynchronously when the stream manager has been shutdown completely. |
| 37 | * Never invoked when `aws_http2_stream_manager_new` failed. |
| 38 | */ |
| 39 | typedef void(aws_http2_stream_manager_shutdown_complete_fn)(void *user_data); |
| 40 | |
| 41 | /** |
| 42 | * HTTP/2 stream manager configuration struct. |
| 43 | * |
| 44 | * Contains all of the configuration needed to create an http2 connection as well as |
| 45 | * connection manager under the hood. |
| 46 | */ |
| 47 | struct aws_http2_stream_manager_options { |
| 48 | /** |
| 49 | * basic http connection configuration |
| 50 | */ |
| 51 | struct aws_client_bootstrap *bootstrap; |
| 52 | const struct aws_socket_options *socket_options; |
| 53 | |
| 54 | /** |
| 55 | * Options to create secure (HTTPS) connections. |
| 56 | * For secure connections, the ALPN string must be "h2". |
| 57 | * |
| 58 | * To create cleartext (HTTP) connections, leave this NULL |
| 59 | * and set `http2_prior_knowledge` (RFC-7540 3.4). |
| 60 | */ |
| 61 | const struct aws_tls_connection_options *tls_connection_options; |
| 62 | |
| 63 | /** |
| 64 | * Specify whether you have prior knowledge that cleartext (HTTP) connections are HTTP/2 (RFC-7540 3.4). |
| 65 | * It is illegal to set this true when secure connections are being used. |
| 66 | * Note that upgrading from HTTP/1.1 to HTTP/2 is not supported (RFC-7540 3.2). |
| 67 | */ |
| 68 | bool http2_prior_knowledge; |
| 69 | |
| 70 | struct aws_byte_cursor host; |
| 71 | uint32_t port; |
| 72 | |
| 73 | /** |
| 74 | * Optional. |
| 75 | * HTTP/2 connection configuration. Check `struct aws_http2_connection_options` for details of each config. |
| 76 | * Notes for window control: |
| 77 | * - By default, client will will maintain its flow-control windows such that no back-pressure is applied and data |
| 78 | * arrives as fast as possible. |
| 79 | * - For connection level window control, `conn_manual_window_management` will enable manual control. The |
| 80 | * inital window size is not controllable. |
| 81 | * - For stream level window control, `enable_read_back_pressure` will enable manual control. The initial window |
| 82 | * size needs to be set through `initial_window_size` or `initial_settings_array`. |
| 83 | */ |
| 84 | const struct aws_http2_setting *initial_settings_array; |
| 85 | size_t num_initial_settings; |
| 86 | size_t max_closed_streams; |
| 87 | bool conn_manual_window_management; |
| 88 | |
| 89 | /** |
| 90 | * HTTP/2 Stream window control. |
| 91 | * If set to true, the read back pressure mechanism will be enabled for streams created. |
| 92 | * The initial window size can be set by `AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE` via `initial_settings_array` |
| 93 | */ |
| 94 | bool enable_read_back_pressure; |
| 95 | /* |
| 96 | * This value will end up being one of the initial settings for the connection, |
| 97 | * `AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE`. |
| 98 | * The corresponding settings from `initial_settings_array` will override this value. |
| 99 | * Notes: |
| 100 | * - the setting value has the limitation of 2^31-1, otherwise the connection will be failed to be established with |
| 101 | * AWS_ERROR_INVALID_ARGUMENT. |
| 102 | * - when this set to 0, the initial window size will be set to 0, when `enable_read_back_pressure` is true. |
| 103 | * */ |
| 104 | size_t initial_window_size; |
| 105 | |
| 106 | /* Connection monitor for the underlying connections made */ |
| 107 | const struct aws_http_connection_monitoring_options *monitoring_options; |
| 108 | |
| 109 | /* Optional. Proxy configuration for underlying http connection */ |
| 110 | const struct aws_http_proxy_options *proxy_options; |
| 111 | const struct proxy_env_var_settings *proxy_ev_settings; |
| 112 | |
| 113 | /** |
| 114 | * Required. |
| 115 | * When the stream manager finishes deleting all the resources, the callback will be invoked. |
| 116 | */ |
| 117 | void *shutdown_complete_user_data; |
| 118 | aws_http2_stream_manager_shutdown_complete_fn *shutdown_complete_callback; |
| 119 | |
| 120 | /** |
| 121 | * Optional. |
| 122 | * When set, connection will be closed if 5xx response received from server. |
| 123 | */ |
| 124 | bool close_connection_on_server_error; |
| 125 | /** |
| 126 | * Optional. |
| 127 | * The period for all the connections held by stream manager to send a PING in milliseconds. |
| 128 | * If you specify 0, manager will NOT send any PING. |
| 129 | * Note: if set, it must be large than the time of ping timeout setting. |
| 130 | */ |
| 131 | size_t connection_ping_period_ms; |
| 132 | /** |
| 133 | * Optional. |
| 134 | * Network connection will be closed if a ping response is not received |
| 135 | * within this amount of time (milliseconds). |
| 136 | * If you specify 0, a default value will be used. |
| 137 | */ |
| 138 | size_t connection_ping_timeout_ms; |
| 139 | |
| 140 | /* TODO: More flexible policy about the connections, but will always has these three values below. */ |
| 141 | /** |
| 142 | * Optional. |
| 143 | * 0 will be considered as using a default value. |
| 144 | * The ideal number of concurrent streams for a connection. Stream manager will try to create a new connection if |
| 145 | * one connection reaches this number. But, if the max connections reaches, manager will reuse connections to create |
| 146 | * the acquired steams as much as possible. */ |
| 147 | size_t ideal_concurrent_streams_per_connection; |
| 148 | /** |
| 149 | * Optional. |
| 150 | * Default is no limit, which will use the limit from the server. 0 will be considered as using the default value. |
| 151 | * The real number of concurrent streams per connection will be controlled by the minmal value of the setting from |
| 152 | * other end and the value here. |
| 153 | */ |
| 154 | size_t max_concurrent_streams_per_connection; |
| 155 | /** |
| 156 | * Required. |
| 157 | * The max number of connections that will be open at the same time. If all the connections are full, the manager |
| 158 | * will wait until a connection is available to vend more streams. |
| 159 | */ |
| 160 | size_t max_connections; |
| 161 | /** |
| 162 | * Optional. |
| 163 | * The max number of concurrent streams that can be active across all connections at the same time. |
| 164 | * 0 means no limit (default). When this limit is reached, the stream manager will wait for |
| 165 | * existing streams to complete before creating new ones, even if connections have available capacity. |
| 166 | */ |
| 167 | size_t max_concurrent_streams; |
| 168 | }; |
| 169 | |
| 170 | struct aws_http2_stream_manager_acquire_stream_options { |
| 171 | /** |
| 172 | * Required. |
| 173 | * Invoked when the stream finishes acquiring by stream manager. |
| 174 | */ |
| 175 | aws_http2_stream_manager_on_stream_acquired_fn *callback; |
| 176 | /** |
| 177 | * Optional. |
| 178 | * User data for the callback. |
| 179 | */ |
| 180 | void *user_data; |
| 181 | /* Required. see `aws_http_make_request_options` */ |
| 182 | const struct aws_http_make_request_options *options; |
| 183 | }; |
| 184 | |
| 185 | AWS_EXTERN_C_BEGIN |
| 186 | |
| 187 | /** |
| 188 | * Acquire a refcount from the stream manager, stream manager will start to destroy after the refcount drops to zero. |
| 189 | * NULL is acceptable. Initial refcount after new is 1. |
| 190 | * |
| 191 | * @param manager |
| 192 | * @return The same pointer acquiring. |
| 193 | */ |
| 194 | AWS_HTTP_API |
| 195 | struct aws_http2_stream_manager *aws_http2_stream_manager_acquire(struct aws_http2_stream_manager *manager); |
| 196 | |
| 197 | /** |
| 198 | * Release a refcount from the stream manager, stream manager will start to destroy after the refcount drops to zero. |
| 199 | * NULL is acceptable. Initial refcount after new is 1. |
| 200 | * |
| 201 | * @param manager |
| 202 | * @return NULL |
| 203 | */ |
| 204 | AWS_HTTP_API |
| 205 | struct aws_http2_stream_manager *aws_http2_stream_manager_release(struct aws_http2_stream_manager *manager); |
| 206 | |
| 207 | AWS_HTTP_API |
| 208 | struct aws_http2_stream_manager *aws_http2_stream_manager_new( |
| 209 | struct aws_allocator *allocator, |
| 210 | const struct aws_http2_stream_manager_options *options); |
| 211 | |
| 212 | /** |
| 213 | * Acquire a stream from stream manager asynchronously. |
| 214 | * |
| 215 | * @param http2_stream_manager |
| 216 | * @param acquire_stream_option see `aws_http2_stream_manager_acquire_stream_options` |
| 217 | */ |
| 218 | AWS_HTTP_API |
| 219 | void aws_http2_stream_manager_acquire_stream( |
| 220 | struct aws_http2_stream_manager *http2_stream_manager, |
| 221 | const struct aws_http2_stream_manager_acquire_stream_options *acquire_stream_option); |
| 222 | |
| 223 | /** |
| 224 | * Fetch the current metrics from stream manager. |
| 225 | * |
| 226 | * @param http2_stream_manager |
| 227 | * @param out_metrics The metrics to be fetched |
| 228 | */ |
| 229 | AWS_HTTP_API |
| 230 | void aws_http2_stream_manager_fetch_metrics( |
| 231 | const struct aws_http2_stream_manager *http2_stream_manager, |
| 232 | struct aws_http_manager_metrics *out_metrics); |
| 233 | |
| 234 | AWS_EXTERN_C_END |
| 235 | AWS_POP_SANE_WARNING_LEVEL |
| 236 | |
| 237 | #endif /* AWS_HTTP2_STREAM_MANAGER_H */ |