xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/congestion_control/pacing_sender.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // A send algorithm that adds pacing on top of an another send algorithm.
6 // It uses the underlying sender's pacing rate to schedule packets.
7 // It also takes into consideration the expected granularity of the underlying
8 // alarm to ensure that alarms are not set too aggressively, and err towards
9 // sending packets too early instead of too late.
10 
11 #ifndef QUICHE_QUIC_CORE_CONGESTION_CONTROL_PACING_SENDER_H_
12 #define QUICHE_QUIC_CORE_CONGESTION_CONTROL_PACING_SENDER_H_
13 
14 #include <cstdint>
15 #include <map>
16 #include <memory>
17 
18 #include "quiche/quic/core/congestion_control/send_algorithm_interface.h"
19 #include "quiche/quic/core/quic_bandwidth.h"
20 #include "quiche/quic/core/quic_config.h"
21 #include "quiche/quic/core/quic_packets.h"
22 #include "quiche/quic/core/quic_time.h"
23 #include "quiche/quic/platform/api/quic_export.h"
24 
25 namespace quic {
26 
27 namespace test {
28 class QuicSentPacketManagerPeer;
29 }  // namespace test
30 
31 class QUICHE_EXPORT PacingSender {
32  public:
33   PacingSender();
34   PacingSender(const PacingSender&) = delete;
35   PacingSender& operator=(const PacingSender&) = delete;
36   ~PacingSender();
37 
38   // Sets the underlying sender. Does not take ownership of |sender|. |sender|
39   // must not be null. This must be called before any of the
40   // SendAlgorithmInterface wrapper methods are called.
41   void set_sender(SendAlgorithmInterface* sender);
42 
set_max_pacing_rate(QuicBandwidth max_pacing_rate)43   void set_max_pacing_rate(QuicBandwidth max_pacing_rate) {
44     max_pacing_rate_ = max_pacing_rate;
45   }
46 
set_remove_non_initial_burst()47   void set_remove_non_initial_burst() { remove_non_initial_burst_ = true; }
48 
max_pacing_rate()49   QuicBandwidth max_pacing_rate() const { return max_pacing_rate_; }
50 
51   void OnCongestionEvent(bool rtt_updated, QuicByteCount bytes_in_flight,
52                          QuicTime event_time,
53                          const AckedPacketVector& acked_packets,
54                          const LostPacketVector& lost_packets,
55                          QuicPacketCount num_ect, QuicPacketCount num_ce);
56 
57   void OnPacketSent(QuicTime sent_time, QuicByteCount bytes_in_flight,
58                     QuicPacketNumber packet_number, QuicByteCount bytes,
59                     HasRetransmittableData has_retransmittable_data);
60 
61   // Called when application throttles the sending, so that pacing sender stops
62   // making up for lost time.
63   void OnApplicationLimited();
64 
65   // Set burst_tokens_ and initial_burst_size_.
66   void SetBurstTokens(uint32_t burst_tokens);
67 
68   QuicTime::Delta TimeUntilSend(QuicTime now,
69                                 QuicByteCount bytes_in_flight) const;
70 
71   QuicBandwidth PacingRate(QuicByteCount bytes_in_flight) const;
72 
GetNextReleaseTime()73   NextReleaseTimeResult GetNextReleaseTime() const {
74     bool allow_burst = (burst_tokens_ > 0 || lumpy_tokens_ > 0);
75     return {ideal_next_packet_send_time_, allow_burst};
76   }
77 
initial_burst_size()78   uint32_t initial_burst_size() const { return initial_burst_size_; }
79 
80  protected:
lumpy_tokens()81   uint32_t lumpy_tokens() const { return lumpy_tokens_; }
82 
83  private:
84   friend class test::QuicSentPacketManagerPeer;
85 
86   // Underlying sender. Not owned.
87   SendAlgorithmInterface* sender_;
88   // If not QuicBandidth::Zero, the maximum rate the PacingSender will use.
89   QuicBandwidth max_pacing_rate_;
90 
91   // Number of unpaced packets to be sent before packets are delayed.
92   uint32_t burst_tokens_;
93   QuicTime ideal_next_packet_send_time_;  // When can the next packet be sent.
94   uint32_t initial_burst_size_;
95 
96   // Number of unpaced packets to be sent before packets are delayed. This token
97   // is consumed after burst_tokens_ ran out.
98   uint32_t lumpy_tokens_;
99 
100   // Indicates whether pacing throttles the sending. If true, make up for lost
101   // time.
102   bool pacing_limited_;
103 
104   bool remove_non_initial_burst_ =
105       GetQuicReloadableFlag(quic_pacing_remove_non_initial_burst);
106 };
107 
108 }  // namespace quic
109 
110 #endif  // QUICHE_QUIC_CORE_CONGESTION_CONTROL_PACING_SENDER_H_
111