| Index: remoting/base/leaky_bucket.cc
|
| diff --git a/remoting/base/leaky_bucket.cc b/remoting/base/leaky_bucket.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..72c15dc028bd43ba1e378034856db6781ce3e39f
|
| --- /dev/null
|
| +++ b/remoting/base/leaky_bucket.cc
|
| @@ -0,0 +1,48 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "remoting/base/leaky_bucket.h"
|
| +
|
| +#include "base/logging.h"
|
| +
|
| +namespace remoting {
|
| +
|
| +LeakyBucket::LeakyBucket(int depth, int rate)
|
| + : depth_(depth),
|
| + rate_(rate),
|
| + level_(0),
|
| + last_update_(base::TimeTicks::Now()) {
|
| + DCHECK_GE(depth_, 0);
|
| +}
|
| +
|
| +LeakyBucket::~LeakyBucket() {}
|
| +
|
| +bool LeakyBucket::RefillOrSpill(int size, base::TimeTicks now) {
|
| + UpdateLevel(now);
|
| +
|
| + int new_level = level_ + size;
|
| + if (depth_ > 0 && new_level > depth_)
|
| + return false;
|
| + level_ = new_level;
|
| + return true;
|
| +}
|
| +
|
| +base::TimeTicks LeakyBucket::GetEmptyTime() {
|
| + return last_update_ + base::TimeDelta::FromSecondsD(level_ / rate_);
|
| +}
|
| +
|
| +void LeakyBucket::UpdateRate(int new_rate, base::TimeTicks now) {
|
| + UpdateLevel(now);
|
| + rate_ = new_rate;
|
| +}
|
| +
|
| +void LeakyBucket::UpdateLevel(base::TimeTicks now) {
|
| + level_ -= rate_ * (now - last_update_).InMicroseconds() /
|
| + base::TimeTicks::kMicrosecondsPerSecond;
|
| + if (level_ < 0)
|
| + level_ = 0;
|
| + last_update_ = now;
|
| +}
|
| +
|
| +} // namespace remoting
|
|
|