57c51b97222e72911ba4355630558715fd594cd7
[folly.git] / folly / wangle / acceptor / LoadShedConfiguration.h
1 /*
2  *  Copyright (c) 2015, Facebook, Inc.
3  *  All rights reserved.
4  *
5  *  This source code is licensed under the BSD-style license found in the
6  *  LICENSE file in the root directory of this source tree. An additional grant
7  *  of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11
12 #include <chrono>
13 #include <folly/Range.h>
14 #include <folly/SocketAddress.h>
15 #include <glog/logging.h>
16 #include <list>
17 #include <set>
18 #include <string>
19
20 #include <folly/wangle/acceptor/NetworkAddress.h>
21
22 namespace folly {
23
24 /**
25  * Class that holds an LoadShed configuration for a service
26  */
27 class LoadShedConfiguration {
28  public:
29
30   // Comparison function for SocketAddress that disregards the port
31   struct AddressOnlyCompare {
32     bool operator()(
33      const SocketAddress& addr1,
34      const SocketAddress& addr2) const {
35       return addr1.getIPAddress() < addr2.getIPAddress();
36     }
37   };
38
39   typedef std::set<SocketAddress, AddressOnlyCompare> AddressSet;
40   typedef std::set<NetworkAddress> NetworkSet;
41
42   LoadShedConfiguration() {}
43
44   ~LoadShedConfiguration() {}
45
46   void addWhitelistAddr(folly::StringPiece);
47
48   /**
49    * Set/get the set of IPs that should be whitelisted through even when we're
50    * trying to shed load.
51    */
52   void setWhitelistAddrs(const AddressSet& addrs) { whitelistAddrs_ = addrs; }
53   const AddressSet& getWhitelistAddrs() const { return whitelistAddrs_; }
54
55   /**
56    * Set/get the set of networks that should be whitelisted through even
57    * when we're trying to shed load.
58    */
59   void setWhitelistNetworks(const NetworkSet& networks) {
60     whitelistNetworks_ = networks;
61   }
62   const NetworkSet& getWhitelistNetworks() const { return whitelistNetworks_; }
63
64   /**
65    * Set/get the maximum number of downstream connections across all VIPs.
66    */
67   void setMaxConnections(uint64_t maxConns) { maxConnections_ = maxConns; }
68   uint64_t getMaxConnections() const { return maxConnections_; }
69
70   /**
71    * Set/get the maximum cpu usage.
72    */
73   void setMaxMemUsage(double max) {
74     CHECK(max >= 0);
75     CHECK(max <= 1);
76     maxMemUsage_ = max;
77   }
78   double getMaxMemUsage() const { return maxMemUsage_; }
79
80   /**
81    * Set/get the maximum memory usage.
82    */
83   void setMaxCpuUsage(double max) {
84     CHECK(max >= 0);
85     CHECK(max <= 1);
86     maxCpuUsage_ = max;
87   }
88   double getMaxCpuUsage() const { return maxCpuUsage_; }
89
90   /**
91    * Set/get the minium actual free memory on the system.
92    */
93   void setMinFreeMem(uint64_t min) {
94     minFreeMem_ = min;
95   }
96   uint64_t getMinFreeMem() const {
97     return minFreeMem_;
98   }
99
100   void setLoadUpdatePeriod(std::chrono::milliseconds period) {
101     period_ = period;
102   }
103   std::chrono::milliseconds getLoadUpdatePeriod() const { return period_; }
104
105   bool isWhitelisted(const SocketAddress& addr) const;
106
107  private:
108
109   AddressSet whitelistAddrs_;
110   NetworkSet whitelistNetworks_;
111   uint64_t maxConnections_{0};
112   uint64_t minFreeMem_{0};
113   double maxMemUsage_;
114   double maxCpuUsage_;
115   std::chrono::milliseconds period_;
116 };
117
118 }