Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / remoting / test / leaky_bucket.cc
blobee8a62b06de136d82c2a040c5abb5706f2d9f06e
1 // Copyright 2014 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 "remoting/test/leaky_bucket.h"
7 #include "base/logging.h"
9 namespace remoting {
11 LeakyBucket::LeakyBucket(double depth, double rate)
12 : depth_(depth),
13 rate_(rate),
14 level_(0.0),
15 last_update_(base::TimeTicks::Now()) {
18 LeakyBucket::~LeakyBucket() {
21 base::TimeDelta LeakyBucket::AddPacket(int size) {
22 UpdateLevel();
24 double new_level = level_ + size;
25 if (new_level > depth_)
26 return base::TimeDelta::Max();
28 base::TimeDelta result = base::TimeDelta::FromSecondsD(level_ / rate_);
29 level_ = new_level;
30 return result;
33 void LeakyBucket::UpdateLevel() {
34 base::TimeTicks now = base::TimeTicks::Now();
35 level_ -= rate_ * (now - last_update_).InSecondsF();
36 if (level_ < 0.0)
37 level_ = 0.0;
38 last_update_ = now;
41 } // namespace remoting