Skip to content

Commit

Permalink
Change from map to array
Browse files Browse the repository at this point in the history
Signed-off-by: Sotiris Nanopoulos <sonanopo@microsoft.com>
  • Loading branch information
Sotiris Nanopoulos committed Dec 2, 2020
1 parent 6cc663b commit c378f5f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
3 changes: 2 additions & 1 deletion include/envoy/common/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ struct msghdr {
#define HANDLE_ERROR_PERM ERROR_ACCESS_DENIED
#define HANDLE_ERROR_INVALID ERROR_INVALID_HANDLE

#define ENVOY_WIN32_SIGTERM 1
#define ENVOY_WIN32_SIGNAL_COUNT 1
#define ENVOY_WIN32_SIGTERM 0

namespace Platform {
constexpr absl::string_view null_device_path{"NUL"};
Expand Down
11 changes: 8 additions & 3 deletions source/common/event/win32/signal_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ namespace Event {

SignalEventImpl::SignalEventImpl(DispatcherImpl& dispatcher, signal_t signal_num, SignalCb cb)
: cb_(cb) {
auto handler_it = eventBridgeHandlersSingleton::get().find(signal_num);
if (handler_it != eventBridgeHandlersSingleton::get().end()) {

if (signal_num > eventBridgeHandlersSingleton::get().size()) {
ENVOY_BUG(false, "Attempting to create SignalEventImpl with a signal id that exceeds the "
"number of supported signals.");
return;
}

if (eventBridgeHandlersSingleton::get()[signal_num]) {
return;
}
os_fd_t socks[2];
Api::SysCallIntResult result =
Api::OsSysCallsSingleton::get().socketpair(AF_INET, SOCK_STREAM, IPPROTO_TCP, socks);
Expand All @@ -33,7 +38,7 @@ SignalEventImpl::SignalEventImpl(DispatcherImpl& dispatcher, signal_t signal_num
cb_();
},
Event::FileTriggerType::Level, Event::FileReadyType::Read);
eventBridgeHandlersSingleton::get().insert({signal_num, write_handle});
eventBridgeHandlersSingleton::get()[signal_num] = write_handle;
}

} // namespace Event
Expand Down
2 changes: 1 addition & 1 deletion source/common/event/win32/signal_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ class SignalEventImpl : public SignalEvent {
// Here we have a map from signal types to IoHandle. When we write to this handle we trigger an
// event that notifies Envoy to act on the signal.
using eventBridgeHandlersSingleton =
ThreadSafeSingleton<absl::flat_hash_map<signal_t, std::shared_ptr<Network::IoHandle>>>;
ThreadSafeSingleton<std::array<std::shared_ptr<Network::IoHandle>, ENVOY_WIN32_SIGNAL_COUNT>>;
} // namespace Event
} // namespace Envoy
12 changes: 8 additions & 4 deletions source/exe/win32/platform_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) {
}
shutdown_pending = true;

auto eventBridgeHandlers = Event::eventBridgeHandlersSingleton::get();
auto handler_it = eventBridgeHandlers.find(ENVOY_WIN32_SIGTERM);
if (handler_it == eventBridgeHandlers.end() || !handler_it->second) {
auto handler = Event::eventBridgeHandlersSingleton::get()[ENVOY_WIN32_SIGTERM];
if (!handler) {
return 0;
}

// This code is executed as part of a thread running under a Windows
// context. For that reason we want to avoid allocating memory or
// taking locks. This is why we use dont want to just write to a socket
// to wake up the signal handler.
Buffer::OwnedImpl buffer;
constexpr absl::string_view data{"a"};
buffer.add(data);
auto result = handler_it->second->write(buffer);
auto result = handler->write(buffer);
RELEASE_ASSERT(result.rc_ == 1,
fmt::format("failed to write 1 byte: {}", result.err_->getErrorDetails()));

Expand Down
2 changes: 1 addition & 1 deletion test/exe/win32_outofproc_main_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ INSTANTIATE_TEST_SUITE_P(IpVersions, MainCommonTest,
testing::ValuesIn(TestEnvironment::getIpVersionsForTest()),
TestUtility::ipTestParamsToString);

TEST_P(MainCommonTest, Envoy1) {
TEST_P(MainCommonTest, EnvoyHandlesCtrlBreakEvent) {
createEnvoyProcess();
ENVOY_LOG_MISC(warn, "Envoy process with pid {}", piProcInfo_.dwProcessId);
Sleep(2 * 1000);
Expand Down

0 comments on commit c378f5f

Please sign in to comment.