Repository: envoyproxy/envoy
File: source/common/listener_manager/active_tcp_socket.cc (C++)

Code before:
#include "source/common/stream_info/stream_info_impl.h"

namespace Envoy {
namespace Server {

ActiveTcpSocket::ActiveTcpSocket(ActiveStreamListenerBase& listener,
                                 Network::ConnectionSocketPtr&& socket,
                                 bool hand_off_restored_destination_connections)
    : listener_(listener), socket_(std::move(socket)),
      hand_off_restored_destination_connections_(hand_off_restored_destination_connections),
      iter_(accept_filters_.end()),
      stream_info_(std::make_unique<StreamInfo::StreamInfoImpl>(
          listener_.dispatcher().timeSource(), socket_->connectionInfoProviderSharedPtr(),
          StreamInfo::FilterState::LifeSpan::Connection)) {
  listener_.stats_.downstream_pre_cx_active_.inc();

  // Automatically populate network namespace from listener address if present.
  const auto& local_address = socket_->connectionInfoProvider().localAddress();
  if (local_address && local_address->networkNamespace().has_value() &&
      !local_address->networkNamespace()->empty()) {
    stream_info_->filterState()->setData(
        Network::DownstreamNetworkNamespace::key(),
        std::make_unique<Network::DownstreamNetworkNamespace>(
            local_address->networkNamespace().value()),
        StreamInfo::FilterState::StateType::ReadOnly, StreamInfo::FilterState::LifeSpan::Connection,
        StreamInfo::StreamSharingMayImpactPooling::SharedWithUpstreamConnection);
  }
}

ActiveTcpSocket::~ActiveTcpSocket() {
  accept_filters_.clear();

  listener_.stats_.downstream_pre_cx_active_.dec();
  // If the underlying socket is no longer attached, it means that it has been transferred to
  // an active connection. In this case, the active connection will decrement the number
  // of listener connections.
  // TODO(mattklein123): In general the way we account for the number of listener connections
  // is incredibly fragile. Revisit this by potentially merging ActiveTcpSocket and
  // ActiveTcpConnection, having a shared object which does accounting (but would require
  // another allocation, etc.).
  if (socket_ != nullptr) {
    listener_.decNumConnections();
  }
}

Event::Dispatcher& ActiveTcpSocket::dispatcher() { return listener_.dispatcher(); }

void ActiveTcpSocket::onTimeout() {
  listener_.stats_.downstream_pre_cx_timeout_.inc();
  ASSERT(inserted());
  ENVOY_LOG(debug, "listener filter times out after {} ms",

Review comment (suggestion):
No, this should not be a default! This will effectively disable pooling at HTTP level because it'll never combine requests from multiple network namespaces.

Code after:
#include "source/common/stream_info/stream_info_impl.h"

namespace Envoy {
namespace Server {

ActiveTcpSocket::ActiveTcpSocket(ActiveStreamListenerBase& listener,
                                 Network::ConnectionSocketPtr&& socket,
                                 bool hand_off_restored_destination_connections)
    : listener_(listener), socket_(std::move(socket)),
      hand_off_restored_destination_connections_(hand_off_restored_destination_connections),
      iter_(accept_filters_.end()),
      stream_info_(std::make_unique<StreamInfo::StreamInfoImpl>(
          listener_.dispatcher().timeSource(), socket_->connectionInfoProviderSharedPtr(),
          StreamInfo::FilterState::LifeSpan::Connection)) {
  listener_.stats_.downstream_pre_cx_active_.inc();

  // Automatically populate network namespace from listener address if present.
  const auto& local_address = socket_->connectionInfoProvider().localAddress();
  if (local_address && local_address->networkNamespace().has_value() &&
      !local_address->networkNamespace()->empty()) {
    stream_info_->filterState()->setData(Network::DownstreamNetworkNamespace::key(),
                                         std::make_unique<Network::DownstreamNetworkNamespace>(
                                             local_address->networkNamespace().value()),
                                         StreamInfo::FilterState::StateType::ReadOnly,
                                         StreamInfo::FilterState::LifeSpan::Connection);
  }
}

ActiveTcpSocket::~ActiveTcpSocket() {
  accept_filters_.clear();

  listener_.stats_.downstream_pre_cx_active_.dec();
  // If the underlying socket is no longer attached, it means that it has been transferred to
  // an active connection. In this case, the active connection will decrement the number
  // of listener connections.
  // TODO(mattklein123): In general the way we account for the number of listener connections
  // is incredibly fragile. Revisit this by potentially merging ActiveTcpSocket and
  // ActiveTcpConnection, having a shared object which does accounting (but would require
  // another allocation, etc.).
  if (socket_ != nullptr) {
    listener_.decNumConnections();
  }
}

Event::Dispatcher& ActiveTcpSocket::dispatcher() { return listener_.dispatcher(); }

void ActiveTcpSocket::onTimeout() {
  listener_.stats_.downstream_pre_cx_timeout_.inc();
  ASSERT(inserted());
  ENVOY_LOG(debug, "listener filter times out after {} ms",
            listener_.listener_filters_timeout_.count());