]> granicus.if.org Git - icinga2/commitdiff
EventQueue#WaitForEvent(): re-add timeout
authorAlexander A. Klimov <alexander.klimov@icinga.com>
Wed, 3 Apr 2019 07:53:45 +0000 (09:53 +0200)
committerAlexander A. Klimov <alexander.klimov@icinga.com>
Wed, 3 Apr 2019 07:53:45 +0000 (09:53 +0200)
lib/remote/eventqueue.cpp
lib/remote/eventqueue.hpp
lib/remote/eventshandler.cpp

index ea7474aaca5f10fec58ae966ebb601de734f15ad..017d3dc57956ab2c43a6345724f838f2b18281f3 100644 (file)
@@ -5,6 +5,7 @@
 #include "base/io-engine.hpp"
 #include "base/singleton.hpp"
 #include "base/logger.hpp"
+#include "base/utility.hpp"
 #include <boost/asio/spawn.hpp>
 
 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;
index 8f6a76c0b29c7df1b33de08e70afa0dc30c84d03..1a53baabb26d151c14b8c865c8e5914d748127b8 100644 (file)
@@ -32,7 +32,7 @@ public:
        void SetFilter(std::unique_ptr<Expression> 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<EventQueue::Ptr> GetQueuesForType(const String& type);
        static void UnregisterIfUnused(const String& name, const EventQueue::Ptr& queue);
index 20f655f860c0006bcfe002bb55f9ebb78fc3581d..5e8e0590173b310ea70e83995195340b2dc3c70b 100644 (file)
@@ -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);
+               }
        }
 }