/
quota_tracker.cpp
56 lines (42 loc) · 1.56 KB
/
quota_tracker.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "quota_tracker.h"
namespace NKikimr::NPQ {
TQuotaTracker::TQuotaTracker(const ui64 maxBurst, const ui64 speedPerSecond, const TInstant timestamp)
: AvailableSize(maxBurst)
, SpeedPerSecond(speedPerSecond)
, LastUpdateTime(timestamp)
, MaxBurst(maxBurst)
{}
bool TQuotaTracker::UpdateConfigIfChanged(const ui64 maxBurst, const ui64 speedPerSecond) {
if (maxBurst != MaxBurst || speedPerSecond != SpeedPerSecond) {
SpeedPerSecond = speedPerSecond;
MaxBurst = maxBurst;
AvailableSize = maxBurst;
return true;
}
return false;
}
void TQuotaTracker::Update(const TInstant timestamp) {
TDuration diff = timestamp - LastUpdateTime;
LastUpdateTime = timestamp;
if (AvailableSize < 0) {
QuotedTime += diff;
}
AvailableSize = Min<i64>(AvailableSize + (ui64)SpeedPerSecond * diff.MicroSeconds() / 1000'000, MaxBurst);
}
bool TQuotaTracker::CanExaust(const TInstant timestamp) {
Update(timestamp);
return AvailableSize > 0;
}
void TQuotaTracker::Exaust(const ui64 size, const TInstant timestamp) {
Update(timestamp);
AvailableSize -= (i64)size;
Update(timestamp);
}
TDuration TQuotaTracker::GetQuotedTime(const TInstant timestamp) {
Update(timestamp);
return QuotedTime;
}
ui64 TQuotaTracker::GetTotalSpeed() const {
return SpeedPerSecond;
}
} // NKikimr::NPQ