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

Code before:
  // Handle TLS handshake completion for connections where we're waiting for it.
  if (event == Network::ConnectionEvent::Connected && waiting_for_tls_handshake_) {
    // The Connected event for SSL connections is fired after TLS handshake completes.
    ENVOY_CONN_LOG(debug, "downstream TLS handshake completed via Connected event",
                   read_callbacks_->connection());
    onDownstreamTlsHandshakeComplete();
    return;
  }

  if (event == Network::ConnectionEvent::LocalClose ||
      event == Network::ConnectionEvent::RemoteClose) {
    downstream_closed_ = true;
    // Cancel the potential odcds callback.
    cluster_discovery_handle_ = nullptr;
  }

  ENVOY_CONN_LOG(trace, "on downstream event {}, has upstream = {}", read_callbacks_->connection(),
                 static_cast<int>(event), upstream_ != nullptr);

  if (upstream_) {
    absl::string_view downstream_local_close_reason =
        idle_timeout_triggered_
            ? absl::string_view(StreamInfo::LocalCloseReasons::get().TcpSessionIdleTimeout)
            : "";
    Tcp::ConnectionPool::ConnectionDataPtr conn_data(
        upstream_->onDownstreamEvent(event, downstream_local_close_reason));
    if (conn_data != nullptr &&
        conn_data->connection().state() != Network::Connection::State::Closed) {
      config_->drainManager().add(config_->sharedConfig(), std::move(conn_data),
                                  std::move(upstream_callbacks_), std::move(idle_timer_),
                                  idle_timeout_, read_callbacks_->upstreamHost());
    }
    if (event == Network::ConnectionEvent::LocalClose ||
        event == Network::ConnectionEvent::RemoteClose) {
      upstream_.reset();
      disableIdleTimer();
    }
  }

  if (generic_conn_pool_) {
    if (event == Network::ConnectionEvent::LocalClose ||
        event == Network::ConnectionEvent::RemoteClose) {
      // Cancel the conn pool request and close any excess pending requests.
      generic_conn_pool_.reset();
    }
  }
}

void Filter::onUpstreamData(Buffer::Instance& data, bool end_stream) {
  ENVOY_CONN_LOG(trace, "upstream connection received {} bytes, end_stream={}",
                 read_callbacks_->connection(), data.length(), end_stream);

Review comment (suggestion):
Conceptually, I think using `read_callbacks_->connection().localCloseReason()` here makes more sense, and the above `setLocalCloseReason` is not used in the Envoy repo, can we make these lines more generic? `idle_timeout_triggered_` can be avoided as well.

Code after:
  // Handle TLS handshake completion for connections where we're waiting for it.
  if (event == Network::ConnectionEvent::Connected && waiting_for_tls_handshake_) {
    // The Connected event for SSL connections is fired after TLS handshake completes.
    ENVOY_CONN_LOG(debug, "downstream TLS handshake completed via Connected event",
                   read_callbacks_->connection());
    onDownstreamTlsHandshakeComplete();
    return;
  }

  if (event == Network::ConnectionEvent::LocalClose ||
      event == Network::ConnectionEvent::RemoteClose) {
    downstream_closed_ = true;
    // Cancel the potential odcds callback.
    cluster_discovery_handle_ = nullptr;
  }

  ENVOY_CONN_LOG(trace, "on downstream event {}, has upstream = {}", read_callbacks_->connection(),
                 static_cast<int>(event), upstream_ != nullptr);

  if (upstream_) {
    absl::string_view downstream_close_details = read_callbacks_->connection().localCloseReason();
    Tcp::ConnectionPool::ConnectionDataPtr conn_data(
        upstream_->onDownstreamEvent(event, downstream_close_details));
    if (conn_data != nullptr &&
        conn_data->connection().state() != Network::Connection::State::Closed) {
      config_->drainManager().add(config_->sharedConfig(), std::move(conn_data),
                                  std::move(upstream_callbacks_), std::move(idle_timer_),
                                  idle_timeout_, read_callbacks_->upstreamHost());
    }
    if (event == Network::ConnectionEvent::LocalClose ||
        event == Network::ConnectionEvent::RemoteClose) {
      upstream_.reset();
      disableIdleTimer();
    }
  }

  if (generic_conn_pool_) {
    if (event == Network::ConnectionEvent::LocalClose ||
        event == Network::ConnectionEvent::RemoteClose) {
      // Cancel the conn pool request and close any excess pending requests.
      generic_conn_pool_.reset();
    }
  }
}

void Filter::onUpstreamData(Buffer::Instance& data, bool end_stream) {
  ENVOY_CONN_LOG(trace, "upstream connection received {} bytes, end_stream={}",
                 read_callbacks_->connection(), data.length(), end_stream);
  getStreamInfo().getUpstreamBytesMeter()->addWireBytesReceived(data.length());
  getStreamInfo().getDownstreamBytesMeter()->addWireBytesSent(data.length());
  read_callbacks_->connection().write(data, end_stream);