| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 "remoting/base/leaky_bucket.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace remoting { |
| 10 |
| 11 LeakyBucket::LeakyBucket(int depth, int rate) |
| 12 : depth_(depth), |
| 13 rate_(rate), |
| 14 current_level_(0), |
| 15 level_updated_time_(base::TimeTicks::Now()) {} |
| 16 |
| 17 LeakyBucket::~LeakyBucket() {} |
| 18 |
| 19 bool LeakyBucket::RefillOrSpill(int drops, base::TimeTicks now) { |
| 20 UpdateLevel(now); |
| 21 |
| 22 int new_level = current_level_ + drops; |
| 23 if (depth_ >= 0 && new_level > depth_) |
| 24 return false; |
| 25 current_level_ = new_level; |
| 26 return true; |
| 27 } |
| 28 |
| 29 base::TimeTicks LeakyBucket::GetEmptyTime() { |
| 30 return level_updated_time_ + |
| 31 base::TimeDelta::FromMicroseconds( |
| 32 base::TimeTicks::kMicrosecondsPerSecond * current_level_ / rate_); |
| 33 } |
| 34 |
| 35 void LeakyBucket::UpdateRate(int new_rate, base::TimeTicks now) { |
| 36 UpdateLevel(now); |
| 37 rate_ = new_rate; |
| 38 } |
| 39 |
| 40 void LeakyBucket::UpdateLevel(base::TimeTicks now) { |
| 41 current_level_ -= rate_ * (now - level_updated_time_).InMicroseconds() / |
| 42 base::TimeTicks::kMicrosecondsPerSecond; |
| 43 if (current_level_ < 0) |
| 44 current_level_ = 0; |
| 45 level_updated_time_ = now; |
| 46 } |
| 47 |
| 48 } // namespace remoting |
| OLD | NEW |