1
#pragma once
2

            
3
#include "envoy/network/listen_socket.h"
4

            
5
namespace Envoy {
6
namespace Network {
7

            
8
/**
9
 * A connection handler that is balanced. Typically implemented by individual listeners depending
10
 * on their balancing configuration.
11
 */
12
class BalancedConnectionHandler {
13
public:
14
36989
  virtual ~BalancedConnectionHandler() = default;
15

            
16
  /**
17
   * @return the number of active connections within the handler.
18
   */
19
  virtual uint64_t numConnections() const PURE;
20

            
21
  /**
22
   * Increment the number of connections within the handler. This must be called by a connection
23
   * balancer implementation prior to a connection being picked via pickTargetHandler(). This makes
24
   * sure that connection counts are accurate during connection transfer (i.e., that the target
25
   * balancer accounts for the incoming connection). This is done by the balancer vs. the
26
   * connection handler to account for different locking needs inside the balancer.
27
   */
28
  virtual void incNumConnections() PURE;
29

            
30
  /**
31
   * Post a connected socket to this connection handler. This is used for cross-thread connection
32
   * transfer during the balancing process.
33
   */
34
  virtual void post(Network::ConnectionSocketPtr&& socket) PURE;
35

            
36
  /**
37
   * Main call to accept a socket on a worker.
38
   * @param hand_off_restored_destination_connections used to select the original destination
39
listener.
40
   * @param rebalanced indicates whether rebalancing is already done
41
   * @param network_namespace file path to the network namespace from the listener address
42
   */
43
  virtual void onAcceptWorker(Network::ConnectionSocketPtr&& socket,
44
                              bool hand_off_restored_destination_connections, bool rebalanced,
45
                              const absl::optional<std::string>& network_namespace) PURE;
46
};
47

            
48
/**
49
 * An implementation of a connection balancer. This abstracts the underlying policy (e.g., exact,
50
 * fuzzy, etc.).
51
 */
52
class ConnectionBalancer {
53
public:
54
38612
  virtual ~ConnectionBalancer() = default;
55

            
56
  /**
57
   * Register a new handler with the balancer that is available for balancing.
58
   */
59
  virtual void registerHandler(BalancedConnectionHandler& handler) PURE;
60

            
61
  /**
62
   * Unregister a handler with the balancer that is no longer available for balancing.
63
   */
64
  virtual void unregisterHandler(BalancedConnectionHandler& handler) PURE;
65

            
66
  /**
67
   * Pick a target handler to send a connection to.
68
   * @param current_handler supplies the currently executing connection handler.
69
   * @return current_handler if the connection should stay bound to the current handler, or a
70
   *         different handler if the connection should be rebalanced.
71
   *
72
   * NOTE: It is the responsibility of the balancer to call incNumConnections() on the returned
73
   *       balancer. See the comments above for more explanation.
74
   */
75
  virtual BalancedConnectionHandler&
76
  pickTargetHandler(BalancedConnectionHandler& current_handler) PURE;
77
};
78

            
79
using ConnectionBalancerSharedPtr = std::shared_ptr<ConnectionBalancer>;
80
using BalancedConnectionHandlerOptRef =
81
    absl::optional<std::reference_wrapper<BalancedConnectionHandler>>;
82

            
83
} // namespace Network
84
} // namespace Envoy