• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "remoting/test/leaky_bucket.h"
6 
7 #include "base/logging.h"
8 
9 namespace remoting {
10 
LeakyBucket(double depth,double rate)11 LeakyBucket::LeakyBucket(double depth, double rate)
12     : depth_(depth),
13       rate_(rate),
14       level_(0.0),
15       last_update_(base::TimeTicks::Now()) {
16 }
17 
~LeakyBucket()18 LeakyBucket::~LeakyBucket() {
19 }
20 
AddPacket(int size)21 base::TimeDelta LeakyBucket::AddPacket(int size) {
22   UpdateLevel();
23 
24   double new_level = level_ + size;
25   if (new_level > depth_)
26     return base::TimeDelta::Max();
27 
28   base::TimeDelta result = base::TimeDelta::FromSecondsD(level_ / rate_);
29   level_ = new_level;
30   return result;
31 }
32 
UpdateLevel()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;
39 }
40 
41 }  // namespace remoting
42