|
| 1 | +#ifndef AWS_HTTP2_STREAM_MANAGER_IMPL_H |
| 2 | +#define AWS_HTTP2_STREAM_MANAGER_IMPL_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/common/mutex.h> |
| 10 | +#include <aws/common/ref_count.h> |
| 11 | +#include <aws/http/http2_stream_manager.h> |
| 12 | +#include <aws/http/private/random_access_set.h> |
| 13 | + |
| 14 | +enum aws_h2_sm_state_type { |
| 15 | + AWS_H2SMST_READY, |
| 16 | + AWS_H2SMST_DESTROYING, /* On zero external ref count, can destroy */ |
| 17 | +}; |
| 18 | + |
| 19 | +enum aws_h2_sm_connection_state_type { |
| 20 | + AWS_H2SMCST_IDEAL, |
| 21 | + AWS_H2SMCST_NEARLY_FULL, |
| 22 | + AWS_H2SMCST_FULL, |
| 23 | +}; |
| 24 | + |
| 25 | +/* Live with the streams opening, and if there no outstanding pending acquisition and no opening streams on the |
| 26 | + * connection, this structure should die */ |
| 27 | +struct aws_h2_sm_connection { |
| 28 | + struct aws_http2_stream_manager *stream_manager; |
| 29 | + struct aws_http_connection *connection; |
| 30 | + uint32_t num_streams_assigned; /* From a stream assigned to the connection until the stream completed |
| 31 | + or failed to be created from the connection. */ |
| 32 | + uint32_t max_concurrent_streams; /* lower bound between user configured and the other side */ |
| 33 | + |
| 34 | + enum aws_h2_sm_connection_state_type state; |
| 35 | +}; |
| 36 | + |
| 37 | +/* Live from the user request to acquire a stream to the stream completed. */ |
| 38 | +struct aws_h2_sm_pending_stream_acquisition { |
| 39 | + struct aws_allocator *allocator; |
| 40 | + struct aws_linked_list_node node; |
| 41 | + struct aws_http_make_request_options options; |
| 42 | + struct aws_h2_sm_connection *sm_connection; /* The connection to make request to. Keep |
| 43 | + NULL, until find available one and move it to the pending_make_requests |
| 44 | + list. */ |
| 45 | + struct aws_http_message *request; |
| 46 | + struct aws_channel_task make_request_task; |
| 47 | + aws_http2_stream_manager_on_stream_acquired_fn *callback; |
| 48 | + void *user_data; |
| 49 | +}; |
| 50 | + |
| 51 | +/* connections_acquiring_count, open_stream_count, pending_make_requests_count AND pending_stream_acquisition_count */ |
| 52 | +enum aws_sm_count_type { |
| 53 | + AWS_SMCT_CONNECTIONS_ACQUIRING, |
| 54 | + AWS_SMCT_OPEN_STREAM, |
| 55 | + AWS_SMCT_PENDING_MAKE_REQUESTS, |
| 56 | + AWS_SMCT_PENDING_ACQUISITION, |
| 57 | + AWS_SMCT_COUNT, |
| 58 | +}; |
| 59 | + |
| 60 | +struct aws_http2_stream_manager { |
| 61 | + struct aws_allocator *allocator; |
| 62 | + void *shutdown_complete_user_data; |
| 63 | + aws_http2_stream_manager_shutdown_complete_fn *shutdown_complete_callback; |
| 64 | + /** |
| 65 | + * Underlying connection manager. Always has the same life time with the stream manager who owns it. |
| 66 | + */ |
| 67 | + struct aws_http_connection_manager *connection_manager; |
| 68 | + /** |
| 69 | + * Refcount managed by user. Once this drops to zero, the manager state transitions to shutting down |
| 70 | + */ |
| 71 | + struct aws_ref_count external_ref_count; |
| 72 | + /** |
| 73 | + * Internal refcount that keeps connection manager alive. |
| 74 | + * |
| 75 | + * It's a sum of connections_acquiring_count, open_stream_count, pending_make_requests_count and |
| 76 | + * pending_stream_acquisition_count, besides the number of `struct aws_http2_stream_management_transaction` alive. |
| 77 | + * And one for external usage. |
| 78 | + * |
| 79 | + * Once this refcount drops to zero, stream manager should either be cleaned up all the memory all waiting for |
| 80 | + * the last task to clean un the memory and do nothing else. |
| 81 | + */ |
| 82 | + struct aws_ref_count internal_ref_count; |
| 83 | + struct aws_client_bootstrap *bootstrap; |
| 84 | + |
| 85 | + size_t max_connections; |
| 86 | + |
| 87 | + /** |
| 88 | + * Default is no limit. 0 will be considered as using the default value. |
| 89 | + * The ideal number of concurrent streams for a connection. Stream manager will try to create a new connection if |
| 90 | + * one connection reaches this number. But, if the max connections reaches, manager will reuse connections to create |
| 91 | + * the acquired steams as much as possible. */ |
| 92 | + size_t ideal_concurrent_streams_per_connection; |
| 93 | + /** |
| 94 | + * Default is no limit. 0 will be considered as using the default value. |
| 95 | + * The real number of concurrent streams per connection will be controlled by the minmal value of the setting from |
| 96 | + * other end and the value here. |
| 97 | + */ |
| 98 | + size_t max_concurrent_streams_per_connection; |
| 99 | + |
| 100 | + /** |
| 101 | + * Task to invoke pending acquisition callbacks asynchronously if stream manager is shutting. |
| 102 | + */ |
| 103 | + struct aws_event_loop *finish_pending_stream_acquisitions_task_event_loop; |
| 104 | + |
| 105 | + /* Any thread may touch this data, but the lock must be held (unless it's an atomic) */ |
| 106 | + struct { |
| 107 | + struct aws_mutex lock; |
| 108 | + /* |
| 109 | + * A manager can be in one of two states, READY or SHUTTING_DOWN. The state transition |
| 110 | + * takes place when ref_count drops to zero. |
| 111 | + */ |
| 112 | + enum aws_h2_sm_state_type state; |
| 113 | + |
| 114 | + /** |
| 115 | + * A set of all connections that meet all requirement to use. Note: there will be connections not in this set, |
| 116 | + * but hold by the stream manager, which can be tracked by the streams created on it. Set of `struct |
| 117 | + * aws_h2_sm_connection *` |
| 118 | + */ |
| 119 | + struct aws_random_access_set ideal_available_set; |
| 120 | + /** |
| 121 | + * A set of all available connections that exceed the soft limits set by users. Note: there will be connections |
| 122 | + * not in this set, but hold by the stream manager, which can be tracked by the streams created. Set of `struct |
| 123 | + * aws_h2_sm_connection *` |
| 124 | + */ |
| 125 | + struct aws_random_access_set nonideal_available_set; |
| 126 | + /* We don't mantain set for connections that is full or "dead" (Cannot make any new streams). We have streams |
| 127 | + * opening from the connection tracking them */ |
| 128 | + |
| 129 | + /** |
| 130 | + * The set of all incomplete stream acquisition requests (haven't decide what connection to make the request |
| 131 | + * to), list of `struct aws_h2_sm_pending_stream_acquisition*` |
| 132 | + */ |
| 133 | + struct aws_linked_list pending_stream_acquisitions; |
| 134 | + |
| 135 | + /** |
| 136 | + * The number of connections acquired from connection manager and not released yet. |
| 137 | + */ |
| 138 | + size_t holding_connections_count; |
| 139 | + |
| 140 | + /** |
| 141 | + * Counts that contributes to the internal refcount. |
| 142 | + * When the value changes, s_sm_count_increase/decrease_synced needed. |
| 143 | + * |
| 144 | + * AWS_SMCT_CONNECTIONS_ACQUIRING: The number of new connections we acquiring from the connection manager. |
| 145 | + * AWS_SMCT_OPEN_STREAM: The number of streams that opened and not completed yet. |
| 146 | + * AWS_SMCT_PENDING_MAKE_REQUESTS: The number of streams that scheduled to be made from a connection but haven't |
| 147 | + * been executed yet. |
| 148 | + * AWS_SMCT_PENDING_ACQUISITION: The number of all incomplete stream acquisition requests (haven't decide what |
| 149 | + * connection to make the request to). So that we don't have compute the size of a linked list every time. |
| 150 | + */ |
| 151 | + size_t internal_refcount_stats[AWS_SMCT_COUNT]; |
| 152 | + |
| 153 | + bool finish_pending_stream_acquisitions_task_scheduled; |
| 154 | + } synced_data; |
| 155 | +}; |
| 156 | + |
| 157 | +/** |
| 158 | + * Encompasses all of the external operations that need to be done for various |
| 159 | + * events: |
| 160 | + * - User level: |
| 161 | + * stream manager release |
| 162 | + * stream acquire |
| 163 | + * - Internal eventloop (anther thread): |
| 164 | + * connection_acquired |
| 165 | + * stream_completed |
| 166 | + * - Internal (can happen from any thread): |
| 167 | + * connection acquire |
| 168 | + * connection release |
| 169 | + * |
| 170 | + * The transaction is built under the manager's lock (and the internal state is updated optimistically), |
| 171 | + * but then executed outside of it. |
| 172 | + */ |
| 173 | +struct aws_http2_stream_management_transaction { |
| 174 | + struct aws_http2_stream_manager *stream_manager; |
| 175 | + struct aws_allocator *allocator; |
| 176 | + size_t new_connections; |
| 177 | + struct aws_h2_sm_connection *sm_connection_to_release; |
| 178 | + struct aws_linked_list |
| 179 | + pending_make_requests; /* List of aws_h2_sm_pending_stream_acquisition with chosen connection */ |
| 180 | +}; |
| 181 | + |
| 182 | +#endif /* AWS_HTTP2_STREAM_MANAGER_IMPL_H */ |
0 commit comments