We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / net / quic / congestion_control / pacing_sender.cc
blob54c97bf6f79c06b67791ed354de9124e4019f185
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 Perspective perspective) {
25 sender_->SetFromConfig(config, perspective);
28 bool PacingSender::ResumeConnectionState(
29 const CachedNetworkParameters& cached_network_params,
30 bool max_bandwidth_resumption) {
31 return sender_->ResumeConnectionState(cached_network_params,
32 max_bandwidth_resumption);
35 void PacingSender::SetNumEmulatedConnections(int num_connections) {
36 sender_->SetNumEmulatedConnections(num_connections);
39 void PacingSender::SetMaxCongestionWindow(QuicByteCount max_congestion_window) {
40 sender_->SetMaxCongestionWindow(max_congestion_window);
43 void PacingSender::OnCongestionEvent(bool rtt_updated,
44 QuicByteCount bytes_in_flight,
45 const CongestionVector& acked_packets,
46 const CongestionVector& lost_packets) {
47 sender_->OnCongestionEvent(
48 rtt_updated, bytes_in_flight, acked_packets, lost_packets);
51 bool PacingSender::OnPacketSent(
52 QuicTime sent_time,
53 QuicByteCount bytes_in_flight,
54 QuicPacketSequenceNumber sequence_number,
55 QuicByteCount bytes,
56 HasRetransmittableData has_retransmittable_data) {
57 const bool in_flight =
58 sender_->OnPacketSent(sent_time, bytes_in_flight, sequence_number,
59 bytes, has_retransmittable_data);
60 if (has_retransmittable_data != HAS_RETRANSMITTABLE_DATA) {
61 return in_flight;
63 if (bytes_in_flight == 0) {
64 // Add more burst tokens anytime the connection is leaving quiescence.
65 burst_tokens_ = initial_packet_burst_;
67 if (burst_tokens_ > 0) {
68 --burst_tokens_;
69 was_last_send_delayed_ = false;
70 last_delayed_packet_sent_time_ = QuicTime::Zero();
71 ideal_next_packet_send_time_ = QuicTime::Zero();
72 return in_flight;
74 // The next packet should be sent as soon as the current packets has been
75 // transferred.
76 QuicTime::Delta delay = PacingRate().TransferTime(bytes);
77 // If the last send was delayed, and the alarm took a long time to get
78 // invoked, allow the connection to make up for lost time.
79 if (was_last_send_delayed_) {
80 ideal_next_packet_send_time_ = ideal_next_packet_send_time_.Add(delay);
81 // The send was application limited if it takes longer than the
82 // pacing delay between sent packets.
83 const bool application_limited =
84 last_delayed_packet_sent_time_.IsInitialized() &&
85 sent_time > last_delayed_packet_sent_time_.Add(delay);
86 const bool making_up_for_lost_time =
87 ideal_next_packet_send_time_ <= sent_time;
88 // As long as we're making up time and not application limited,
89 // continue to consider the packets delayed, allowing the packets to be
90 // sent immediately.
91 if (making_up_for_lost_time && !application_limited) {
92 last_delayed_packet_sent_time_ = sent_time;
93 } else {
94 was_last_send_delayed_ = false;
95 last_delayed_packet_sent_time_ = QuicTime::Zero();
97 } else {
98 ideal_next_packet_send_time_ = QuicTime::Max(
99 ideal_next_packet_send_time_.Add(delay), sent_time.Add(delay));
101 return in_flight;
104 void PacingSender::OnRetransmissionTimeout(bool packets_retransmitted) {
105 sender_->OnRetransmissionTimeout(packets_retransmitted);
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