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

Code before:
    return Network::FilterStatus::StopIteration;
  }

  ENVOY_CONN_LOG(debug, "Creating connection to cluster {}", read_callbacks_->connection(),
                 cluster_name);
  if (!initial_upstream_connection_start_time_.has_value()) {
    initial_upstream_connection_start_time_.emplace(
        read_callbacks_->connection().dispatcher().timeSource().monotonicTime());
  }

  const Upstream::ClusterInfoConstSharedPtr& cluster = thread_local_cluster->info();
  getStreamInfo().setUpstreamClusterInfo(cluster);

  // Check this here because the TCP conn pool will queue our request waiting for a connection that
  // will never be released.
  if (!cluster->resourceManager(Upstream::ResourcePriority::Default).connections().canCreate()) {
    getStreamInfo().setResponseFlag(StreamInfo::CoreResponseFlag::UpstreamOverflow);
    cluster->trafficStats()->upstream_cx_overflow_.inc();
    onInitFailure(UpstreamFailureReason::ResourceLimitExceeded);
    return Network::FilterStatus::StopIteration;
  }

  auto& downstream_connection = read_callbacks_->connection();
  auto& filter_state = downstream_connection.streamInfo().filterState();

  const auto* existing_state = filter_state->getDataReadOnly<Network::ProxyProtocolFilterState>(
      Network::ProxyProtocolFilterState::key());

  if (existing_state == nullptr) {
    // No downstream proxy protocol state exists - create new state with tcp_proxy TLVs.
    const auto tlvs = config_->sharedConfig()->evaluateDynamicTLVs(getStreamInfo());
    filter_state->setData(
        Network::ProxyProtocolFilterState::key(),
        std::make_shared<Network::ProxyProtocolFilterState>(Network::ProxyProtocolData{
            downstream_connection.connectionInfoProvider().remoteAddress(),
            downstream_connection.connectionInfoProvider().localAddress(), tlvs}),
        StreamInfo::FilterState::StateType::ReadOnly,
        StreamInfo::FilterState::LifeSpan::Connection);
  } else if (config_->sharedConfig()->proxyProtocolTlvMergePolicy() !=
             envoy::extensions::filters::network::tcp_proxy::v3::ADD_IF_ABSENT) {
    // Existing state found and merge policy is not ADD_IF_ABSENT - merge TLVs.
    auto* mutable_state = filter_state->getDataMutable<Network::ProxyProtocolFilterState>(
        Network::ProxyProtocolFilterState::key());
    const auto& existing_data = mutable_state->value();
    const auto configured_tlvs = config_->sharedConfig()->evaluateDynamicTLVs(getStreamInfo());

    Network::ProxyProtocolTLVVector merged_tlvs;

    if (config_->sharedConfig()->proxyProtocolTlvMergePolicy() ==
        envoy::extensions::filters::network::tcp_proxy::v3::OVERWRITE_BY_TYPE_IF_EXISTS_OR_ADD) {
      // Overwrite by type: configured TLVs take precedence for matching types.

Review comment (suggestion):
```suggestion
  auto* existing_state = filter_state->getDataMutable<Network::ProxyProtocolFilterState>(
```

Code after:
    return Network::FilterStatus::StopIteration;
  }

  ENVOY_CONN_LOG(debug, "Creating connection to cluster {}", read_callbacks_->connection(),
                 cluster_name);
  if (!initial_upstream_connection_start_time_.has_value()) {
    initial_upstream_connection_start_time_.emplace(
        read_callbacks_->connection().dispatcher().timeSource().monotonicTime());
  }

  const Upstream::ClusterInfoConstSharedPtr& cluster = thread_local_cluster->info();
  getStreamInfo().setUpstreamClusterInfo(cluster);

  // Check this here because the TCP conn pool will queue our request waiting for a connection that
  // will never be released.
  if (!cluster->resourceManager(Upstream::ResourcePriority::Default).connections().canCreate()) {
    getStreamInfo().setResponseFlag(StreamInfo::CoreResponseFlag::UpstreamOverflow);
    cluster->trafficStats()->upstream_cx_overflow_.inc();
    onInitFailure(UpstreamFailureReason::ResourceLimitExceeded);
    return Network::FilterStatus::StopIteration;
  }

  auto& downstream_connection = read_callbacks_->connection();
  auto& filter_state = downstream_connection.streamInfo().filterState();

  auto* existing_state = filter_state->getDataMutable<Network::ProxyProtocolFilterState>(
      Network::ProxyProtocolFilterState::key());

  if (existing_state == nullptr) {
    // No downstream proxy protocol state exists - create new state with tcp_proxy TLVs.
    const auto tlvs = config_->sharedConfig()->evaluateDynamicTLVs(getStreamInfo());
    filter_state->setData(
        Network::ProxyProtocolFilterState::key(),
        std::make_shared<Network::ProxyProtocolFilterState>(Network::ProxyProtocolData{
            downstream_connection.connectionInfoProvider().remoteAddress(),
            downstream_connection.connectionInfoProvider().localAddress(), tlvs}),
        StreamInfo::FilterState::StateType::Mutable, StreamInfo::FilterState::LifeSpan::Connection);
  } else if (config_->sharedConfig()->proxyProtocolTlvMergePolicy() !=
             envoy::extensions::filters::network::tcp_proxy::v3::ADD_IF_ABSENT) {
    // Existing state found and merge policy is not ADD_IF_ABSENT - merge TLVs.
    const auto& existing_data = existing_state->value();
    const auto configured_tlvs = config_->sharedConfig()->evaluateDynamicTLVs(getStreamInfo());

    Network::ProxyProtocolTLVVector merged_tlvs;

    if (config_->sharedConfig()->proxyProtocolTlvMergePolicy() ==
        envoy::extensions::filters::network::tcp_proxy::v3::OVERWRITE_BY_TYPE_IF_EXISTS_OR_ADD) {
      // Overwrite by type: configured TLVs take precedence for matching types.
      absl::flat_hash_set<uint8_t> configured_tlv_types;
      for (const auto& tlv : configured_tlvs) {
        configured_tlv_types.insert(tlv.type);