From 3cb0891c58ec0535a30422265e352ae81b8bc27a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sat, 17 Mar 2018 17:14:54 +0100 Subject: [PATCH] Create the DNS helper event lazily. Reduces resource usage on threads that don't perform DNS queries. --- source/eventcore/drivers/posix/dns.d | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/source/eventcore/drivers/posix/dns.d b/source/eventcore/drivers/posix/dns.d index babcf08..be1f129 100644 --- a/source/eventcore/drivers/posix/dns.d +++ b/source/eventcore/drivers/posix/dns.d @@ -38,21 +38,23 @@ final class EventDriverDNS_GAI(Events : EventDriverEvents, Signals : EventDriver } ChoppedVector!Lookup m_lookups; Events m_events; - EventID m_event; + EventID m_event = EventID.invalid; size_t m_maxHandle; } this(Events events, Signals signals) { m_events = events; - m_event = events.createInternal(); - m_events.wait(m_event, &onDNSSignal); + setupEvent(); } void dispose() { - m_events.cancelWait(m_event, &onDNSSignal); - m_events.releaseRef(m_event); + if (m_event != EventID.invalid) { + m_events.cancelWait(m_event, &onDNSSignal); + m_events.releaseRef(m_event); + m_event = EventID.invalid; + } } override DNSLookupID lookupHost(string name, DNSLookupCallback on_lookup_finished) @@ -61,6 +63,8 @@ final class EventDriverDNS_GAI(Events : EventDriverEvents, Signals : EventDriver auto handle = getFreeHandle(); if (handle > m_maxHandle) m_maxHandle = handle; + setupEvent(); + assert(!m_lookups[handle].result); Lookup* l = () @trusted { return &m_lookups[handle]; } (); l.name = name; @@ -131,6 +135,14 @@ final class EventDriverDNS_GAI(Events : EventDriverEvents, Signals : EventDriver return cast(DNSLookupID)cast(int)i; return cast(DNSLookupID)cast(int)m_lookups.length; } + + private void setupEvent() + { + if (m_event == EventID.invalid) { + m_event = m_events.createInternal(); + m_events.wait(m_event, &onDNSSignal); + } + } }