• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 QUIC_EXPORT_PRIVATE 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_alarm_granularity(QuicTime::Delta alarm_granularity)47   void set_alarm_granularity(QuicTime::Delta alarm_granularity) {
48     alarm_granularity_ = alarm_granularity;
49   }
50 
max_pacing_rate()51   QuicBandwidth max_pacing_rate() const { return max_pacing_rate_; }
52 
53   void OnCongestionEvent(bool rtt_updated, QuicByteCount bytes_in_flight,
54                          QuicTime event_time,
55                          const AckedPacketVector& acked_packets,
56                          const LostPacketVector& lost_packets,
57                          QuicPacketCount num_ect, QuicPacketCount num_ce);
58 
59   void OnPacketSent(QuicTime sent_time, QuicByteCount bytes_in_flight,
60                     QuicPacketNumber packet_number, QuicByteCount bytes,
61                     HasRetransmittableData has_retransmittable_data);
62 
63   // Called when application throttles the sending, so that pacing sender stops
64   // making up for lost time.
65   void OnApplicationLimited();
66 
67   // Set burst_tokens_ and initial_burst_size_.
68   void SetBurstTokens(uint32_t burst_tokens);
69 
70   QuicTime::Delta TimeUntilSend(QuicTime now,
71                                 QuicByteCount bytes_in_flight) const;
72 
73   QuicBandwidth PacingRate(QuicByteCount bytes_in_flight) const;
74 
GetNextReleaseTime()75   NextReleaseTimeResult GetNextReleaseTime() const {
76     bool allow_burst = (burst_tokens_ > 0 || lumpy_tokens_ > 0);
77     return {ideal_next_packet_send_time_, allow_burst};
78   }
79 
initial_burst_size()80   uint32_t initial_burst_size() const { return initial_burst_size_; }
81 
82  protected:
lumpy_tokens()83   uint32_t lumpy_tokens() const { return lumpy_tokens_; }
84 
85  private:
86   friend class test::QuicSentPacketManagerPeer;
87 
88   // Underlying sender. Not owned.
89   SendAlgorithmInterface* sender_;
90   // If not QuicBandidth::Zero, the maximum rate the PacingSender will use.
91   QuicBandwidth max_pacing_rate_;
92 
93   // Number of unpaced packets to be sent before packets are delayed.
94   uint32_t burst_tokens_;
95   QuicTime ideal_next_packet_send_time_;  // When can the next packet be sent.
96   uint32_t initial_burst_size_;
97 
98   // Number of unpaced packets to be sent before packets are delayed. This token
99   // is consumed after burst_tokens_ ran out.
100   uint32_t lumpy_tokens_;
101 
102   // If the next send time is within alarm_granularity_, send immediately.
103   // TODO(fayang): Remove alarm_granularity_ when deprecating
104   // quic_offload_pacing_to_usps2 flag.
105   QuicTime::Delta alarm_granularity_;
106 
107   // Indicates whether pacing throttles the sending. If true, make up for lost
108   // time.
109   bool pacing_limited_;
110 };
111 
112 }  // namespace quic
113 
114 #endif  // QUICHE_QUIC_CORE_CONGESTION_CONTROL_PACING_SENDER_H_
115