proxygen
LoadShedConfiguration.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
17 
18 #include <folly/Conv.h>
20 
22 using std::string;
23 
24 namespace wangle {
25 
27  auto addr = input.str();
28  size_t separator = addr.find_first_of('/');
29  if (separator == string::npos) {
31  } else {
32  unsigned prefixLen = folly::to<unsigned>(addr.substr(separator + 1));
33  addr.erase(separator);
34  whitelistNetworks_.insert(
35  NetworkAddress(SocketAddress(addr, 0), prefixLen));
36  }
37 }
38 
40  if (whitelistAddrs_.find(address) != whitelistAddrs_.end()) {
41  return true;
42  }
43  for (auto& network : whitelistNetworks_) {
44  if (network.contains(address)) {
45  return true;
46  }
47  }
48  return false;
49 }
50 
51 void LoadShedConfiguration::checkIsSane(const SysParams& sysParams) const {
53  // Period must be greater than or equal to 0.
54  CHECK_GE(period_.count(), std::chrono::milliseconds(0).count());
55 
56  // CPU exceed window must be of size at least equal to 1.
57  CHECK_GE(cpuUsageExceedWindowSize_, 1);
58 
59  // Min cpu idle and max cpu ratios must have values in the range of [0-1]
60  // inclusive and min cpu idle, normalized, must be greater than or equal
61  // to max cpu ratio.
62  CHECK_GE(minCpuIdle_, 0.0);
63  CHECK_LE(minCpuIdle_, 1.0);
64  CHECK_GE(1.0 - minCpuIdle_, maxCpuUsage_);
65  CHECK_GE(maxCpuUsage_, 0.0);
66  CHECK_LE(maxCpuUsage_, 1.0);
67  CHECK_GE(logicalCpuCoreQuorum_, 0);
68  CHECK_LE(logicalCpuCoreQuorum_, sysParams.numLogicalCpuCores);
69 
70  // Max mem usage must be less than or equal to min free mem, normalized.
71  // We also must verify that min free mem is less than or equal to total
72  // mem bytes.
73  CHECK_GE(maxMemUsage_, 0.0);
74  CHECK_LE(maxMemUsage_, 1.0);
75  CHECK_GE(
76  1.0 - ((double)minFreeMem_ / sysParams.totalMemBytes), maxMemUsage_);
77  CHECK_LE(minFreeMem_, sysParams.totalMemBytes);
78 
79  // Max TCP mem and min free TCP mem ratios must have values in the range
80  // of [0-1] inclusive and 1.0 minus min TCP mem ration must be greater than
81  // or equal to max TCP mem ratio.
82  CHECK_GE(maxTcpMemUsage_, 0.0);
83  CHECK_LE(maxTcpMemUsage_, 1.0);
84  CHECK_GE(1.0 - minFreeTcpMemPct_, maxTcpMemUsage_);
85  CHECK_GE(minFreeTcpMemPct_, 0.0);
86  CHECK_LE(minFreeTcpMemPct_, 1.0);
87 
88  // Active connetions obviously must be less than or equal to max
89  // connections.
91  }
92 }
93 
94 } // namespace wangle
std::string str() const
Definition: Range.h:591
void checkIsSane(const SysParams &sysParams) const
std::chrono::milliseconds period_
bool isWhitelisted(const folly::SocketAddress &addr) const
const char * string
Definition: Conv.cpp:212
void addWhitelistAddr(folly::StringPiece)
ThreadPoolListHook * addr