Skip to content

CXX-3100 Check return when acquiring client from pool #1205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ enum class error_code : std::int32_t {
// used.
k_invalid_search_index_view,

// Timed out while waiting for a client to be returned to the pool
k_pool_wait_queue_timeout,

// Add new constant string message to error_code.cpp as well!
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class error_category final : public std::error_category {
case error_code::k_invalid_search_index_view:
return "invalid use of default constructed or moved-from "
"mongocxx::search_index_view object";
case error_code::k_pool_wait_queue_timeout:
return "timed out while waiting for a client to be returned to the pool";
default:
return "unknown mongocxx error";
}
Expand Down
10 changes: 8 additions & 2 deletions src/mongocxx/lib/mongocxx/v_noabi/mongocxx/pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,14 @@ pool::entry::operator bool() const noexcept {
pool::entry::entry(pool::entry::unique_client p) : _client(std::move(p)) {}

pool::entry pool::acquire() {
return entry(entry::unique_client(new client(libmongoc::client_pool_pop(_impl->client_pool_t)),
[this](client* client) { _release(client); }));
auto cli = libmongoc::client_pool_pop(_impl->client_pool_t);
if (!cli)
throw exception{
error_code::k_pool_wait_queue_timeout,
"failed to acquire client, possibly due to parameter 'waitQueueTimeoutMS' limits."};

return entry(
entry::unique_client(new client(cli), [this](client* client) { _release(client); }));
}

stdx::optional<pool::entry> pool::try_acquire() {
Expand Down
15 changes: 15 additions & 0 deletions src/mongocxx/test/pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <bsoncxx/test/catch.hh>
#include <mongocxx/test/catch_helpers.hh>
#include <mongocxx/test/client_helpers.hh>

namespace {
using namespace mongocxx;
Expand Down Expand Up @@ -183,4 +184,18 @@ TEST_CASE("a pool is created with an invalid connection string", "[pool]") {

REQUIRE_THROWS_AS(pool{mongocxx::uri(uristr)}, operation_exception);
}

TEST_CASE("acquiring a client throws if waitQueueTimeoutMS expires", "[pool]") {
instance::current();
mongocxx::pool pool{
mongocxx::uri{"mongodb://localhost:27017/?waitQueueTimeoutMS=1&maxPoolSize=1"},
options::pool(test_util::add_test_server_api())};
// Acquire only available client:
auto client = pool.acquire();
CHECK(client);
// Try to acquire again. Expect timeout:
REQUIRE_THROWS_WITH(pool.acquire(),
Catch::Matchers::ContainsSubstring("failed to acquire client"));
}

} // namespace