Repository: envoyproxy/envoy
File: mobile/library/common/extensions/quic_packet_writer/platform/platform_packet_writer_factory.cc (C++)

Code before:
#include "library/common/extensions/quic_packet_writer/platform/platform_packet_writer_factory.h"

#include <chrono>
#include <iostream>

#include "source/common/network/udp_packet_writer_handler_impl.h"
#include "source/common/quic/envoy_quic_packet_writer.h"
#include "source/common/quic/envoy_quic_utils.h"

#include "library/common/system/system_helper.h"

namespace Envoy {
namespace Quic {

constexpr size_t MaxRetries =
    12; // Max retries for ENOBUFS error. Same as Chromium value
        // (https://source.chromium.org/chromium/chromium/src/+/main:net/quic/quic_chromium_packet_writer.cc;l=34;bpv=1;bpt=0).

class RetriablePacketWriter : public Network::UdpDefaultWriter {
public:
  RetriablePacketWriter(Event::Dispatcher& dispatcher, Network::IoHandle& io_handle)
      : Network::UdpDefaultWriter(io_handle), dispatcher_(dispatcher) {}

  Api::IoCallUint64Result writePacket(const Buffer::Instance& buffer,
                                      const Network::Address::Ip* local_ip,
                                      const Network::Address::Instance& peer_address) override {
    Api::IoCallUint64Result result =
        Network::UdpDefaultWriter::writePacket(buffer, local_ip, peer_address);
    if (result.ok()) {
      // Reset retry count on successful write.
      retry_count_ = 0;
    } else if (result.err_->getErrorCode() == Api::IoError::IoErrorCode::NoBufferSpace &&
               retry_count_ < MaxRetries) {
      // Override the error to EAGAIN and treat it as a write block signal.
      ENVOY_LOG_MISC(debug, "Encountered ENOBUFS, will retry");
      setBlocked();
      if (retry_timer_ == nullptr) {
        retry_timer_ = dispatcher_.createTimer([this]() {
          // On timer fire, re-arm a write event to retry sending packets.
          ioHandle().activateFileEvents(Event::FileReadyType::Write);

Review comment (suggestion):
I suggest moving the comment to above the declaration, i.e.

```
// Max retries for ENOBUFS error. Same as Chromium value
        // (https://source.chromium.org/chromium/chromium/src/+/main:net/quic/quic_chromium_packet_writer.cc;l=34;bpv=1;bpt=0).

constexpr size_t MaxRetries = 12;
```


Code after:
#include "library/common/extensions/quic_packet_writer/platform/platform_packet_writer_factory.h"

#include <chrono>
#include <iostream>

#include "source/common/network/udp_packet_writer_handler_impl.h"
#include "source/common/quic/envoy_quic_packet_writer.h"
#include "source/common/quic/envoy_quic_utils.h"

#include "library/common/system/system_helper.h"

namespace Envoy {
namespace Quic {

namespace {

// Max retries for ENOBUFS error. Same as Chromium value
// (https://source.chromium.org/chromium/chromium/src/+/main:net/quic/quic_chromium_packet_writer.cc;l=34;bpv=1;bpt=0).
constexpr size_t MaxRetries = 12;

class RetriablePacketWriter : public Network::UdpDefaultWriter {
public:
  RetriablePacketWriter(Event::Dispatcher& dispatcher, Network::IoHandle& io_handle)
      : Network::UdpDefaultWriter(io_handle), dispatcher_(dispatcher) {}

  Api::IoCallUint64Result writePacket(const Buffer::Instance& buffer,
                                      const Network::Address::Ip* local_ip,
                                      const Network::Address::Instance& peer_address) override {
    Api::IoCallUint64Result result =
        Network::UdpDefaultWriter::writePacket(buffer, local_ip, peer_address);
    if (result.ok()) {
      // Reset retry count on successful write.
      retry_count_ = 0;
    } else if (result.err_->getErrorCode() == Api::IoError::IoErrorCode::NoBufferSpace &&
               retry_count_ < MaxRetries) {
      // Override the error to EAGAIN and treat it as a write block signal.
      ENVOY_LOG_MISC(debug, "Encountered ENOBUFS, will retry");
      setBlocked();
      if (retry_timer_ == nullptr) {
        retry_timer_ = dispatcher_.createTimer([this]() {