Debian Codegraph @

crt/aws-c-http/include/aws/http/private/http2_stream_manager_impl.h

raw symbols SCIP
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_allocator *allocator;
29 struct aws_http2_stream_manager *stream_manager;
30 struct aws_http_connection *connection;
31 uint32_t num_streams_assigned; /* From a stream assigned to the connection until the stream completed
32 or failed to be created from the connection. */
33 uint32_t max_concurrent_streams; /* lower bound between user configured and the other side */
34
35 /* task to send ping periodically from connection thread. */
36 struct aws_ref_count ref_count;
37 struct aws_channel_task ping_task;
38 struct aws_channel_task ping_timeout_task;
39 struct {
40 bool ping_received;
41 bool stopped_new_requests;
42 uint64_t next_ping_task_time;
43 } thread_data;
44
45 enum aws_h2_sm_connection_state_type state;
46
47 /**
48 * Node for tracking in the all_held_connections list,
49 * NOTE: lock required to alter the state of the list.
50 */
51 struct aws_linked_list_node node;
52 };
53
54 /* Live from the user request to acquire a stream to the stream completed. */
55 struct aws_h2_sm_pending_stream_acquisition {
56 struct aws_allocator *allocator;
57 struct aws_linked_list_node node;
58 struct aws_http_make_request_options options;
59 struct aws_h2_sm_connection *sm_connection; /* The connection to make request to. Keep
60 NULL, until find available one and move it to the pending_make_requests
61 list. */
62 struct aws_http_message *request;
63 struct aws_channel_task make_request_task;
64 aws_http2_stream_manager_on_stream_acquired_fn *callback;
65 void *user_data;
66 };
67
68 /* connections_acquiring_count, open_stream_count, pending_make_requests_count AND pending_stream_acquisition_count */
69 enum aws_sm_count_type {
70 AWS_SMCT_CONNECTIONS_ACQUIRING,
71 AWS_SMCT_OPEN_STREAM,
72 AWS_SMCT_PENDING_MAKE_REQUESTS,
73 AWS_SMCT_PENDING_ACQUISITION,
74 AWS_SMCT_COUNT,
75 };
76
77 struct aws_http2_stream_manager {
78 struct aws_allocator *allocator;
79 void *shutdown_complete_user_data;
80 aws_http2_stream_manager_shutdown_complete_fn *shutdown_complete_callback;
81 /**
82 * Underlying connection manager. Always has the same life time with the stream manager who owns it.
83 */
84 struct aws_http_connection_manager *connection_manager;
85 /**
86 * Refcount managed by user. Once this drops to zero, the manager state transitions to shutting down
87 */
88 struct aws_ref_count external_ref_count;
89 /**
90 * Internal refcount that keeps connection manager alive.
91 *
92 * It's a sum of connections_acquiring_count, open_stream_count, pending_make_requests_count and
93 * pending_stream_acquisition_count, besides the number of `struct aws_http2_stream_management_transaction` alive.
94 * And one for external usage.
95 *
96 * Once this refcount drops to zero, stream manager should either be cleaned up all the memory all waiting for
97 * the last task to clean un the memory and do nothing else.
98 */
99 struct aws_ref_count internal_ref_count;
100 struct aws_client_bootstrap *bootstrap;
101
102 /* Configurations */
103 size_t max_connections;
104 /* Connection will be closed if 5xx response received from server. */
105 bool close_connection_on_server_error;
106
107 uint64_t connection_ping_period_ns;
108 uint64_t connection_ping_timeout_ns;
109
110 /**
111 * Default is no limit. 0 will be considered as using the default value.
112 * The ideal number of concurrent streams for a connection. Stream manager will try to create a new connection if
113 * one connection reaches this number. But, if the max connections reaches, manager will reuse connections to create
114 * the acquired steams as much as possible. */
115 size_t ideal_concurrent_streams_per_connection;
116 /**
117 * Default is no limit. 0 will be considered as using the default value.
118 * The real number of concurrent streams per connection will be controlled by the minmal value of the setting from
119 * other end and the value here.
120 */
121 size_t max_concurrent_streams_per_connection;
122
123 /**
124 * Optional. 0 means no limit (default).
125 * The max number of concurrent streams that can be active across all connections at the same time.
126 * When this limit is reached, the stream manager will wait for existing streams to complete
127 * before creating new ones, even if connections have available capacity.
128 */
129 size_t max_concurrent_streams;
130
131 /**
132 * Task to invoke pending acquisition callbacks asynchronously if stream manager is shutting.
133 */
134 struct aws_event_loop *finish_pending_stream_acquisitions_task_event_loop;
135
136 /* Any thread may touch this data, but the lock must be held (unless it's an atomic) */
137 struct {
138 struct aws_mutex lock;
139 /*
140 * A manager can be in one of two states, READY or SHUTTING_DOWN. The state transition
141 * takes place when ref_count drops to zero.
142 */
143 enum aws_h2_sm_state_type state;
144
145 /**
146 * A set of all connections that meet all requirement to use. Doesn't own the connection.
147 *
148 * Note: there will be connections not in this set, but hold by the stream manager, which can be tracked by the
149 * all_held_connections. Set of `struct aws_h2_sm_connection *`
150 */
151 struct aws_random_access_set ideal_available_set;
152 /**
153 * A set of all available connections that exceed the soft limits set by users. Doesn't own the connection.
154 *
155 * Note: there will be connections not in this set, but hold by the stream manager, which can be tracked by the
156 * all_held_connections. Set of `struct aws_h2_sm_connection *`
157 */
158 struct aws_random_access_set nonideal_available_set;
159 /* We don't mantain set for connections that is full or "dead" (Cannot make any new streams). We have
160 * all_held_connections tracking them */
161
162 /**
163 * The set of all incomplete stream acquisition requests (haven't decide what connection to make the request
164 * to), list of `struct aws_h2_sm_pending_stream_acquisition*`
165 */
166 struct aws_linked_list pending_stream_acquisitions;
167
168 /**
169 * List of all aws_h2_sm_connection that holding the HTTP connection from connection manager.
170 * This list tracks all aws_h2_sm_connection from getting the connection from connection manager until release
171 * it back.
172 * list of `struct aws_h2_sm_connection*`
173 */
174 struct aws_linked_list all_held_connections;
175
176 /**
177 * The number of connections acquired from connection manager and not released yet.
178 */
179 size_t holding_connections_count;
180
181 /**
182 * Counts that contributes to the internal refcount.
183 * When the value changes, s_sm_count_increase/decrease_synced needed.
184 *
185 * AWS_SMCT_CONNECTIONS_ACQUIRING: The number of new connections we acquiring from the connection manager.
186 * AWS_SMCT_OPEN_STREAM: The number of streams that opened and not completed yet.
187 * AWS_SMCT_PENDING_MAKE_REQUESTS: The number of streams that scheduled to be made from a connection but haven't
188 * been executed yet.
189 * AWS_SMCT_PENDING_ACQUISITION: The number of all incomplete stream acquisition requests (haven't decide what
190 * connection to make the request to). So that we don't have compute the size of a linked list every time.
191 */
192 size_t internal_refcount_stats[AWS_SMCT_COUNT];
193
194 bool finish_pending_stream_acquisitions_task_scheduled;
195 } synced_data;
196 };
197
198 /**
199 * Encompasses all of the external operations that need to be done for various
200 * events:
201 * - User level:
202 * stream manager release
203 * stream acquire
204 * - Internal eventloop (anther thread):
205 * connection_acquired
206 * stream_completed
207 * - Internal (can happen from any thread):
208 * connection acquire
209 * connection release
210 *
211 * The transaction is built under the manager's lock (and the internal state is updated optimistically),
212 * but then executed outside of it.
213 */
214 struct aws_http2_stream_management_transaction {
215 struct aws_http2_stream_manager *stream_manager;
216 struct aws_allocator *allocator;
217 size_t new_connections;
218 struct aws_h2_sm_connection *sm_connection_to_release;
219 struct aws_linked_list
220 pending_make_requests; /* List of aws_h2_sm_pending_stream_acquisition with chosen connection */
221 };
222
223 #endif /* AWS_HTTP2_STREAM_MANAGER_IMPL_H */