void ConnectionManager::addConnection()

in wangle/acceptor/ConnectionManager.cpp [39:87]


void ConnectionManager::addConnection(
    ManagedConnection* connection,
    bool timeout) {
  CHECK_NOTNULL(connection);
  ConnectionManager* oldMgr = connection->getConnectionManager();
  if (oldMgr != this) {
    if (oldMgr) {
      // 'connection' was being previously managed in a different thread.
      // We must remove it from that manager before adding it to this one.
      oldMgr->removeConnection(connection);
    }

    // put the connection into busy part first.  This should not matter at all
    // because the last callback for an idle connection must be onDeactivated(),
    // so the connection must be moved to idle part then.
    conns_.push_front(*connection);

    connection->setConnectionManager(this);
    if (callback_) {
      callback_->onConnectionAdded(connection);
    }
  }
  if (timeout) {
    scheduleTimeout(connection, timeout_);
  }

  if (drainHelper_.getShutdownState() >=
          ShutdownState::NOTIFY_PENDING_SHUTDOWN &&
      notifyPendingShutdown_) {
    connection->fireNotifyPendingShutdown();
  }

  if (drainHelper_.getShutdownState() >= ShutdownState::CLOSE_WHEN_IDLE) {
    // closeWhenIdle can delete the connection (it was just created, so it's
    // probably idle).  Delay the closeWhenIdle call until the end of the loop
    // where it will be safer to terminate the conn.
    // Hold a DestructorGuard to the end of the loop for this
    eventBase_->runInLoop([connection,
                           this,
                           cmDg = DestructorGuard(this),
                           connDg = DestructorGuard(connection)] {
      if (connection->listHook_.is_linked()) {
        auto it = conns_.iterator_to(*connection);
        DCHECK(it != conns_.end());
        connection->fireCloseWhenIdle(!notifyPendingShutdown_);
      }
    });
  }
}