Skip to content

Commit

Permalink
Add blueprint for limiting connections created simultaneously
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Mar 2, 2024
1 parent 3f11772 commit cf151d4
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/test/pq/connection_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

Expand Down

0 comments on commit cf151d4

Please sign in to comment.