diff --git a/.gitignore b/.gitignore index 8def154..e7105eb 100644 --- a/.gitignore +++ b/.gitignore @@ -278,3 +278,9 @@ BUCKAROO_DEPS # Vim *.swp *.swo + +# clangd +.cache/ + +# Claude Code +.claude/settings.local.json diff --git a/timeplus/client_pool.cpp b/timeplus/client_pool.cpp index ec0649e..874c0fb 100644 --- a/timeplus/client_pool.cpp +++ b/timeplus/client_pool.cpp @@ -30,15 +30,17 @@ ClientPool::ClientPtr ClientPool::Acquire(int64_t timeout_ms) { } void ClientPool::GuardedClient::TestConnection() noexcept { + if (!client) { + valid = false; + return; + } + try { client->Ping(); valid = true; } catch (...) { valid = false; } - - TRACE("test connection: host=%s port=%d valid=%s", client->GetCurrentEndpoint()->host.c_str(), client->GetCurrentEndpoint()->port, - valid ? "true" : "false"); } } // namespace timeplus diff --git a/timeplus/client_pool.h b/timeplus/client_pool.h index d687dbe..5c982e1 100644 --- a/timeplus/client_pool.h +++ b/timeplus/client_pool.h @@ -47,7 +47,7 @@ class ClientPool { GuardedClient& operator=(GuardedClient&& other) noexcept { if (this != &other) { - if (pool_ && client) { + if (pool_) { pool_->Release(std::move(client), valid); } pool_ = other.pool_; diff --git a/ut/client_pool_ut.cpp b/ut/client_pool_ut.cpp index 6823a6e..839ce40 100644 --- a/ut/client_pool_ut.cpp +++ b/ut/client_pool_ut.cpp @@ -44,3 +44,18 @@ TEST(ClientPool, BadHost) { ASSERT_THROW(pool.Acquire(1000), std::system_error); } + +TEST(ClientPool, GuardedClientResetStillReturnsSlot) { + ClientOptions client_options; + client_options.host = "localhost"; + ClientPool pool{client_options, /*pool_size=*/1}; + + { + auto guarded = pool.GetGuardedClient(1000); + ASSERT_NE(guarded.client, nullptr); + guarded.client.reset(); + } + + auto client = pool.Acquire(100); + ASSERT_NE(client, nullptr); +}