1
#pragma once
2

            
3
#include <string>
4

            
5
#include "envoy/common/pure.h"
6
#include "envoy/event/dispatcher.h"
7
#include "envoy/event/scaled_range_timer_manager.h"
8
#include "envoy/server/overload/load_shed_point.h"
9
#include "envoy/server/overload/thread_local_overload_state.h"
10

            
11
#include "source/common/singleton/const_singleton.h"
12

            
13
namespace Envoy {
14
namespace Server {
15
/**
16
 * Well-known overload action names.
17
 */
18
class OverloadActionNameValues {
19
public:
20
  // Overload action to stop accepting new HTTP requests.
21
  const std::string StopAcceptingRequests = "envoy.overload_actions.stop_accepting_requests";
22

            
23
  // Overload action to disable http keepalive (for HTTP1.x).
24
  const std::string DisableHttpKeepAlive = "envoy.overload_actions.disable_http_keepalive";
25

            
26
  // Overload action to stop accepting new connections.
27
  const std::string StopAcceptingConnections = "envoy.overload_actions.stop_accepting_connections";
28

            
29
  // Overload action to reject (accept and then close) new connections.
30
  const std::string RejectIncomingConnections =
31
      "envoy.overload_actions.reject_incoming_connections";
32

            
33
  // Overload action to try to shrink the heap by releasing free memory.
34
  const std::string ShrinkHeap = "envoy.overload_actions.shrink_heap";
35

            
36
  // Overload action to reduce some subset of configured timeouts.
37
  const std::string ReduceTimeouts = "envoy.overload_actions.reduce_timeouts";
38

            
39
  // Overload action to reset streams using excessive memory.
40
  const std::string ResetStreams = "envoy.overload_actions.reset_high_memory_stream";
41

            
42
  // This should be kept current with the Overload actions available.
43
  // This is the last member of this class to duplicating the strings with
44
  // proper lifetime guarantees.
45
  const std::array<absl::string_view, 7> WellKnownActions = {StopAcceptingRequests,
46
                                                             DisableHttpKeepAlive,
47
                                                             StopAcceptingConnections,
48
                                                             RejectIncomingConnections,
49
                                                             ShrinkHeap,
50
                                                             ReduceTimeouts,
51
                                                             ResetStreams};
52
};
53

            
54
using OverloadActionNames = ConstSingleton<OverloadActionNameValues>;
55

            
56
/**
57
 * Well-known overload action stats.
58
 */
59
class OverloadActionStatsNameValues {
60
public:
61
  // Count of the number of streams the reset streams action has reset
62
  const std::string ResetStreamsCount = "envoy.overload_actions.reset_high_memory_stream.count";
63
};
64

            
65
using OverloadActionStatsNames = ConstSingleton<OverloadActionStatsNameValues>;
66

            
67
/**
68
 * The OverloadManager protects the Envoy instance from being overwhelmed by client
69
 * requests. It monitors a set of resources and notifies registered listeners if
70
 * configured thresholds for those resources have been exceeded.
71
 */
72
class OverloadManager : public LoadShedPointProvider {
73
public:
74
  /**
75
   * Start a recurring timer to monitor resources and notify listeners when overload actions
76
   * change state.
77
   */
78
  virtual void start() PURE;
79

            
80
  /**
81
   * Register a callback to be invoked when the specified overload action changes state
82
   * (i.e., becomes activated or inactivated). Must be called before the start method is called.
83
   * @param action const std::string& the name of the overload action to register for
84
   * @param dispatcher Event::Dispatcher& the dispatcher on which callbacks will be posted
85
   * @param callback OverloadActionCb the callback to post when the overload action
86
   *        changes state
87
   * @returns true if action was registered and false if no such action has been configured
88
   */
89
  virtual bool registerForAction(const std::string& action, Event::Dispatcher& dispatcher,
90
                                 OverloadActionCb callback) PURE;
91

            
92
  /**
93
   * Get the thread-local overload action states. Lookups in this object can be used as
94
   * an alternative to registering a callback for overload action state changes.
95
   */
96
  virtual ThreadLocalOverloadState& getThreadLocalOverloadState() PURE;
97

            
98
  /**
99
   * Get a factory for constructing scaled timer managers that respond to overload state.
100
   */
101
  virtual Event::ScaledRangeTimerManagerFactory scaledTimerFactory() PURE;
102

            
103
  /**
104
   * Stop the overload manager timer and wait for any pending resource updates to complete.
105
   * After this returns, overload manager clients should not receive any more callbacks
106
   * about overload state changes.
107
   */
108
  virtual void stop() PURE;
109
};
110

            
111
} // namespace Server
112
} // namespace Envoy