From 4c5ee0dbbfdde5f658706c321ba67741d6657e50 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 3 Apr 2019 09:53:45 +0200 Subject: [PATCH] EventQueue#WaitForEvent(): re-add timeout --- lib/remote/eventqueue.cpp | 13 +++++++++++-- lib/remote/eventqueue.hpp | 2 +- lib/remote/eventshandler.cpp | 18 +++++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/remote/eventqueue.cpp b/lib/remote/eventqueue.cpp index ea7474aac..017d3dc57 100644 --- a/lib/remote/eventqueue.cpp +++ b/lib/remote/eventqueue.cpp @@ -5,6 +5,7 @@ #include "base/io-engine.hpp" #include "base/singleton.hpp" #include "base/logger.hpp" +#include "base/utility.hpp" #include using namespace icinga; @@ -102,8 +103,10 @@ Dictionary::Ptr EventQueue::WaitForEvent(void *client, double timeout) } } -Dictionary::Ptr EventQueue::WaitForEvent(void *client, boost::asio::yield_context yc) +Dictionary::Ptr EventQueue::WaitForEvent(void *client, boost::asio::yield_context yc, double timeout) { + double deadline = -1.0; + for (;;) { { boost::mutex::scoped_try_lock lock(m_Mutex); @@ -112,7 +115,13 @@ Dictionary::Ptr EventQueue::WaitForEvent(void *client, boost::asio::yield_contex auto it = m_Events.find(client); ASSERT(it != m_Events.end()); - if (!it->second.empty()) { + if (it->second.empty()) { + if (deadline == -1.0) { + deadline = Utility::GetTime() + timeout; + } else if (Utility::GetTime() >= deadline) { + return nullptr; + } + } else { Dictionary::Ptr result = *it->second.begin(); it->second.pop_front(); return result; diff --git a/lib/remote/eventqueue.hpp b/lib/remote/eventqueue.hpp index 8f6a76c0b..1a53baabb 100644 --- a/lib/remote/eventqueue.hpp +++ b/lib/remote/eventqueue.hpp @@ -32,7 +32,7 @@ public: void SetFilter(std::unique_ptr filter); Dictionary::Ptr WaitForEvent(void *client, double timeout = 5); - Dictionary::Ptr WaitForEvent(void *client, boost::asio::yield_context yc); + Dictionary::Ptr WaitForEvent(void *client, boost::asio::yield_context yc, double timeout = 5); static std::vector GetQueuesForType(const String& type); static void UnregisterIfUnused(const String& name, const EventQueue::Ptr& queue); diff --git a/lib/remote/eventshandler.cpp b/lib/remote/eventshandler.cpp index 20f655f86..5e8e05901 100644 --- a/lib/remote/eventshandler.cpp +++ b/lib/remote/eventshandler.cpp @@ -103,17 +103,21 @@ bool EventsHandler::HandleRequest( asio::const_buffer newLine ("\n", 1); for (;;) { - String body = JsonEncode(queue->WaitForEvent(&request, yc)); + auto event (queue->WaitForEvent(&request, yc)); - boost::algorithm::replace_all(body, "\n", ""); + if (event) { + String body = JsonEncode(event); - asio::const_buffer payload (body.CStr(), body.GetLength()); + boost::algorithm::replace_all(body, "\n", ""); - IoBoundWorkSlot dontLockTheIoThreadWhileWriting (yc); + asio::const_buffer payload (body.CStr(), body.GetLength()); - asio::async_write(stream, payload, yc); - asio::async_write(stream, newLine, yc); - stream.async_flush(yc); + IoBoundWorkSlot dontLockTheIoThreadWhileWriting (yc); + + asio::async_write(stream, payload, yc); + asio::async_write(stream, newLine, yc); + stream.async_flush(yc); + } } } -- 2.40.0