void AsyncConnectionPool::registerForConnection()

in squangle/mysql_client/AsyncConnectionPool.cpp [397:438]


void AsyncConnectionPool::registerForConnection(
    ConnectPoolOperation* raw_pool_op) {
  // Runs only in main thread by run() in the ConnectPoolOperation
  DCHECK_EQ(std::this_thread::get_id(), mysql_client_->threadId());
  {
    std::unique_lock<std::mutex> lock(shutdown_mutex_);
    if (shutting_down_) {
      VLOG(4) << "Pool is shutting down, operation being canceled";
      raw_pool_op->cancel();
      return;
    }
  }
  stats()->incrConnectionsRequested();
  // Pass that to pool
  auto pool_key = PoolKey(
      raw_pool_op->getConnectionKey(), raw_pool_op->getConnectionOptions());

  std::unique_ptr<MysqlPooledHolder> mysql_conn =
      conn_storage_.popConnection(pool_key);

  if (mysql_conn == nullptr) {
    // One more try before opening a new connection: finding a connection from
    // level2 cache and if found, run COM_CHANGE_USER
    if (raw_pool_op->getConnectionOptions().isEnableChangeUser()) {
      auto ret = conn_storage_.popInstanceConnection(pool_key);
      if (ret) { // found
        reuseConnWithChangeUser(raw_pool_op, pool_key, std::move(ret));
        return;
      }
    }
    openNewConnection(raw_pool_op, pool_key);
  } else if (mysql_conn->needResetBeforeReuse()) {
    // reset connection before reusing the connection
    resetConnection(raw_pool_op, pool_key, std::move(mysql_conn));
  } else {
    // Cache hit
    stats()->incrPoolHits();

    mysql_conn->setReusable(true);
    raw_pool_op->connectionCallback(std::move(mysql_conn));
  }
}