-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolicy.cc
More file actions
92 lines (75 loc) · 2.03 KB
/
policy.cc
File metadata and controls
92 lines (75 loc) · 2.03 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "nexran.h"
namespace nexran {
std::map<AllocationPolicy::Type,const char *> AllocationPolicy::type_to_string = {
{ Proportional, "proportional" },
};
int ProportionalAllocationPolicy::maybeEndThrottling()
{
if (!isThrottling())
return -1;
else if (throttle && std::time(nullptr) < throttle_end)
return -1;
int ret = throttle_saved_share;
is_throttling = false;
throttle_end = 0;
throttle_saved_share = -1;
// Caller must call setShare() on the new value.
return ret;
}
int ProportionalAllocationPolicy::maybeStartThrottling()
{
if (!throttle)
return -1;
else if (isThrottling())
return -1;
else if (metrics.get_total_bytes() < throttle_threshold)
return -1;
throttle_saved_share = share;
is_throttling = true;
throttle_end = std::time(nullptr) + throttle_period;
// Caller must call setShare() on the new value.
return throttle_share;
}
bool ProportionalAllocationPolicy::update(const rapidjson::Value& obj,AppError **ae)
{
if (!obj.IsObject()) {
if (ae) {
if (!*ae)
*ae = new AppError(400);
(*ae)->add(std::string("request is not an object"));
}
return NULL;
}
if (!obj.HasMember("type")
|| !obj["type"].IsString()
|| strcmp(obj["type"].GetString(),getName()) != 0
|| !obj.HasMember("share")
|| !obj["share"].IsInt()
|| obj["share"].GetInt() < 0
|| obj["share"].GetInt() > 1024) {
if (ae) {
if (!*ae)
*ae = new AppError(400);
(*ae)->add(std::string("malformed allocation_policy property"));
}
return NULL;
}
if ((obj.HasMember("auto_equalize")
&& !obj["auto_equalize"].IsBool())
|| (obj.HasMember("throttle")
&& !obj["throttle"].IsBool())) {
if (ae) {
if (!*ae)
*ae = new AppError(400);
(*ae)->add(std::string("malformed allocation_policy property"));
}
return NULL;
}
share = obj["share"].GetInt();
if (obj.HasMember("auto_equalize"))
auto_equalize = obj["auto_equalize"].GetBool();
if (obj.HasMember("throttle"))
throttle = obj["throttle"].GetBool();
return true;
}
}