From cf151d4f588436bc58f245ecf420405bb03126b1 Mon Sep 17 00:00:00 2001 From: Daniel Frey Date: Sat, 2 Mar 2024 11:36:39 +0100 Subject: [PATCH] Add blueprint for limiting connections created simultaneously --- src/test/pq/connection_pool.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/test/pq/connection_pool.cpp b/src/test/pq/connection_pool.cpp index e6d6076..46c0fa9 100644 --- a/src/test/pq/connection_pool.cpp +++ b/src/test/pq/connection_pool.cpp @@ -10,14 +10,19 @@ class limited_connection_pool : public tao::pq::connection_pool { + mutable std::atomic< std::size_t > m_creating = 0; + using tao::pq::connection_pool::connection_pool; [[nodiscard]] auto v_create() const -> std::unique_ptr< tao::pq::connection > override { - if( attached() >= 4 ) { + if( attached() >= 4 || ( m_creating.load() > 2 ) ) { throw std::runtime_error( "connection limit reached" ); } - return connection_pool::v_create(); + ++m_creating; + auto c = connection_pool::v_create(); + --m_creating; + return c; } };