1
#pragma once
2

            
3
#include <functional>
4
#include <memory>
5

            
6
#include "envoy/config/listener/v3/listener.pb.h"
7
#include "envoy/event/dispatcher.h"
8
#include "envoy/network/drain_decision.h"
9
#include "envoy/thread_local/thread_local_object.h"
10

            
11
namespace Envoy {
12
namespace Server {
13

            
14
class DrainManager;
15
using DrainManagerPtr = std::unique_ptr<DrainManager>;
16

            
17
/**
18
 * Handles connection draining. This concept is used globally during hot restart / server draining
19
 * as well as on individual listeners and filter-chains when they are being dynamically removed.
20
 */
21
class DrainManager : public Network::DrainDecision, public ThreadLocal::ThreadLocalObject {
22
public:
23
  /**
24
   * @brief Create a child drain-manager. Will proxy the drain status from the parent, but can also
25
   * be used to enact local draining.
26
   *
27
   * Child managers can be used to construct "drain trees" where each node in the tree can drain
28
   * independently of it's parent node, but the drain status cascades to child nodes.
29
   *
30
   * A notable difference to drain callbacks is that child managers are notified immediately and
31
   * without a delay timing. Additionally, notifications from parent to child is a thread-safe
32
   * operation whereas callback registration and triggering is not.
33
   *
34
   * @param dispatcher Dispatcher for the current thread in which the new child drain-manager will
35
   * exist.
36
   * @param drain_type The drain-type for the manager. May be different from the parent manager.
37
   */
38
  virtual DrainManagerPtr
39
  createChildManager(Event::Dispatcher& dispatcher,
40
                     envoy::config::listener::v3::Listener::DrainType drain_type) PURE;
41
  virtual DrainManagerPtr createChildManager(Event::Dispatcher& dispatcher) PURE;
42

            
43
  /**
44
   * Invoked to begin the drain procedure. (Making drain close operations more likely).
45
   * @param direction is the direction of the drain.
46
   * @param drain_complete_cb will be invoked once the drain sequence is finished. The parameter is
47
   * optional and can be an unassigned function.
48
   */
49
  virtual void startDrainSequence(Network::DrainDirection direction,
50
                                  std::function<void()> drain_complete_cb) PURE;
51

            
52
  /**
53
   * @return whether the drain sequence has started for this direction.
54
   */
55
  virtual bool draining(Network::DrainDirection) const PURE;
56

            
57
  /**
58
   * Invoked in the newly launched primary process to begin the parent shutdown sequence. At the end
59
   * of the sequence the previous primary process will be terminated.
60
   */
61
  virtual void startParentShutdownSequence() PURE;
62
};
63

            
64
using DrainManagerPtr = std::unique_ptr<DrainManager>;
65

            
66
} // namespace Server
67
} // namespace Envoy