forked from sanyaade-mobiledev/chromium.src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaky_bucket.cc
41 lines (31 loc) · 956 Bytes
/
leaky_bucket.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2014 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/test/leaky_bucket.h"
#include "base/logging.h"
namespace remoting {
LeakyBucket::LeakyBucket(double depth, double rate)
: depth_(depth),
rate_(rate),
level_(0.0),
last_update_(base::TimeTicks::Now()) {
}
LeakyBucket::~LeakyBucket() {
}
base::TimeDelta LeakyBucket::AddPacket(int size) {
UpdateLevel();
double new_level = level_ + size;
if (new_level > depth_)
return base::TimeDelta::Max();
base::TimeDelta result = base::TimeDelta::FromSecondsD(level_ / rate_);
level_ = new_level;
return result;
}
void LeakyBucket::UpdateLevel() {
base::TimeTicks now = base::TimeTicks::Now();
level_ -= rate_ * (now - last_update_).InSecondsF();
if (level_ < 0.0)
level_ = 0.0;
last_update_ = now;
}
} // namespace remoting