proxygen
LoadShedConfiguration.h
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  */
16 #pragma once
17 
18 #include <chrono>
19 #include <folly/Range.h>
20 #include <folly/SocketAddress.h>
21 #include <glog/logging.h>
22 #include <list>
23 #include <set>
24 #include <string>
25 
27 
28 namespace wangle {
29 
34  public:
35 
36  // Comparison function for SocketAddress that disregards the port
38  bool operator()(
39  const folly::SocketAddress& addr1,
40  const folly::SocketAddress& addr2) const {
41  return addr1.getIPAddress() < addr2.getIPAddress();
42  }
43  };
44 
45  typedef std::set<folly::SocketAddress, AddressOnlyCompare> AddressSet;
46  typedef std::set<NetworkAddress> NetworkSet;
47 
48  LoadShedConfiguration() = default;
49 
50  ~LoadShedConfiguration() = default;
51 
53 
58  void setWhitelistAddrs(const AddressSet& addrs) { whitelistAddrs_ = addrs; }
59  const AddressSet& getWhitelistAddrs() const { return whitelistAddrs_; }
60 
65  void setWhitelistNetworks(const NetworkSet& networks) {
66  whitelistNetworks_ = networks;
67  }
68  const NetworkSet& getWhitelistNetworks() const { return whitelistNetworks_; }
69 
73  void setMaxConnections(uint64_t maxConns) { maxConnections_ = maxConns; }
75 
80  void setMaxActiveConnections(uint64_t maxActiveConns) {
81  maxActiveConnections_ = maxActiveConns;
82  }
84 
90  const uint64_t acceptPauseOnAcceptorQueueSize) {
91  acceptPauseOnAcceptorQueueSize_ = acceptPauseOnAcceptorQueueSize;
92  }
95  }
96 
102  const uint64_t acceptResumeOnAcceptorQueueSize) {
103  acceptResumeOnAcceptorQueueSize_ = acceptResumeOnAcceptorQueueSize;
104  }
107  }
108 
114  void setMaxMemUsage(double max) {
115  CHECK(max >= 0);
116  CHECK(max <= 1);
117  maxMemUsage_ = max;
118  }
119  double getMaxMemUsage() const { return maxMemUsage_; }
120 
126  void setMaxCpuUsage(double max) {
127  CHECK(max >= 0);
128  CHECK(max <= 1);
129  maxCpuUsage_ = max;
130  }
131  double getMaxCpuUsage() const { return maxCpuUsage_; }
132 
138  void setMinCpuIdle(double min) {
139  CHECK(min >= 0);
140  CHECK(min <= 1);
141  minCpuIdle_ = min;
142  }
143  double getMinCpuIdle() const { return minCpuIdle_; }
144 
154  logicalCpuCoreQuorum_ = quorum;
155  }
157  return logicalCpuCoreQuorum_;
158  }
159 
165  }
166 
169  }
170 
177  minFreeMem_ = min;
178  }
180  return minFreeMem_;
181  }
182 
183  void setLoadUpdatePeriod(std::chrono::milliseconds period) {
184  period_ = period;
185  }
186  std::chrono::milliseconds getLoadUpdatePeriod() const { return period_; }
187 
188  void setMaxTcpMemUsage(double max) {
189  CHECK_GE(max, 0.0);
190  CHECK_LE(max, 1.0);
192  }
193  double getMaxTcpMemUsage() const {
194  return maxTcpMemUsage_;
195  }
196 
197  void setMinFreeTcpMemPct(double min) {
198  CHECK_GE(min, 0.0);
199  CHECK_LE(min, 1.0);
201  }
202  double getMinFreeTcpMemPct() const {
203  return minFreeTcpMemPct_;
204  }
205 
206  void setLoadSheddingEnabled(bool enabled) {
207  loadSheddingEnabled_ = enabled;
208  }
209 
210  bool getLoadSheddingEnabled() const {
211  return loadSheddingEnabled_;
212  }
213 
214  bool isWhitelisted(const folly::SocketAddress& addr) const;
215 
224  struct SysParams {
225  uint64_t numLogicalCpuCores{0};
226  uint64_t totalMemBytes{0};
227  };
228  void checkIsSane(const SysParams& sysParams) const;
229 
230  private:
231 
232  AddressSet whitelistAddrs_;
233  NetworkSet whitelistNetworks_;
239  double maxMemUsage_{1.0};
240  double maxCpuUsage_{1.0};
241  double minCpuIdle_{0.0};
244  double maxTcpMemUsage_{1.0};
245  double minFreeTcpMemPct_{0.0};
246  std::chrono::milliseconds period_;
248 };
249 
250 } // namespace wangle
void setCpuUsageExceedWindowSize(const uint64_t size)
void setLogicalCpuCoreQuorum(uint64_t quorum)
void setMaxConnections(uint64_t maxConns)
std::chrono::milliseconds getLoadUpdatePeriod() const
uint64_t getCpuUsageExceedWindowSize() const
void checkIsSane(const SysParams &sysParams) const
LogLevel max
Definition: LogLevel.cpp:31
std::chrono::milliseconds period_
void setWhitelistNetworks(const NetworkSet &networks)
void setMaxActiveConnections(uint64_t maxActiveConns)
void setWhitelistAddrs(const AddressSet &addrs)
bool isWhitelisted(const folly::SocketAddress &addr) const
const NetworkSet & getWhitelistNetworks() const
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
std::set< folly::SocketAddress, AddressOnlyCompare > AddressSet
LogLevel min
Definition: LogLevel.cpp:30
const folly::IPAddress & getIPAddress() const
void setLoadUpdatePeriod(std::chrono::milliseconds period)
std::set< NetworkAddress > NetworkSet
const AddressSet & getWhitelistAddrs() const
uint64_t getAcceptPauseOnAcceptorQueueSize() const
void setAcceptPauseOnAcceptorQueueSize(const uint64_t acceptPauseOnAcceptorQueueSize)
bool operator()(const folly::SocketAddress &addr1, const folly::SocketAddress &addr2) const
void addWhitelistAddr(folly::StringPiece)
ThreadPoolListHook * addr
void setAcceptResumeOnAcceptorQueueSize(const uint64_t acceptResumeOnAcceptorQueueSize)
uint64_t getAcceptResumeOnAcceptorQueueSize() const