blob: eb5ee9b011ea664a33bfaccdfb4f143b87d88d3d (
plain) (
blame)
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
57
58
59
|
#pragma once
#include "cpu.h"
#include <util/datetime/base.h>
#include <util/system/types.h>
#include <util/thread/factory.h>
#include <deque>
namespace NCpuLimit {
class TCpuMeasurer {
struct TProbe {
TDuration Period;
std::atomic<double> Usage;
THolder<IThreadFactory::IThread> MeasurerThread;
std::deque<std::pair<TInstant, TDuration>> Window;
TDuration WindowDuration{};
TDuration WindowUsage{};
};
public:
explicit TCpuMeasurer(TDuration probePeriod);
~TCpuMeasurer();
double CpuUsageFast() const {
return FastProbe_.Usage.load();
}
double CpuUsageSlow() const {
return SlowProbe_.Usage.load();
}
private:
void UpdateProbeThread(TProbe& probe);
std::atomic<bool> Finished_ = false;
TProbe FastProbe_;
TProbe SlowProbe_;
};
class TCpuLimiter {
public:
TCpuLimiter(double slowThreshold, double fastThresholdBegin, double fastThresholdEnd);
// Only throttle (all requests) when LA is greater than FastThresholdEnd_
bool ThrottleSoft(double slowUsage, double fastUsage) const;
// Throttle requests with some probability when LA is greater than
// FastThresholdBegin_
bool ThrottleHard(double slowUsage, double fastUsage) const;
private:
double SlowThreshold_;
double FastThresholdBegin_;
double FastThresholdEnd_;
};
}
|