-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathHttpConnectionManager.h
More file actions
142 lines (122 loc) · 6.73 KB
/
HttpConnectionManager.h
File metadata and controls
142 lines (122 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#pragma once
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/crt/http/HttpConnection.h>
#include <atomic>
#include <condition_variable>
#include <future>
#include <mutex>
struct aws_http_connection_manager;
namespace Aws
{
namespace Crt
{
namespace Http
{
/**
* Invoked when a connection from the pool is available. If a connection was successfully obtained
* the connection shared_ptr can be seated into your own copy of connection. If it failed, errorCode
* will be non-zero.
*/
using OnClientConnectionAvailable =
std::function<void(std::shared_ptr<HttpClientConnection>, int errorCode)>;
/**
* Configuration struct containing all options related to connection manager behavior
*/
class AWS_CRT_CPP_API HttpClientConnectionManagerOptions
{
public:
HttpClientConnectionManagerOptions() noexcept;
HttpClientConnectionManagerOptions(const HttpClientConnectionManagerOptions &rhs) = default;
HttpClientConnectionManagerOptions(HttpClientConnectionManagerOptions &&rhs) = default;
HttpClientConnectionManagerOptions &operator=(const HttpClientConnectionManagerOptions &rhs) = default;
HttpClientConnectionManagerOptions &operator=(HttpClientConnectionManagerOptions &&rhs) = default;
/**
* The http connection options to use for each connection created by the manager
*/
HttpClientConnectionOptions ConnectionOptions;
/**
* The maximum number of connections the manager is allowed to create/manage
*/
size_t MaxConnections;
/** If set, initiate shutdown will return a future that will allow a user to block until the
* connection manager has completely released all resources. This isn't necessary during the normal
* flow of an application, but it is useful for scenarios, such as tests, that need deterministic
* shutdown ordering. Be aware, if you use this anywhere other than the main thread, you will most
* likely cause a deadlock. If this is set, you MUST call InitiateShutdown() before releasing your last
* reference to the connection manager.
*/
bool EnableBlockingShutdown;
/**
* THIS IS AN EXPERIMENTAL AND UNSTABLE API
* (Optional)
* An array of network interface names. The manager will distribute the connections across network
* interface names provided in this array. If any interface name is invalid, goes down, or has any
* issues like network access, you will see connection failures. If
* `socket_options.network_interface_name` is also set, an `AWS_ERROR_INVALID_ARGUMENT` error will be
* raised.
*
* This option is only supported on Linux, MacOS, and platforms that have either SO_BINDTODEVICE or
* IP_BOUND_IF. It is not supported on Windows. `AWS_ERROR_PLATFORM_NOT_SUPPORTED` will be raised on
* unsupported platforms.
*/
Vector<ByteCursor> NetworkInterfaces;
};
/**
* Manages a pool of connections to a specific endpoint using the same socket and tls options.
*/
class AWS_CRT_CPP_API HttpClientConnectionManager final
: public std::enable_shared_from_this<HttpClientConnectionManager>
{
public:
~HttpClientConnectionManager();
/**
* Acquires a connection from the pool. onClientConnectionAvailable will be invoked upon an available
* connection. Returns true if the connection request was successfully queued, returns false if it
* failed. On failure, onClientConnectionAvailable will not be invoked. After receiving a connection, it
* will automatically be cleaned up when your last reference to the shared_ptr is released.
*
* @param onClientConnectionAvailable callback to invoke when a connection becomes available or the
* acquisition attempt terminates
* @return true if the acquisition was successfully kicked off, false otherwise (no callback)
*/
bool AcquireConnection(const OnClientConnectionAvailable &onClientConnectionAvailable) noexcept;
/**
* Starts shutdown of the connection manager. Returns a future to the connection manager's shutdown
* process. If EnableBlockingDestruct was enabled on the connection manager options, calling get() on
* the returned future will block until the last connection is released. If the option is not set, get()
* will immediately return.
* @return future which will complete when shutdown has completed
*/
std::future<void> InitiateShutdown() noexcept;
/**
* Factory function for connection managers
*
* @param connectionManagerOptions connection manager configuration data
* @param allocator allocator to use
* @return a new connection manager instance
*/
static std::shared_ptr<HttpClientConnectionManager> NewClientConnectionManager(
const HttpClientConnectionManagerOptions &connectionManagerOptions,
Allocator *allocator = ApiAllocator()) noexcept;
private:
HttpClientConnectionManager(
const HttpClientConnectionManagerOptions &options,
Allocator *allocator = ApiAllocator()) noexcept;
Allocator *m_allocator;
aws_http_connection_manager *m_connectionManager;
HttpClientConnectionManagerOptions m_options;
std::promise<void> m_shutdownPromise;
std::atomic<bool> m_releaseInvoked;
static void s_onConnectionSetup(
aws_http_connection *connection,
int errorCode,
void *userData) noexcept;
static void s_shutdownCompleted(void *userData) noexcept;
friend class ManagedConnection;
};
} // namespace Http
} // namespace Crt
} // namespace Aws