Skip to content

Commit c156326

Browse files
authored
Connection manager (#50)
* connection manager implementation and tests
1 parent b787708 commit c156326

File tree

8 files changed

+1741
-0
lines changed

8 files changed

+1741
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#ifndef AWS_HTTP_CONNECTION_MANAGER_H
2+
#define AWS_HTTP_CONNECTION_MANAGER_H
3+
4+
/*
5+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License").
8+
* You may not use this file except in compliance with the License.
9+
* A copy of the License is located at
10+
*
11+
* http://aws.amazon.com/apache2.0
12+
*
13+
* or in the "license" file accompanying this file. This file is distributed
14+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
15+
* express or implied. See the License for the specific language governing
16+
* permissions and limitations under the License.
17+
*/
18+
19+
#include <aws/http/http.h>
20+
21+
#include <aws/common/byte_buf.h>
22+
23+
struct aws_client_bootstrap;
24+
struct aws_http_connection;
25+
struct aws_http_connection_manager;
26+
struct aws_http_connection_manager_mocks;
27+
struct aws_socket_options;
28+
struct aws_tls_connection_options;
29+
30+
typedef void(aws_http_connection_manager_on_connection_setup_fn)(
31+
struct aws_http_connection *connection,
32+
int error_code,
33+
void *user_data);
34+
35+
/*
36+
* Connection manager configuration struct.
37+
*
38+
* Contains all of the configuration needed to create an http connection as well as
39+
* the maximum number of connections to ever have in existence.
40+
*/
41+
struct aws_http_connection_manager_options {
42+
/*
43+
* http connection configuration
44+
*/
45+
struct aws_client_bootstrap *bootstrap;
46+
size_t initial_window_size;
47+
struct aws_socket_options *socket_options;
48+
struct aws_tls_connection_options *tls_connection_options;
49+
struct aws_byte_cursor host;
50+
uint16_t port;
51+
52+
/*
53+
* Maximum number of connections this manager is allowed to contain
54+
*/
55+
size_t max_connections;
56+
};
57+
58+
AWS_EXTERN_C_BEGIN
59+
60+
/*
61+
* Connection managers are ref counted. Adds one external ref to the manager.
62+
*/
63+
AWS_HTTP_API
64+
void aws_http_connection_manager_acquire(struct aws_http_connection_manager *manager);
65+
66+
/*
67+
* Connection managers are ref counted. Removes one external ref from the manager.
68+
*
69+
* When the ref count goes to zero, the connection manager begins its shut down
70+
* process. All pending connection acquisitions are failed (with callbacks
71+
* invoked) and any (erroneous) subsequent attempts to acquire a connection
72+
* fail immediately. The connection manager destroys itself once all pending
73+
* asynchronous activities have resolved.
74+
*/
75+
AWS_HTTP_API
76+
void aws_http_connection_manager_release(struct aws_http_connection_manager *manager);
77+
78+
/*
79+
* Creates a new connection manager with the supplied configuration options.
80+
*
81+
* The returned connection manager begins with a ref count of 1.
82+
*/
83+
AWS_HTTP_API
84+
struct aws_http_connection_manager *aws_http_connection_manager_new(
85+
struct aws_allocator *allocator,
86+
struct aws_http_connection_manager_options *options);
87+
88+
/*
89+
* Requests a connection from the manager. The requester is notified of
90+
* an acquired connection (or failure to acquire) via the supplied callback.
91+
*
92+
* Once a connection has been successfully acquired from the manager it
93+
* must be released back (via aws_http_connection_manager_release_connection)
94+
* at some point. Failure to do so will cause a resource leak.
95+
*/
96+
AWS_HTTP_API
97+
void aws_http_connection_manager_acquire_connection(
98+
struct aws_http_connection_manager *manager,
99+
aws_http_connection_manager_on_connection_setup_fn *callback,
100+
void *user_data);
101+
102+
/*
103+
* Returns a connection back to the manager. All acquired connections must
104+
* eventually be released back to the manager in order to avoid a resource leak.
105+
*/
106+
AWS_HTTP_API
107+
int aws_http_connection_manager_release_connection(
108+
struct aws_http_connection_manager *manager,
109+
struct aws_http_connection *connection);
110+
111+
AWS_EXTERN_C_END
112+
113+
#endif /* AWS_HTTP_CONNECTION_MANAGER_H */

include/aws/http/http.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ enum aws_http_errors {
3131
AWS_ERROR_HTTP_CALLBACK_FAILURE,
3232
AWS_ERROR_HTTP_WEBSOCKET_CLOSE_FRAME_SENT,
3333
AWS_ERROR_HTTP_WEBSOCKET_IS_MIDCHANNEL_HANDLER,
34+
AWS_ERROR_HTTP_CONNECTION_MANAGER_INVALID_STATE_FOR_ACQUIRE,
35+
AWS_ERROR_HTTP_CONNECTION_MANAGER_VENDED_CONNECTION_UNDERFLOW,
36+
3437
AWS_ERROR_HTTP_END_RANGE = 0x0C00,
3538
};
3639

@@ -39,6 +42,7 @@ enum aws_http_log_subject {
3942
AWS_LS_HTTP_CONNECTION,
4043
AWS_LS_HTTP_SERVER,
4144
AWS_LS_HTTP_STREAM,
45+
AWS_LS_HTTP_CONNECTION_MANAGER,
4246
AWS_LS_HTTP_WEBSOCKET,
4347
};
4448

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#ifndef AWS_HTTP_CONNECTION_MANAGER_FUNCTION_TABLE_H
2+
#define AWS_HTTP_CONNECTION_MANAGER_FUNCTION_TABLE_H
3+
4+
/*
5+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License").
8+
* You may not use this file except in compliance with the License.
9+
* A copy of the License is located at
10+
*
11+
* http://aws.amazon.com/apache2.0
12+
*
13+
* or in the "license" file accompanying this file. This file is distributed
14+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
15+
* express or implied. See the License for the specific language governing
16+
* permissions and limitations under the License.
17+
*/
18+
19+
#include <aws/http/http.h>
20+
21+
#include <aws/http/connection.h>
22+
23+
typedef int(aws_http_connection_manager_create_connection_fn)(const struct aws_http_client_connection_options *options);
24+
typedef void(aws_http_connection_manager_close_connection_fn)(struct aws_http_connection *connection);
25+
typedef void(aws_http_connection_manager_release_connection_fn)(struct aws_http_connection *connection);
26+
typedef bool(aws_http_connection_manager_is_connection_open_fn)(const struct aws_http_connection *connection);
27+
28+
struct aws_http_connection_manager_function_table {
29+
/*
30+
* Downstream http functions
31+
*/
32+
aws_http_connection_manager_create_connection_fn *create_connection;
33+
aws_http_connection_manager_close_connection_fn *close_connection;
34+
aws_http_connection_manager_release_connection_fn *release_connection;
35+
aws_http_connection_manager_is_connection_open_fn *is_connection_open;
36+
};
37+
38+
AWS_HTTP_API
39+
bool aws_http_connection_manager_function_table_is_valid(
40+
const struct aws_http_connection_manager_function_table *table);
41+
42+
AWS_HTTP_API
43+
void aws_http_connection_manager_set_function_table(
44+
struct aws_http_connection_manager *manager,
45+
const struct aws_http_connection_manager_function_table *function_table);
46+
47+
AWS_HTTP_API
48+
extern const struct aws_http_connection_manager_function_table
49+
*g_aws_http_connection_manager_default_function_table_ptr;
50+
51+
#endif /* AWS_HTTP_CONNECTION_MANAGER_FUNCTION_TABLE_H */

0 commit comments

Comments
 (0)