Skip to content

Commit

Permalink
Connection manager (#50)
Browse files Browse the repository at this point in the history
* connection manager implementation and tests
  • Loading branch information
bretambrose authored May 21, 2019
1 parent b787708 commit c156326
Show file tree
Hide file tree
Showing 8 changed files with 1,741 additions and 0 deletions.
113 changes: 113 additions & 0 deletions include/aws/http/connection_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#ifndef AWS_HTTP_CONNECTION_MANAGER_H
#define AWS_HTTP_CONNECTION_MANAGER_H

/*
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

#include <aws/http/http.h>

#include <aws/common/byte_buf.h>

struct aws_client_bootstrap;
struct aws_http_connection;
struct aws_http_connection_manager;
struct aws_http_connection_manager_mocks;
struct aws_socket_options;
struct aws_tls_connection_options;

typedef void(aws_http_connection_manager_on_connection_setup_fn)(
struct aws_http_connection *connection,
int error_code,
void *user_data);

/*
* Connection manager configuration struct.
*
* Contains all of the configuration needed to create an http connection as well as
* the maximum number of connections to ever have in existence.
*/
struct aws_http_connection_manager_options {
/*
* http connection configuration
*/
struct aws_client_bootstrap *bootstrap;
size_t initial_window_size;
struct aws_socket_options *socket_options;
struct aws_tls_connection_options *tls_connection_options;
struct aws_byte_cursor host;
uint16_t port;

/*
* Maximum number of connections this manager is allowed to contain
*/
size_t max_connections;
};

AWS_EXTERN_C_BEGIN

/*
* Connection managers are ref counted. Adds one external ref to the manager.
*/
AWS_HTTP_API
void aws_http_connection_manager_acquire(struct aws_http_connection_manager *manager);

/*
* Connection managers are ref counted. Removes one external ref from the manager.
*
* When the ref count goes to zero, the connection manager begins its shut down
* process. All pending connection acquisitions are failed (with callbacks
* invoked) and any (erroneous) subsequent attempts to acquire a connection
* fail immediately. The connection manager destroys itself once all pending
* asynchronous activities have resolved.
*/
AWS_HTTP_API
void aws_http_connection_manager_release(struct aws_http_connection_manager *manager);

/*
* Creates a new connection manager with the supplied configuration options.
*
* The returned connection manager begins with a ref count of 1.
*/
AWS_HTTP_API
struct aws_http_connection_manager *aws_http_connection_manager_new(
struct aws_allocator *allocator,
struct aws_http_connection_manager_options *options);

/*
* Requests a connection from the manager. The requester is notified of
* an acquired connection (or failure to acquire) via the supplied callback.
*
* Once a connection has been successfully acquired from the manager it
* must be released back (via aws_http_connection_manager_release_connection)
* at some point. Failure to do so will cause a resource leak.
*/
AWS_HTTP_API
void aws_http_connection_manager_acquire_connection(
struct aws_http_connection_manager *manager,
aws_http_connection_manager_on_connection_setup_fn *callback,
void *user_data);

/*
* Returns a connection back to the manager. All acquired connections must
* eventually be released back to the manager in order to avoid a resource leak.
*/
AWS_HTTP_API
int aws_http_connection_manager_release_connection(
struct aws_http_connection_manager *manager,
struct aws_http_connection *connection);

AWS_EXTERN_C_END

#endif /* AWS_HTTP_CONNECTION_MANAGER_H */
4 changes: 4 additions & 0 deletions include/aws/http/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ enum aws_http_errors {
AWS_ERROR_HTTP_CALLBACK_FAILURE,
AWS_ERROR_HTTP_WEBSOCKET_CLOSE_FRAME_SENT,
AWS_ERROR_HTTP_WEBSOCKET_IS_MIDCHANNEL_HANDLER,
AWS_ERROR_HTTP_CONNECTION_MANAGER_INVALID_STATE_FOR_ACQUIRE,
AWS_ERROR_HTTP_CONNECTION_MANAGER_VENDED_CONNECTION_UNDERFLOW,

AWS_ERROR_HTTP_END_RANGE = 0x0C00,
};

Expand All @@ -39,6 +42,7 @@ enum aws_http_log_subject {
AWS_LS_HTTP_CONNECTION,
AWS_LS_HTTP_SERVER,
AWS_LS_HTTP_STREAM,
AWS_LS_HTTP_CONNECTION_MANAGER,
AWS_LS_HTTP_WEBSOCKET,
};

Expand Down
51 changes: 51 additions & 0 deletions include/aws/http/private/connection_manager_function_table.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef AWS_HTTP_CONNECTION_MANAGER_FUNCTION_TABLE_H
#define AWS_HTTP_CONNECTION_MANAGER_FUNCTION_TABLE_H

/*
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

#include <aws/http/http.h>

#include <aws/http/connection.h>

typedef int(aws_http_connection_manager_create_connection_fn)(const struct aws_http_client_connection_options *options);
typedef void(aws_http_connection_manager_close_connection_fn)(struct aws_http_connection *connection);
typedef void(aws_http_connection_manager_release_connection_fn)(struct aws_http_connection *connection);
typedef bool(aws_http_connection_manager_is_connection_open_fn)(const struct aws_http_connection *connection);

struct aws_http_connection_manager_function_table {
/*
* Downstream http functions
*/
aws_http_connection_manager_create_connection_fn *create_connection;
aws_http_connection_manager_close_connection_fn *close_connection;
aws_http_connection_manager_release_connection_fn *release_connection;
aws_http_connection_manager_is_connection_open_fn *is_connection_open;
};

AWS_HTTP_API
bool aws_http_connection_manager_function_table_is_valid(
const struct aws_http_connection_manager_function_table *table);

AWS_HTTP_API
void aws_http_connection_manager_set_function_table(
struct aws_http_connection_manager *manager,
const struct aws_http_connection_manager_function_table *function_table);

AWS_HTTP_API
extern const struct aws_http_connection_manager_function_table
*g_aws_http_connection_manager_default_function_table_ptr;

#endif /* AWS_HTTP_CONNECTION_MANAGER_FUNCTION_TABLE_H */
Loading

0 comments on commit c156326

Please sign in to comment.