-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* HttpClientConnection manager and unit tests. * Updated Http tag. * Updated common version. * Updated aws-c-io tag. * HttpConnection can clean itself up * Added host resolver, updates to handle io changes, added test for host resolver.
- Loading branch information
1 parent
30f4fb0
commit be7daab
Showing
22 changed files
with
1,172 additions
and
4,284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#pragma once | ||
/* | ||
* 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/crt/http/HttpConnection.h> | ||
#include <condition_variable> | ||
#include <mutex> | ||
|
||
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. It is your responsibility to release the connection when you are finished with it. | ||
*/ | ||
using OnClientConnectionAvailable = | ||
std::function<void(std::shared_ptr<HttpClientConnection> connection, int errorCode)>; | ||
|
||
struct HttpClientConnectionManagerOptions | ||
{ | ||
HttpClientConnectionManagerOptions(); | ||
Io::ClientBootstrap *bootstrap; | ||
size_t initialWindowSize; | ||
Io::SocketOptions *socketOptions; | ||
Io::TlsConnectionOptions *tlsConnectionOptions; | ||
ByteCursor hostName; | ||
uint16_t port; | ||
size_t maxConnections; | ||
}; | ||
|
||
/** | ||
* Manages a pool of connections to a specific endpoint using the same socket and tls options. | ||
*/ | ||
class 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 pooled, returns false if it | ||
* failed. On failure, onClientConnectionAvailable will not be invoked. After receiving a connection, | ||
* you must invoke ReleaseConnection(). | ||
*/ | ||
bool AcquireConnection(const OnClientConnectionAvailable &onClientConnectionAvailable) noexcept; | ||
|
||
/** | ||
* Releases a connection back to the pool. This will cause queued consumers to be serviced, or the | ||
* connection will be pooled waiting on another call to AcquireConnection | ||
*/ | ||
void ReleaseConnection(std::shared_ptr<HttpClientConnection> connection) noexcept; | ||
|
||
int LastError() const noexcept { return m_lastError; } | ||
explicit operator bool() const noexcept { return m_good; } | ||
|
||
static std::shared_ptr<HttpClientConnectionManager> NewClientConnectionManager( | ||
const HttpClientConnectionManagerOptions &connectionManagerOptions, | ||
Allocator *allocator = DefaultAllocator()) noexcept; | ||
|
||
private: | ||
HttpClientConnectionManager( | ||
const HttpClientConnectionManagerOptions &connectionManagerOptions, | ||
Allocator *allocator = DefaultAllocator()) noexcept; | ||
|
||
Vector<std::shared_ptr<HttpClientConnection>> m_connections; | ||
List<OnClientConnectionAvailable> m_pendingConnectionRequests; | ||
Allocator *m_allocator; | ||
Io::ClientBootstrap *m_bootstrap; | ||
size_t m_initialWindowSize; | ||
Io::SocketOptions m_socketOptions; | ||
Io::TlsConnectionOptions m_tlsConnOptions; | ||
String m_hostName; | ||
uint16_t m_port; | ||
bool m_good; | ||
int m_lastError; | ||
size_t m_maxSize; | ||
size_t m_outstandingVendedConnections; | ||
size_t m_pendingConnections; | ||
std::mutex m_connectionsLock; | ||
|
||
void onConnectionSetup(const std::shared_ptr<HttpClientConnection> &connection, int errorCode) noexcept; | ||
void onConnectionShutdown(HttpClientConnection &connection, int errorCode) noexcept; | ||
bool createConnection() noexcept; | ||
void poolOrVendConnection(std::shared_ptr<HttpClientConnection> connection, bool isRelease) noexcept; | ||
}; | ||
} // namespace Http | ||
} // namespace Crt | ||
} // namespace Aws |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#pragma once | ||
/* | ||
* 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/crt/Types.h> | ||
|
||
#include <aws/io/host_resolver.h> | ||
|
||
#include <functional> | ||
|
||
namespace Aws | ||
{ | ||
namespace Crt | ||
{ | ||
namespace Io | ||
{ | ||
class EventLoopGroup; | ||
class HostResolver; | ||
|
||
using HostAddress = aws_host_address; | ||
|
||
/** | ||
* Invoked upon resolution of an address. You do not own the memory pointed to in addresses, if you persist | ||
* the data, copy it first. If errorCode is AWS_ERROR_SUCCESS, the operation succeeded. Otherwise, the | ||
* operation failed. | ||
*/ | ||
using OnHostResolved = | ||
std::function<void(HostResolver &resolver, const Vector<HostAddress> &addresses, int errorCode)>; | ||
|
||
class HostResolver | ||
{ | ||
public: | ||
virtual ~HostResolver(); | ||
virtual bool ResolveHost(const String &host, const OnHostResolved &onResolved) noexcept = 0; | ||
virtual aws_host_resolver *GetUnderlyingHandle() noexcept = 0; | ||
virtual aws_host_resolution_config *GetConfig() noexcept = 0; | ||
}; | ||
|
||
class DefaultHostResolver final : public HostResolver | ||
{ | ||
public: | ||
/** | ||
* Resolves DNS addresses. maxHosts is the number of unique hosts to maintain in the cache. maxTTL is | ||
* how long to keep an address in the cache before evicting it. | ||
*/ | ||
DefaultHostResolver( | ||
EventLoopGroup &elGroup, | ||
size_t maxHosts, | ||
size_t maxTTL, | ||
Allocator *allocator = DefaultAllocator()) noexcept; | ||
~DefaultHostResolver(); | ||
DefaultHostResolver(const DefaultHostResolver &) = delete; | ||
DefaultHostResolver &operator=(const DefaultHostResolver &) = delete; | ||
DefaultHostResolver(DefaultHostResolver &&) = delete; | ||
DefaultHostResolver &operator=(DefaultHostResolver &&) = delete; | ||
|
||
operator bool() const noexcept { return m_initialized; } | ||
|
||
/** | ||
* Kicks off an asynchronous resolution of host. onResolved will be invoked upon completion of the | ||
* resolution. If this returns false, the resolution was not attempted. On true, onResolved will be | ||
* called with the result. | ||
*/ | ||
bool ResolveHost(const String &host, const OnHostResolved &onResolved) noexcept override; | ||
aws_host_resolver *GetUnderlyingHandle() noexcept override { return &m_resolver; } | ||
aws_host_resolution_config *GetConfig() noexcept override { return &m_config; } | ||
|
||
private: | ||
aws_host_resolver m_resolver; | ||
aws_host_resolution_config m_config; | ||
Allocator *m_allocator; | ||
bool m_initialized; | ||
|
||
static void s_onHostResolved( | ||
struct aws_host_resolver *resolver, | ||
const struct aws_string *host_name, | ||
int err_code, | ||
const struct aws_array_list *host_addresses, | ||
void *user_data); | ||
}; | ||
} // namespace Io | ||
} // namespace Crt | ||
} // namespace Aws |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.