Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / net / quic / congestion_control / pacing_sender.cc
blob62c64ea74d68095e86647f89ae89461ecf323c85
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.
5 #include "net/quic/congestion_control/pacing_sender.h"
7 namespace net {
9 PacingSender::PacingSender(SendAlgorithmInterface* sender,
10 QuicTime::Delta alarm_granularity,
11 uint32 initial_packet_burst)
12 : sender_(sender),
13 alarm_granularity_(alarm_granularity),
14 initial_packet_burst_(initial_packet_burst),
15 burst_tokens_(initial_packet_burst),
16 last_delayed_packet_sent_time_(QuicTime::Zero()),
17 ideal_next_packet_send_time_(QuicTime::Zero()),
18 was_last_send_delayed_(false) {
21 PacingSender::~PacingSender() {}
23 void PacingSender::SetFromConfig(const QuicConfig& config,
24 bool is_server,
25 bool using_pacing) {
26 DCHECK(using_pacing);
27 sender_->SetFromConfig(config, is_server, using_pacing);
30 bool PacingSender::ResumeConnectionState(
31 const CachedNetworkParameters& cached_network_params) {
32 return sender_->ResumeConnectionState(cached_network_params);
35 void PacingSender::SetNumEmulatedConnections(int num_connections) {
36 sender_->SetNumEmulatedConnections(num_connections);
39 void PacingSender::OnCongestionEvent(bool rtt_updated,
40 QuicByteCount bytes_in_flight,
41 const CongestionVector& acked_packets,
42 const CongestionVector& lost_packets) {
43 sender_->OnCongestionEvent(
44 rtt_updated, bytes_in_flight, acked_packets, lost_packets);
47 bool PacingSender::OnPacketSent(
48 QuicTime sent_time,
49 QuicByteCount bytes_in_flight,
50 QuicPacketSequenceNumber sequence_number,
51 QuicByteCount bytes,
52 HasRetransmittableData has_retransmittable_data) {
53 const bool in_flight =
54 sender_->OnPacketSent(sent_time, bytes_in_flight, sequence_number,
55 bytes, has_retransmittable_data);
56 if (has_retransmittable_data != HAS_RETRANSMITTABLE_DATA) {
57 return in_flight;
59 if (bytes_in_flight == 0) {
60 // Add more burst tokens anytime the connection is leaving quiescence.
61 burst_tokens_ = initial_packet_burst_;
63 if (burst_tokens_ > 0) {
64 --burst_tokens_;
65 was_last_send_delayed_ = false;
66 last_delayed_packet_sent_time_ = QuicTime::Zero();
67 ideal_next_packet_send_time_ = QuicTime::Zero();
68 return in_flight;
70 // The next packet should be sent as soon as the current packets has been
71 // transferred.
72 QuicTime::Delta delay = PacingRate().TransferTime(bytes);
73 // If the last send was delayed, and the alarm took a long time to get
74 // invoked, allow the connection to make up for lost time.
75 if (was_last_send_delayed_) {
76 ideal_next_packet_send_time_ = ideal_next_packet_send_time_.Add(delay);
77 // The send was application limited if it takes longer than the
78 // pacing delay between sent packets.
79 const bool application_limited =
80 last_delayed_packet_sent_time_.IsInitialized() &&
81 sent_time > last_delayed_packet_sent_time_.Add(delay);
82 const bool making_up_for_lost_time =
83 ideal_next_packet_send_time_ <= sent_time;
84 // As long as we're making up time and not application limited,
85 // continue to consider the packets delayed, allowing the packets to be
86 // sent immediately.
87 if (making_up_for_lost_time && !application_limited) {
88 last_delayed_packet_sent_time_ = sent_time;
89 } else {
90 was_last_send_delayed_ = false;
91 last_delayed_packet_sent_time_ = QuicTime::Zero();
93 } else {
94 ideal_next_packet_send_time_ = QuicTime::Max(
95 ideal_next_packet_send_time_.Add(delay), sent_time.Add(delay));
97 return in_flight;
100 void PacingSender::OnRetransmissionTimeout(bool packets_retransmitted) {
101 sender_->OnRetransmissionTimeout(packets_retransmitted);
104 void PacingSender::RevertRetransmissionTimeout() {
105 sender_->RevertRetransmissionTimeout();
108 QuicTime::Delta PacingSender::TimeUntilSend(
109 QuicTime now,
110 QuicByteCount bytes_in_flight,
111 HasRetransmittableData has_retransmittable_data) const {
112 QuicTime::Delta time_until_send =
113 sender_->TimeUntilSend(now, bytes_in_flight, has_retransmittable_data);
114 if (burst_tokens_ > 0 || bytes_in_flight == 0) {
115 // Don't pace if we have burst tokens available or leaving quiescence.
116 return time_until_send;
119 if (!time_until_send.IsZero()) {
120 DCHECK(time_until_send.IsInfinite());
121 // The underlying sender prevents sending.
122 return time_until_send;
125 if (has_retransmittable_data == NO_RETRANSMITTABLE_DATA) {
126 // Don't pace ACK packets, since they do not count against CWND and do not
127 // cause CWND to grow.
128 return QuicTime::Delta::Zero();
131 // If the next send time is within the alarm granularity, send immediately.
132 if (ideal_next_packet_send_time_ > now.Add(alarm_granularity_)) {
133 DVLOG(1) << "Delaying packet: "
134 << ideal_next_packet_send_time_.Subtract(now).ToMicroseconds();
135 was_last_send_delayed_ = true;
136 return ideal_next_packet_send_time_.Subtract(now);
139 DVLOG(1) << "Sending packet now";
140 return QuicTime::Delta::Zero();
143 QuicBandwidth PacingSender::PacingRate() const {
144 return sender_->PacingRate();
147 QuicBandwidth PacingSender::BandwidthEstimate() const {
148 return sender_->BandwidthEstimate();
151 bool PacingSender::HasReliableBandwidthEstimate() const {
152 return sender_->HasReliableBandwidthEstimate();
155 QuicTime::Delta PacingSender::RetransmissionDelay() const {
156 return sender_->RetransmissionDelay();
159 QuicByteCount PacingSender::GetCongestionWindow() const {
160 return sender_->GetCongestionWindow();
163 bool PacingSender::InSlowStart() const {
164 return sender_->InSlowStart();
167 bool PacingSender::InRecovery() const {
168 return sender_->InRecovery();
171 QuicByteCount PacingSender::GetSlowStartThreshold() const {
172 return sender_->GetSlowStartThreshold();
175 CongestionControlType PacingSender::GetCongestionControlType() const {
176 return sender_->GetCongestionControlType();
179 } // namespace net