Skip to content

Commit ce5953b

Browse files
authored
worker: provide removeFilterChain interface (envoyproxy#10528)
Signed-off-by: Yuchen Dai <silentdai@gmail.com>
1 parent 3f515cc commit ce5953b

25 files changed

Lines changed: 732 additions & 175 deletions

include/envoy/network/connection_handler.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ class ConnectionHandler {
3838
virtual void decNumConnections() PURE;
3939

4040
/**
41-
* Adds a listener to the handler.
41+
* Adds a listener to the handler, optionally replacing the existing listener.
42+
* @param overridden_listener tag of the existing listener. nullopt if no previous listener.
4243
* @param config listener configuration options.
4344
*/
44-
virtual void addListener(ListenerConfig& config) PURE;
45+
virtual void addListener(absl::optional<uint64_t> overridden_listener,
46+
ListenerConfig& config) PURE;
4547

4648
/**
4749
* Remove listeners using the listener tag as a key. All connections owned by the removed
@@ -50,6 +52,17 @@ class ConnectionHandler {
5052
*/
5153
virtual void removeListeners(uint64_t listener_tag) PURE;
5254

55+
/**
56+
* Remove the filter chains and the connections in the listener. All connections owned
57+
* by the filter chains will be closed. Once all the connections are destroyed(connections
58+
* could be deferred deleted!), invoke the completion.
59+
* @param listener_tag supplies the tag passed to addListener().
60+
* @param filter_chains supplies the filter chains to be removed.
61+
*/
62+
virtual void removeFilterChains(uint64_t listener_tag,
63+
const std::list<const FilterChain*>& filter_chains,
64+
std::function<void()> completion) PURE;
65+
5366
/**
5467
* Stop listeners using the listener tag as a key. This will not close any connections and is used
5568
* for draining.

include/envoy/server/worker.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ class Worker {
2424
using AddListenerCompletion = std::function<void(bool success)>;
2525

2626
/**
27-
* Add a listener to the worker.
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.
2831
* @param listener supplies the listener to add.
2932
* @param completion supplies the completion to call when the listener has been added (or not) on
3033
* the worker.
3134
*/
32-
virtual void addListener(Network::ListenerConfig& listener,
35+
virtual void addListener(absl::optional<uint64_t> overridden_listener,
36+
Network::ListenerConfig& listener,
3337
AddListenerCompletion completion) PURE;
3438

3539
/**
@@ -63,6 +67,17 @@ class Worker {
6367
*/
6468
virtual void removeListener(Network::ListenerConfig& listener,
6569
std::function<void()> completion) PURE;
70+
/**
71+
* Remove the stale filter chains of the given listener but leave the listener running.
72+
* @param listener_tag supplies the tag passed to addListener().
73+
* @param filter_chains supplies the filter chains to be removed.
74+
* @param completion supplies the completion to be called when the listener removed all the
75+
* untracked connections. This completion is called on the worker thread. No locking is performed
76+
* by the worker.
77+
*/
78+
virtual void removeFilterChains(uint64_t listener_tag,
79+
const std::list<const Network::FilterChain*>& filter_chains,
80+
std::function<void()> completion) PURE;
6681

6782
/**
6883
* Stop a listener from accepting new connections. This is used for server draining.

source/common/event/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,12 @@ envoy_cc_library(
123123
"//source/common/common:scope_tracker",
124124
],
125125
)
126+
127+
envoy_cc_library(
128+
name = "deferred_task",
129+
hdrs = ["deferred_task.h"],
130+
deps = [
131+
"//include/envoy/event:deferred_deletable",
132+
"//include/envoy/event:dispatcher_interface",
133+
],
134+
)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include <memory>
4+
5+
#include "envoy/event/dispatcher.h"
6+
7+
namespace Envoy {
8+
namespace Event {
9+
10+
/**
11+
* A util to schedule a task to run in a future event loop cycle. One of the use cases is to run the
12+
* task after the previously DeferredDeletable objects are destroyed.
13+
*/
14+
class DeferredTaskUtil {
15+
private:
16+
class DeferredTask : public DeferredDeletable {
17+
public:
18+
DeferredTask(std::function<void()>&& task) : task_(std::move(task)) {}
19+
~DeferredTask() override { task_(); }
20+
21+
private:
22+
std::function<void()> task_;
23+
};
24+
25+
public:
26+
/**
27+
* Submits an item for run deferred delete.
28+
*/
29+
static void deferredRun(Dispatcher& dispatcher, std::function<void()>&& func) {
30+
dispatcher.deferredDelete(std::make_unique<DeferredTask>(std::move(func)));
31+
}
32+
};
33+
34+
} // namespace Event
35+
} // namespace Envoy

source/extensions/quic_listeners/quiche/active_quic_listener.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ ActiveQuicListener::ActiveQuicListener(Event::Dispatcher& dispatcher,
3131
Network::ListenerConfig& listener_config,
3232
const quic::QuicConfig& quic_config,
3333
Network::Socket::OptionsSharedPtr options)
34-
: Server::ConnectionHandlerImpl::ActiveListenerImplBase(parent, listener_config),
34+
: Server::ConnectionHandlerImpl::ActiveListenerImplBase(parent, &listener_config),
3535
dispatcher_(dispatcher), version_manager_(quic::CurrentSupportedVersions()),
3636
listen_socket_(*listen_socket) {
3737
if (options != nullptr) {
@@ -59,15 +59,15 @@ ActiveQuicListener::ActiveQuicListener(Event::Dispatcher& dispatcher,
5959
std::make_unique<EnvoyQuicAlarmFactory>(dispatcher_, *connection_helper->GetClock());
6060
quic_dispatcher_ = std::make_unique<EnvoyQuicDispatcher>(
6161
crypto_config_.get(), quic_config, &version_manager_, std::move(connection_helper),
62-
std::move(alarm_factory), quic::kQuicDefaultConnectionIdLength, parent, config_, stats_,
62+
std::move(alarm_factory), quic::kQuicDefaultConnectionIdLength, parent, *config_, stats_,
6363
per_worker_stats_, dispatcher, listen_socket_);
6464
quic_dispatcher_->InitializeWithWriter(new EnvoyQuicPacketWriter(listen_socket_));
6565
}
6666

6767
ActiveQuicListener::~ActiveQuicListener() { onListenerShutdown(); }
6868

6969
void ActiveQuicListener::onListenerShutdown() {
70-
ENVOY_LOG(info, "Quic listener {} shutdown.", config_.name());
70+
ENVOY_LOG(info, "Quic listener {} shutdown.", config_->name());
7171
quic_dispatcher_->Shutdown();
7272
udp_listener_.reset();
7373
}

source/server/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ envoy_cc_library(
7474
"//include/envoy/stats:timespan_interface",
7575
"//source/common/common:linked_object",
7676
"//source/common/common:non_copyable",
77+
"//source/common/event:deferred_task",
7778
"//source/common/network:connection_lib",
7879
"//source/common/stats:timespan_lib",
7980
"//source/common/stream_info:stream_info_lib",

0 commit comments

Comments
 (0)