forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_handler_impl.h
170 lines (140 loc) · 6.01 KB
/
connection_handler_impl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#pragma once
#include <atomic>
#include <cstdint>
#include <list>
#include <memory>
#include "envoy/common/time.h"
#include "envoy/event/deferred_deletable.h"
#include "envoy/network/connection.h"
#include "envoy/network/connection_handler.h"
#include "envoy/network/filter.h"
#include "envoy/network/listen_socket.h"
#include "envoy/network/listener.h"
#include "envoy/server/listener_manager.h"
#include "envoy/stats/timespan.h"
#include "common/common/linked_object.h"
#include "common/common/non_copyable.h"
#include "spdlog/spdlog.h"
namespace Envoy {
namespace Server {
// clang-format off
#define ALL_LISTENER_STATS(COUNTER, GAUGE, HISTOGRAM) \
COUNTER (downstream_cx_total) \
COUNTER (downstream_cx_destroy) \
GAUGE (downstream_cx_active) \
HISTOGRAM(downstream_cx_length_ms)
// clang-format on
/**
* Wrapper struct for listener stats. @see stats_macros.h
*/
struct ListenerStats {
ALL_LISTENER_STATS(GENERATE_COUNTER_STRUCT, GENERATE_GAUGE_STRUCT, GENERATE_HISTOGRAM_STRUCT)
};
/**
* Server side connection handler. This is used both by workers as well as the
* main thread for non-threaded listeners.
*/
class ConnectionHandlerImpl : public Network::ConnectionHandler, NonCopyable {
public:
ConnectionHandlerImpl(spdlog::logger& logger, Event::Dispatcher& dispatcher);
// Network::ConnectionHandler
uint64_t numConnections() override { return num_connections_; }
void addListener(Network::ListenerConfig& config) override;
void removeListeners(uint64_t listener_tag) override;
void stopListeners(uint64_t listener_tag) override;
void stopListeners() override;
Network::Listener* findListenerByAddress(const Network::Address::Instance& address) override;
private:
struct ActiveListener;
ActiveListener* findActiveListenerByAddress(const Network::Address::Instance& address);
struct ActiveConnection;
typedef std::unique_ptr<ActiveConnection> ActiveConnectionPtr;
struct ActiveSocket;
typedef std::unique_ptr<ActiveSocket> ActiveSocketPtr;
/**
* Wrapper for an active listener owned by this handler.
*/
struct ActiveListener : public Network::ListenerCallbacks {
ActiveListener(ConnectionHandlerImpl& parent, Network::ListenerConfig& config);
ActiveListener(ConnectionHandlerImpl& parent, Network::ListenerPtr&& listener,
Network::ListenerConfig& config);
~ActiveListener();
// Network::ListenerCallbacks
void onAccept(Network::ConnectionSocketPtr&& socket,
bool hand_off_restored_destination_connections) override;
void onNewConnection(Network::ConnectionPtr&& new_connection) override;
/**
* Remove and destroy an active connection.
* @param connection supplies the connection to remove.
*/
void removeConnection(ActiveConnection& connection);
/**
* Create a new connection from a socket accepted by the listener.
*/
void newConnection(Network::ConnectionSocketPtr&& socket);
ConnectionHandlerImpl& parent_;
Network::ListenerPtr listener_;
ListenerStats stats_;
std::list<ActiveSocketPtr> sockets_;
std::list<ActiveConnectionPtr> connections_;
const uint64_t listener_tag_;
Network::ListenerConfig& config_;
};
typedef std::unique_ptr<ActiveListener> ActiveListenerPtr;
/**
* Wrapper for an active connection owned by this handler.
*/
struct ActiveConnection : LinkedObject<ActiveConnection>,
public Event::DeferredDeletable,
public Network::ConnectionCallbacks {
ActiveConnection(ActiveListener& listener, Network::ConnectionPtr&& new_connection);
~ActiveConnection();
// Network::ConnectionCallbacks
void onEvent(Network::ConnectionEvent event) override {
// Any event leads to destruction of the connection.
if (event == Network::ConnectionEvent::LocalClose ||
event == Network::ConnectionEvent::RemoteClose) {
listener_.removeConnection(*this);
}
}
void onAboveWriteBufferHighWatermark() override {}
void onBelowWriteBufferLowWatermark() override {}
ActiveListener& listener_;
Network::ConnectionPtr connection_;
Stats::TimespanPtr conn_length_;
};
/**
* Wrapper for an active accepted socket owned by this handler.
*/
struct ActiveSocket : public Network::ListenerFilterManager,
public Network::ListenerFilterCallbacks,
LinkedObject<ActiveSocket>,
public Event::DeferredDeletable {
ActiveSocket(ActiveListener& listener, Network::ConnectionSocketPtr&& socket,
bool hand_off_restored_destination_connections)
: listener_(listener), socket_(std::move(socket)),
hand_off_restored_destination_connections_(hand_off_restored_destination_connections),
iter_(accept_filters_.end()) {}
~ActiveSocket() { accept_filters_.clear(); }
// Network::ListenerFilterManager
void addAcceptFilter(Network::ListenerFilterPtr&& filter) override {
accept_filters_.emplace_back(std::move(filter));
}
// Network::ListenerFilterCallbacks
Network::ConnectionSocket& socket() override { return *socket_.get(); }
Event::Dispatcher& dispatcher() override { return listener_.parent_.dispatcher_; }
void continueFilterChain(bool success) override;
ActiveListener& listener_;
Network::ConnectionSocketPtr socket_;
const bool hand_off_restored_destination_connections_;
std::list<Network::ListenerFilterPtr> accept_filters_;
std::list<Network::ListenerFilterPtr>::iterator iter_;
};
static ListenerStats generateStats(Stats::Scope& scope);
spdlog::logger& logger_;
Event::Dispatcher& dispatcher_;
std::list<std::pair<Network::Address::InstanceConstSharedPtr, ActiveListenerPtr>> listeners_;
std::atomic<uint64_t> num_connections_{};
};
} // Server
} // namespace Envoy