|
| 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 */ |
0 commit comments