void AsyncMysqlClient::drain()

in squangle/mysql_client/AsyncMysqlClient.cpp [84:114]


void AsyncMysqlClient::drain(bool also_block_operations) {
  pending_.withWLock([&](auto& pending) {
    pending.block_operations = also_block_operations;

    auto it = pending.operations.begin();
    // Clean out any unstarted operations.
    while (it != pending.operations.end()) {
      // So here the Operation `run` was not called
      // We don't need to lock the state change in the operation here since the
      // cancelling process is going to fire not matter in which part it is.
      if ((*it)->state() == OperationState::Unstarted) {
        (*it)->cancel();
        it = pending.operations.erase(it);
      } else {
        ++it;
      }
    }
  });

  // Now wait for any started operations to complete.
  std::unique_lock<std::mutex> counter_lock(this->counters_mutex_);
  active_connections_closed_cv_.wait(
      counter_lock, [&also_block_operations, this] {
        if (also_block_operations) {
          VLOG(11)
              << "Waiting for " << this->active_connection_counter_
              << " connections to be released before shutting client down ";
        }
        return this->active_connection_counter_ == 0;
      });
}