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

Code before:
      absl::flat_hash_set<uint8_t> tcp_proxy_tlv_types;
      for (const auto& tlv : tcp_proxy_tlvs) {
        tcp_proxy_tlv_types.insert(tlv.type);
      }

      // Add all tcp_proxy TLVs first (they take precedence).
      for (const auto& tlv : tcp_proxy_tlvs) {
        merged_tlvs.push_back(tlv);
      }

      // Add downstream TLVs that don't have the same type as tcp_proxy TLVs.
      // Note: We preserve all downstream TLVs with non-conflicting types, including duplicates.
      for (const auto& tlv : existing_data.tlv_vector_) {
        if (!tcp_proxy_tlv_types.contains(tlv.type)) {
          merged_tlvs.push_back(tlv);
        }
      }
    } else if (config_->sharedConfig()->downstreamTlvMergePolicy() ==
               envoy::extensions::filters::network::tcp_proxy::v3::APPEND_TO_DOWNSTREAM_TLVS) {
      // Append tcp_proxy TLVs to downstream TLVs.
      // Preserve all downstream TLVs.
      for (const auto& tlv : existing_data.tlv_vector_) {
        merged_tlvs.push_back(tlv);
      }

      // Append all tcp_proxy TLVs.
      for (const auto& tlv : tcp_proxy_tlvs) {
        merged_tlvs.push_back(tlv);
      }
    }

    // Update filter state with merged TLVs, preserving downstream addresses and version.
    // Use Mutable state type to match the existing state (set by proxy_protocol listener filter).
    filter_state->setData(
        Network::ProxyProtocolFilterState::key(),
        std::make_shared<Network::ProxyProtocolFilterState>(Network::ProxyProtocolDataWithVersion{
            {existing_data.src_addr_, existing_data.dst_addr_, merged_tlvs},
            existing_data.version_}),
        StreamInfo::FilterState::StateType::Mutable, StreamInfo::FilterState::LifeSpan::Connection);
  }
  // else: Downstream state exists but merge is disabled (KEEP_DOWNSTREAM_ONLY) - keep existing
  // downstream state as-is.
  transport_socket_options_ =
      Network::TransportSocketOptionsUtility::fromFilterState(*filter_state);

  if (auto typed_state = filter_state->getDataReadOnly<Network::UpstreamSocketOptionsFilterState>(
          Network::UpstreamSocketOptionsFilterState::key());
      typed_state != nullptr) {
    auto downstream_options = typed_state->value();
    if (!upstream_options_) {
      upstream_options_ = std::make_shared<Network::Socket::Options>();

Review comment (suggestion):
You should just get the data as mutable at the top of this block, instead of getting it readonly and then setting it here.

Code after:
        configured_tlv_types.insert(tlv.type);
      }

      for (const auto& tlv : configured_tlvs) {
        merged_tlvs.push_back(tlv);
      }

      for (const auto& tlv : existing_data.tlv_vector_) {
        if (!configured_tlv_types.contains(tlv.type)) {
          merged_tlvs.push_back(tlv);
        }
      }
    } else if (config_->sharedConfig()->proxyProtocolTlvMergePolicy() ==
               envoy::extensions::filters::network::tcp_proxy::v3::APPEND_IF_EXISTS_OR_ADD) {
      // Append: preserve all existing TLVs, then add configured TLVs.
      for (const auto& tlv : existing_data.tlv_vector_) {
        merged_tlvs.push_back(tlv);
      }

      for (const auto& tlv : configured_tlvs) {
        merged_tlvs.push_back(tlv);
      }
    }

    // Update filter state with merged TLVs, preserving existing addresses and version.
    filter_state->setData(
        Network::ProxyProtocolFilterState::key(),
        std::make_shared<Network::ProxyProtocolFilterState>(Network::ProxyProtocolDataWithVersion{
            {existing_data.src_addr_, existing_data.dst_addr_, merged_tlvs},
            existing_data.version_}),
        StreamInfo::FilterState::StateType::Mutable, StreamInfo::FilterState::LifeSpan::Connection);
  }
  // else: ADD_IF_ABSENT policy with existing state - keep existing state as-is.
  transport_socket_options_ =
      Network::TransportSocketOptionsUtility::fromFilterState(*filter_state);

  if (auto typed_state = filter_state->getDataReadOnly<Network::UpstreamSocketOptionsFilterState>(
          Network::UpstreamSocketOptionsFilterState::key());
      typed_state != nullptr) {
    auto downstream_options = typed_state->value();
    if (!upstream_options_) {
      upstream_options_ = std::make_shared<Network::Socket::Options>();
    }
    Network::Socket::appendOptions(upstream_options_, downstream_options);
  }

  if (!maybeTunnel(*thread_local_cluster)) {
    // Either cluster is unknown, factory doesn't exist, there are no healthy hosts, or
    // createGenericConnPool returned nullptr. Handle this gracefully.
    getStreamInfo().setResponseFlag(StreamInfo::CoreResponseFlag::NoHealthyUpstream);
    onInitFailure(UpstreamFailureReason::NoHealthyUpstream);