From d5593cbbdfd9b69679c7bc46111f80f407872fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sat, 12 Jul 2014 12:21:31 +0200 Subject: [PATCH] Avoid spinning the event loop due to unprocessed UDP data. Fixes #715. Also fixes Libevent2UDPConnection.close() to close the actual underlying event. --- source/vibe/core/drivers/libevent2.d | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/source/vibe/core/drivers/libevent2.d b/source/vibe/core/drivers/libevent2.d index 3d1e3a8a1d..b01f1620a0 100644 --- a/source/vibe/core/drivers/libevent2.d +++ b/source/vibe/core/drivers/libevent2.d @@ -847,10 +847,8 @@ final class Libevent2UDPConnection : UDPConnection { // create a context for storing connection information m_ctx = TCPContextAlloc.alloc(driver.m_core, driver.m_eventLoop, sockfd, null, bind_addr, NetworkAddress()); - auto evt = event_new(driver.m_eventLoop, sockfd, EV_READ|EV_PERSIST, &onUDPRead, m_ctx); - if( !evt ) throw new Exception("Failed to create buffer event for socket."); - - enforce(event_add(evt, null) == 0); + m_ctx.listenEvent = event_new(driver.m_eventLoop, sockfd, EV_READ|EV_PERSIST, &onUDPRead, m_ctx); + if (!m_ctx.listenEvent) throw new Exception("Failed to create buffer event for socket."); } @property string bindAddress() const { return m_bindAddressString; } @@ -891,7 +889,7 @@ final class Libevent2UDPConnection : UDPConnection { if (!m_ctx) return; acquire(); - if (m_ctx.event) bufferevent_free(m_ctx.event); + if (m_ctx.listenEvent) event_free(m_ctx.listenEvent); TCPContextAlloc.free(m_ctx); m_ctx = null; } @@ -937,8 +935,13 @@ final class Libevent2UDPConnection : UDPConnection { } acquire(); + // TODO: adds the event only when we actually read to avoid event loop + // spinning when data is available, see #715. Since this may be + // performance critical, a proper benchmark should be performed! + enforce(event_add(m_ctx.listenEvent, null) == 0); scope (exit) { + event_del(m_ctx.listenEvent); release(); if (tm != size_t.max) m_driver.releaseTimer(tm); }