Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions library/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,10 @@ void Engine::flushStats() {
server_->flushStats();
}

void Engine::drainConnections() {
ASSERT(dispatcher_->isThreadSafe(),
"drainConnections must be called from the dispatcher's context");
server_->clusterManager().drainConnections();
}

} // namespace Envoy
5 changes: 5 additions & 0 deletions library/common/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ class Engine : public Logger::Loggable<Logger::Id::main> {
*/
void flushStats();

/**
* Drain all upstream connections associated with this Engine.
*/
void drainConnections();

private:
envoy_status_t main(std::string config, std::string log_level);

Expand Down
15 changes: 14 additions & 1 deletion library/common/main_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ envoy_engine_t init_engine(envoy_engine_callbacks callbacks, envoy_logger logger
envoy_status_t run_engine(envoy_engine_t, const char* config, const char* log_level) {
// This will change once multiple engine support is in place.
// https://github.com/lyft/envoy-mobile/issues/332

if (auto e = engine()) {
e->run(config, log_level);
return ENVOY_SUCCESS;
Expand All @@ -200,3 +199,17 @@ void terminate_engine(envoy_engine_t) {
strong_engine_.reset();
e->terminate();
}

envoy_status_t drain_connections(envoy_engine_t) {
// This will change once multiple engine support is in place.
// https://github.com/lyft/envoy-mobile/issues/332
if (auto e = engine()) {
e->dispatcher().post([]() {
if (auto e = engine()) {
e->drainConnections();
}
});
return ENVOY_SUCCESS;
}
return ENVOY_FAILURE;
}
7 changes: 7 additions & 0 deletions library/common/main_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ envoy_status_t run_engine(envoy_engine_t engine, const char* config, const char*
*/
void terminate_engine(envoy_engine_t engine);

/**
* Drain all upstream connections associated with an engine
* @param engine, handle to the engine to drain.
* @return envoy_status_t, the resulting status of the operation.
*/
envoy_status_t drain_connections(envoy_engine_t engine);

#ifdef __cplusplus
} // functions
#endif
22 changes: 22 additions & 0 deletions test/common/main_interface_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,26 @@ TEST(EngineTest, EventTrackerRegistersAssertionFailureRecordAction) {
ASSERT_TRUE(test_context.on_exit.WaitForNotificationWithTimeout(absl::Seconds(3)));
}

TEST(MainInterfaceTest, DrainConnections) {
engine_test_context test_context{};
envoy_engine_callbacks engine_cbs{[](void* context) -> void {
auto* engine_running =
static_cast<engine_test_context*>(context);
engine_running->on_engine_running.Notify();
} /*on_engine_running*/,
[](void* context) -> void {
auto* exit = static_cast<engine_test_context*>(context);
exit->on_exit.Notify();
} /*on_exit*/,
&test_context /*context*/};
envoy_engine_t engine_handle = init_engine(engine_cbs, {}, {});
run_engine(engine_handle, MINIMAL_TEST_CONFIG.c_str(), LEVEL_DEBUG.c_str());
ASSERT_TRUE(test_context.on_engine_running.WaitForNotificationWithTimeout(absl::Seconds(3)));

ASSERT_EQ(ENVOY_SUCCESS, drain_connections(engine_handle));

terminate_engine(engine_handle);
ASSERT_TRUE(test_context.on_exit.WaitForNotificationWithTimeout(absl::Seconds(3)));
}

} // namespace Envoy