-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix off-by-one error in connection pooling introduced with asio certi…
…ficate changes in 2.10.4 (#920) * Fix off-by-one error introduced in connection pool stack. This got introduced when the connection pool queue was changed into a stack to reuse hot connections. * Make connection_pool_stack testable and add tests.
- Loading branch information
1 parent
943c6f8
commit 9c8e0d4
Showing
5 changed files
with
120 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#pragma once | ||
|
||
#include "cpprest/details/cpprest_compat.h" | ||
#include <stddef.h> | ||
#include <memory> | ||
#include <vector> | ||
|
||
namespace web | ||
{ | ||
namespace http | ||
{ | ||
namespace client | ||
{ | ||
namespace details | ||
{ | ||
|
||
template<class ConnectionIsh> | ||
class connection_pool_stack | ||
{ | ||
public: | ||
// attempts to acquire a connection from the deque; returns nullptr if no connection is | ||
// available | ||
std::shared_ptr<ConnectionIsh> try_acquire() CPPREST_NOEXCEPT | ||
{ | ||
const size_t oldConnectionsSize = m_connections.size(); | ||
if (oldConnectionsSize == 0) | ||
{ | ||
m_staleBefore = 0; | ||
return nullptr; | ||
} | ||
|
||
auto result = std::move(m_connections.back()); | ||
m_connections.pop_back(); | ||
const size_t newConnectionsSize = m_connections.size(); | ||
if (m_staleBefore > newConnectionsSize) | ||
{ | ||
m_staleBefore = newConnectionsSize; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
// releases `released` back to the connection pool | ||
void release(std::shared_ptr<ConnectionIsh>&& released) | ||
{ | ||
m_connections.push_back(std::move(released)); | ||
} | ||
|
||
bool free_stale_connections() CPPREST_NOEXCEPT | ||
{ | ||
assert(m_staleBefore <= m_connections.size()); | ||
m_connections.erase(m_connections.begin(), m_connections.begin() + m_staleBefore); | ||
const size_t connectionsSize = m_connections.size(); | ||
m_staleBefore = connectionsSize; | ||
return (connectionsSize != 0); | ||
} | ||
|
||
private: | ||
std::vector<std::shared_ptr<ConnectionIsh>> m_connections; | ||
size_t m_staleBefore = 0; | ||
}; | ||
|
||
} // details | ||
} // client | ||
} // http | ||
} // web |
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
45 changes: 45 additions & 0 deletions
45
Release/tests/functional/http/client/connection_pool_tests.cpp
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,45 @@ | ||
#include "stdafx.h" | ||
#include <memory> | ||
#include "../../../src/http/common/connection_pool_helpers.h" | ||
|
||
using namespace web::http::client::details; | ||
|
||
SUITE(connection_pooling) { | ||
TEST(empty_returns_nullptr) { | ||
connection_pool_stack<int> connectionStack; | ||
VERIFY_ARE_EQUAL(connectionStack.try_acquire(), std::shared_ptr<int>{}); | ||
} | ||
|
||
static int noisyCount = 0; | ||
struct noisy { | ||
noisy() = delete; | ||
noisy(int) { ++noisyCount; } | ||
noisy(const noisy&) = delete; | ||
noisy(noisy&&) { ++noisyCount; } | ||
noisy& operator=(const noisy&) = delete; | ||
noisy& operator=(noisy&&) = delete; | ||
~noisy() { --noisyCount; } | ||
}; | ||
|
||
TEST(cycled_connections_survive) { | ||
connection_pool_stack<noisy> connectionStack; | ||
VERIFY_ARE_EQUAL(0, noisyCount); | ||
connectionStack.release(std::make_shared<noisy>(42)); | ||
connectionStack.release(std::make_shared<noisy>(42)); | ||
connectionStack.release(std::make_shared<noisy>(42)); | ||
VERIFY_ARE_EQUAL(3, noisyCount); | ||
VERIFY_IS_TRUE(connectionStack.free_stale_connections()); | ||
auto tmp = connectionStack.try_acquire(); | ||
VERIFY_ARE_NOT_EQUAL(tmp, std::shared_ptr<noisy>{}); | ||
connectionStack.release(std::move(tmp)); | ||
VERIFY_ARE_EQUAL(tmp, std::shared_ptr<noisy>{}); | ||
tmp = connectionStack.try_acquire(); | ||
VERIFY_ARE_NOT_EQUAL(tmp, std::shared_ptr<noisy>{}); | ||
connectionStack.release(std::move(tmp)); | ||
VERIFY_IS_TRUE(connectionStack.free_stale_connections()); | ||
VERIFY_ARE_EQUAL(1, noisyCount); | ||
VERIFY_IS_FALSE(connectionStack.free_stale_connections()); | ||
VERIFY_ARE_EQUAL(0, noisyCount); | ||
VERIFY_IS_FALSE(connectionStack.free_stale_connections()); | ||
} | ||
}; |