1
#pragma once
2

            
3
#include <functional>
4

            
5
#include "envoy/event/dispatcher.h"
6
#include "envoy/runtime/runtime.h"
7
#include "envoy/server/guarddog.h"
8
#include "envoy/server/overload/overload_manager.h"
9

            
10
namespace Envoy {
11
namespace Server {
12

            
13
/**
14
 * Interface for a threaded connection handling worker. All routines are thread safe.
15
 */
16
class Worker {
17
public:
18
11177
  virtual ~Worker() = default;
19

            
20
  /**
21
   * Completion called when a listener has been added on a worker and is listening for new
22
   * connections.
23
   */
24
  using AddListenerCompletion = std::function<void()>;
25

            
26
  /**
27
   * Add a listener to the worker and replace the previous listener if any. If the previous listener
28
   * doesn't exist, the behavior should be equivalent to add a new listener.
29
   * @param overridden_listener The previous listener tag to be replaced. nullopt if it's a new
30
   * listener.
31
   * @param listener supplies the listener to add.
32
   * @param completion supplies the completion to call when the listener has been added (or not) on
33
   *                   the worker.
34
   * @param runtime, supplies the runtime for the server
35
   * @param random, supplies a random number generator
36
   */
37
  virtual void addListener(absl::optional<uint64_t> overridden_listener,
38
                           Network::ListenerConfig& listener, AddListenerCompletion completion,
39
                           Runtime::Loader& runtime, Random::RandomGenerator& random) PURE;
40

            
41
  /**
42
   * @return uint64_t the number of connections across all listeners that the worker owns.
43
   */
44
  virtual uint64_t numConnections() const PURE;
45

            
46
  /**
47
   * Start the worker thread.
48
   * @param guard_dog supplies the optional guard dog to use for thread watching.
49
   * @param cb a callback to run when the worker thread starts running.
50
   */
51
  virtual void start(OptRef<GuardDog> guard_dog, const std::function<void()>& cb) PURE;
52

            
53
  /**
54
   * Initialize stats for this worker's dispatcher, if available. The worker will output
55
   * thread-specific stats under the given scope.
56
   * @param scope the scope to contain the new per-dispatcher stats created here.
57
   */
58
  virtual void initializeStats(Stats::Scope& scope) PURE;
59

            
60
  /**
61
   * Stop the worker thread.
62
   */
63
  virtual void stop() PURE;
64

            
65
  /**
66
   * Remove a listener from the worker.
67
   * @param listener supplies the listener to remove.
68
   * @param completion supplies the completion to be called when the listener has been removed.
69
   *        This completion is called on the worker thread. No locking is performed by the worker.
70
   */
71
  virtual void removeListener(Network::ListenerConfig& listener,
72
                              std::function<void()> completion) PURE;
73
  /**
74
   * Remove the stale filter chains of the given listener but leave the listener running.
75
   * @param listener_tag supplies the tag passed to addListener().
76
   * @param filter_chains supplies the filter chains to be removed.
77
   * @param completion supplies the completion to be called when the listener removed all the
78
   * untracked connections. This completion is called on the worker thread. No locking is performed
79
   * by the worker.
80
   */
81
  virtual void removeFilterChains(uint64_t listener_tag,
82
                                  const std::list<const Network::FilterChain*>& filter_chains,
83
                                  std::function<void()> completion) PURE;
84

            
85
  /**
86
   * Stop a listener from accepting new connections. This is used for server draining.
87
   * @param listener supplies the listener to stop.
88
   * @param options additional options to be passed through to shutdownListener.
89
   * @param completion supplies the completion to be called when the listener has stopped
90
   * accepting new connections. This completion is called on the worker thread. No locking is
91
   * performed by the worker.
92
   */
93
  virtual void stopListener(Network::ListenerConfig& listener,
94
                            const Network::ExtraShutdownListenerOptions& options,
95
                            std::function<void()> completion) PURE;
96
};
97

            
98
using WorkerPtr = std::unique_ptr<Worker>;
99

            
100
/**
101
 * Factory for creating workers.
102
 */
103
class WorkerFactory {
104
public:
105
11177
  virtual ~WorkerFactory() = default;
106

            
107
  /**
108
   * @param index supplies the index of the worker, in the range of [0, concurrency).
109
   * @param overload_manager supplies the server's overload manager.
110
   * @param null_overload_manager supplies the server's null overload manager for conditions where
111
   * overload manager is disabled.
112
   * @param worker_name supplies the name of the worker, used for per-worker stats.
113
   * @return WorkerPtr a new worker.
114
   */
115
  virtual WorkerPtr createWorker(uint32_t index, OverloadManager& overload_manager,
116
                                 OverloadManager& null_overload_manager,
117
                                 const std::string& worker_name) PURE;
118
};
119

            
120
} // namespace Server
121
} // namespace Envoy