bool AsyncConnectionPool::canCreateMoreConnections()

in squangle/mysql_client/AsyncConnectionPool.cpp [440:471]


bool AsyncConnectionPool::canCreateMoreConnections(
    const PoolKey& pool_key) const {
  DCHECK_EQ(std::this_thread::get_id(), mysql_client_->threadId());
  auto enqueued_pool_ops = conn_storage_.numQueuedOperations(pool_key);

  auto client_total_conns = mysql_client_->numStartedAndOpenConnections();
  auto client_conn_limit = mysql_client_->getPoolsConnectionLimit();

  // We have the number of connections we are opening and the number of already
  // open, we shouldn't try to create over this sum
  auto [num_pool_allocated, open_conns, pending_conns] =
      counters_.withRLock([&](const auto& locked) {
        return std::make_tuple(
            locked.num_open_connections + locked.num_pending_connections,
            folly::get_default(locked.open_connections, pool_key, 0),
            folly::get_default(locked.pending_connections, pool_key, 0));
      });
  int num_per_key_allocated = open_conns + pending_conns;

  // First we check global limit, then limits of the pool. If we can create more
  // connections, we check if we need comparing the amount of already being
  // opened connections for that key with the number of enqueued operations (the
  // operation that is requesting a new connection should be enqueued at this
  // point.
  if (client_total_conns < client_conn_limit &&
      num_pool_allocated < pool_conn_limit_ &&
      num_per_key_allocated < conn_per_key_limit_ &&
      pending_conns < enqueued_pool_ops) {
    return true;
  }
  return false;
}