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

Code before:
  position_.emplace(connection_map, filter_chain, position);
}

quic::QuicSSLConfig EnvoyQuicServerSession::GetSSLConfig() const {
  quic::QuicSSLConfig config = quic::QuicServerSessionBase::GetSSLConfig();
  config.early_data_enabled = position_.has_value()
                                  ? dynamic_cast<const QuicServerTransportSocketFactory&>(
                                        position_->filter_chain_.transportSocketFactory())
                                        .earlyDataEnabled()
                                  : true;
  return config;
}

void EnvoyQuicServerSession::ProcessUdpPacket(const quic::QuicSocketAddress& self_address,
                                              const quic::QuicSocketAddress& peer_address,
                                              const quic::QuicReceivedPacket& packet) {
  // If L4 filters causes the connection to be closed early during initialization, now
  // is the time to actually close the connection.
  maybeHandleCloseDuringInitialize();

  if (should_send_go_away_and_close_on_dispatch_ != nullptr &&
      should_send_go_away_and_close_on_dispatch_->shouldShedLoad()) {
    ENVOY_LOG_EVERY_POW_2(info, "EnvoyQuicServerSession::ProcessUdpPacket: "
                                "sending GOAWAY and close on dispatch");
    SendHttp3GoAway(quic::QUIC_PEER_GOING_AWAY, "Server overloaded");
    h3_go_away_sent_ = true;
    closeConnectionImmediately();
  }
  if (should_send_go_away_on_dispatch_ != nullptr &&
      should_send_go_away_on_dispatch_->shouldShedLoad() && !h3_go_away_sent_) {
    ENVOY_LOG_EVERY_POW_2(info, "EnvoyQuicServerSession::ProcessUdpPacket: "
                                "sending GOAWAY on dispatch");
    SendHttp3GoAway(quic::QUIC_PEER_GOING_AWAY, "Server overloaded");
    h3_go_away_sent_ = true;
  }

  quic::QuicServerSessionBase::ProcessUdpPacket(self_address, peer_address, packet);
  if (connection()->expected_server_preferred_address().IsInitialized() &&
      self_address == connection()->expected_server_preferred_address()) {
    connection_stats_.num_packets_rx_on_preferred_address_.inc();
  }
  maybeApplyDelayedClose();
}

std::vector<absl::string_view>::const_iterator
EnvoyQuicServerSession::SelectAlpn(const std::vector<absl::string_view>& alpns) const {
  if (!position_.has_value()) {
    return quic::QuicServerSessionBase::SelectAlpn(alpns);
  }
  const std::vector<absl::string_view>& configured_alpns =
      dynamic_cast<const QuicServerTransportSocketFactory&>(

Review comment (suggestion):
Is it possible for this block to be called multuple times, or is there a side-effect which prevents that? I wonder if we should also check `!h3_go_away_sent_` just to be safe.

Code after:
  position_.emplace(connection_map, filter_chain, position);
}

quic::QuicSSLConfig EnvoyQuicServerSession::GetSSLConfig() const {
  quic::QuicSSLConfig config = quic::QuicServerSessionBase::GetSSLConfig();
  config.early_data_enabled = position_.has_value()
                                  ? dynamic_cast<const QuicServerTransportSocketFactory&>(
                                        position_->filter_chain_.transportSocketFactory())
                                        .earlyDataEnabled()
                                  : true;
  return config;
}

void EnvoyQuicServerSession::ProcessUdpPacket(const quic::QuicSocketAddress& self_address,
                                              const quic::QuicSocketAddress& peer_address,
                                              const quic::QuicReceivedPacket& packet) {
  // If L4 filters causes the connection to be closed early during initialization, now
  // is the time to actually close the connection.
  maybeHandleCloseDuringInitialize();

  if (should_send_go_away_and_close_on_dispatch_ != nullptr &&
      should_send_go_away_and_close_on_dispatch_->shouldShedLoad()) {
    ENVOY_LOG_EVERY_POW_2(info, "EnvoyQuicServerSession::ProcessUdpPacket: "
                                "sending GOAWAY and close on dispatch");
    SendHttp3GoAway(quic::QUIC_PEER_GOING_AWAY, "Server overloaded");
    closeConnectionImmediately();
  } else if (should_send_go_away_on_dispatch_ != nullptr &&
             should_send_go_away_on_dispatch_->shouldShedLoad() && !h3_go_away_sent_) {
    ENVOY_LOG_EVERY_POW_2(info, "EnvoyQuicServerSession::ProcessUdpPacket: "
                                "sending GOAWAY on dispatch");
    SendHttp3GoAway(quic::QUIC_PEER_GOING_AWAY, "Server overloaded");
    h3_go_away_sent_ = true;
  }

  quic::QuicServerSessionBase::ProcessUdpPacket(self_address, peer_address, packet);
  if (connection()->expected_server_preferred_address().IsInitialized() &&
      self_address == connection()->expected_server_preferred_address()) {
    connection_stats_.num_packets_rx_on_preferred_address_.inc();
  }
  maybeApplyDelayedClose();
}

std::vector<absl::string_view>::const_iterator
EnvoyQuicServerSession::SelectAlpn(const std::vector<absl::string_view>& alpns) const {
  if (!position_.has_value()) {
    return quic::QuicServerSessionBase::SelectAlpn(alpns);
  }
  const std::vector<absl::string_view>& configured_alpns =
      dynamic_cast<const QuicServerTransportSocketFactory&>(
          position_->filter_chain_.transportSocketFactory())
          .supportedAlpnProtocols();