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. 4 5 #include "net/quic/congestion_control/leaky_bucket.h" 6 7 #include "base/time/time.h" 8 9 namespace net { 10 11 LeakyBucket::LeakyBucket(QuicBandwidth draining_rate) 12 : bytes_(0), 13 time_last_updated_(QuicTime::Zero()), 14 draining_rate_(draining_rate) { 15 } 16 17 void LeakyBucket::SetDrainingRate(QuicTime now, QuicBandwidth draining_rate) { 18 Update(now); 19 draining_rate_ = draining_rate; 20 } 21 22 void LeakyBucket::Add(QuicTime now, QuicByteCount bytes) { 23 Update(now); 24 bytes_ += bytes; 25 } 26 27 QuicTime::Delta LeakyBucket::TimeRemaining(QuicTime now) { 28 Update(now); 29 return QuicTime::Delta::FromMicroseconds( 30 (bytes_ * base::Time::kMicrosecondsPerSecond) / 31 draining_rate_.ToBytesPerSecond()); 32 } 33 34 QuicByteCount LeakyBucket::BytesPending(QuicTime now) { 35 Update(now); 36 return bytes_; 37 } 38 39 void LeakyBucket::Update(QuicTime now) { 40 QuicTime::Delta elapsed_time = now.Subtract(time_last_updated_); 41 QuicByteCount bytes_cleared = draining_rate_.ToBytesPerPeriod(elapsed_time); 42 if (bytes_cleared >= bytes_) { 43 bytes_ = 0; 44 } else { 45 bytes_ -= bytes_cleared; 46 } 47 time_last_updated_ = now; 48 } 49 50 } // namespace net 51