| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/congestion_control/leaky_bucket.h" | 5 #include "net/quic/congestion_control/leaky_bucket.h" |
| 6 | 6 |
| 7 #include "base/time.h" | 7 #include "base/time.h" |
| 8 | 8 |
| 9 namespace net { | 9 namespace net { |
| 10 | 10 |
| 11 LeakyBucket::LeakyBucket(const QuicClock* clock, QuicBandwidth draining_rate) | 11 LeakyBucket::LeakyBucket(QuicBandwidth draining_rate) |
| 12 : clock_(clock), | 12 : bytes_(0), |
| 13 bytes_(0), | |
| 14 time_last_updated_(QuicTime::Zero()), | 13 time_last_updated_(QuicTime::Zero()), |
| 15 draining_rate_(draining_rate) { | 14 draining_rate_(draining_rate) { |
| 16 } | 15 } |
| 17 | 16 |
| 18 void LeakyBucket::SetDrainingRate(QuicBandwidth draining_rate) { | 17 void LeakyBucket::SetDrainingRate(QuicTime now, QuicBandwidth draining_rate) { |
| 19 Update(); | 18 Update(now); |
| 20 draining_rate_ = draining_rate; | 19 draining_rate_ = draining_rate; |
| 21 } | 20 } |
| 22 | 21 |
| 23 void LeakyBucket::Add(QuicByteCount bytes) { | 22 void LeakyBucket::Add(QuicTime now, QuicByteCount bytes) { |
| 24 Update(); | 23 Update(now); |
| 25 bytes_ += bytes; | 24 bytes_ += bytes; |
| 26 } | 25 } |
| 27 | 26 |
| 28 QuicTime::Delta LeakyBucket::TimeRemaining() { | 27 QuicTime::Delta LeakyBucket::TimeRemaining(QuicTime now) { |
| 29 Update(); | 28 Update(now); |
| 30 return QuicTime::Delta::FromMicroseconds( | 29 return QuicTime::Delta::FromMicroseconds( |
| 31 (bytes_ * base::Time::kMicrosecondsPerSecond) / | 30 (bytes_ * base::Time::kMicrosecondsPerSecond) / |
| 32 draining_rate_.ToBytesPerSecond()); | 31 draining_rate_.ToBytesPerSecond()); |
| 33 } | 32 } |
| 34 | 33 |
| 35 QuicByteCount LeakyBucket::BytesPending() { | 34 QuicByteCount LeakyBucket::BytesPending(QuicTime now) { |
| 36 Update(); | 35 Update(now); |
| 37 return bytes_; | 36 return bytes_; |
| 38 } | 37 } |
| 39 | 38 |
| 40 void LeakyBucket::Update() { | 39 void LeakyBucket::Update(QuicTime now) { |
| 41 QuicTime now = clock_->Now(); | |
| 42 QuicTime::Delta elapsed_time = now.Subtract(time_last_updated_); | 40 QuicTime::Delta elapsed_time = now.Subtract(time_last_updated_); |
| 43 QuicByteCount bytes_cleared = draining_rate_.ToBytesPerPeriod(elapsed_time); | 41 QuicByteCount bytes_cleared = draining_rate_.ToBytesPerPeriod(elapsed_time); |
| 44 if (bytes_cleared >= bytes_) { | 42 if (bytes_cleared >= bytes_) { |
| 45 bytes_ = 0; | 43 bytes_ = 0; |
| 46 } else { | 44 } else { |
| 47 bytes_ -= bytes_cleared; | 45 bytes_ -= bytes_cleared; |
| 48 } | 46 } |
| 49 time_last_updated_ = now; | 47 time_last_updated_ = now; |
| 50 } | 48 } |
| 51 | 49 |
| 52 } // namespace net | 50 } // namespace net |
| OLD | NEW |