void ConnectPoolOperation::specializedTimeoutTriggered()

in squangle/mysql_client/AsyncConnectionPool.cpp [902:947]


void ConnectPoolOperation::specializedTimeoutTriggered() {
  auto locked_pool = pool_.lock();
  if (locked_pool) {
    cancelPreOperation();

    // Check if the timeout happened because of the host is being slow or the
    // pool is lacking resources
    auto pool_key = PoolKey(getConnectionKey(), getConnectionOptions());
    auto key_stats = locked_pool->getPoolKeyStats(pool_key);
    auto num_open = key_stats.open_connections;
    auto num_opening = key_stats.pending_connections;

    // As a way to be realistic regarding the reason a connection was not
    // obtained, we start from the principle that this is pool's fault.
    // We can only blame the host (by forwarding 2013) if we have no
    // open connections and none trying to be open.
    // The second rule is applied where the resource restriction is so small
    // that the pool can't even try to open a connection.
    if (!(num_open == 0 &&
          (num_opening > 0 ||
           locked_pool->canCreateMoreConnections(pool_key)))) {
      auto delta = std::chrono::steady_clock::now() - start_time_;
      int64_t delta_micros =
          std::chrono::duration_cast<std::chrono::microseconds>(delta).count();
      auto msg = folly::stringPrintf(
          "[%d](%s)Connection to %s:%d timed out in pool"
          "(open %lu, opening %lu, key limit %lu) (took %.2fms)",
          static_cast<uint16_t>(SquangleErrno::SQ_ERRNO_POOL_CONN_TIMEOUT),
          kErrorPrefix,
          host().c_str(),
          port(),
          num_open,
          num_opening,
          locked_pool->conn_per_key_limit_,
          delta_micros / 1000.0);
      setAsyncClientError(
          static_cast<uint16_t>(SquangleErrno::SQ_ERRNO_POOL_CONN_TIMEOUT),
          msg,
          "Connection timed out in pool");
      attemptFailed(OperationResult::TimedOut);
      return;
    }
  }

  ConnectOperation::timeoutHandler(false, true);
}