1 // Copyright (c) 2012 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/paced_sender.h"
9 #include "net/quic/quic_protocol.h"
13 // To prevent too aggressive pacing we allow the following packet burst size.
14 const int64 kMinPacketBurstSize = 2;
15 // Max estimated time between calls to TimeUntilSend and
16 // AvailableCongestionWindow.
17 const int64 kMaxSchedulingDelayUs = 2000;
19 PacedSender::PacedSender(QuicBandwidth estimate, QuicByteCount max_segment_size)
20 : leaky_bucket_(estimate),
22 max_segment_size_(kDefaultMaxPacketSize) {
25 void PacedSender::set_max_segment_size(QuicByteCount max_segment_size) {
26 max_segment_size_ = max_segment_size;
29 void PacedSender::UpdateBandwidthEstimate(QuicTime now,
30 QuicBandwidth estimate) {
31 leaky_bucket_.SetDrainingRate(now, estimate);
35 void PacedSender::OnPacketSent(QuicTime now, QuicByteCount bytes) {
36 leaky_bucket_.Add(now, bytes);
39 QuicTime::Delta PacedSender::TimeUntilSend(QuicTime now,
40 QuicTime::Delta time_until_send) {
41 if (time_until_send.ToMicroseconds() >= kMaxSchedulingDelayUs) {
42 return time_until_send;
45 QuicByteCount pacing_window = pace_.ToBytesPerPeriod(
46 QuicTime::Delta::FromMicroseconds(kMaxSchedulingDelayUs));
47 QuicByteCount min_window_size = kMinPacketBurstSize * max_segment_size_;
48 pacing_window = std::max(pacing_window, min_window_size);
50 if (pacing_window > leaky_bucket_.BytesPending(now)) {
51 // We have not filled our pacing window yet.
52 return time_until_send;
54 return leaky_bucket_.TimeRemaining(now);