Create the DNS helper event lazily.

Reduces resource usage on threads that don't perform DNS queries.
This commit is contained in:
Sönke Ludwig 2018-03-17 17:14:54 +01:00
parent eaf7ab778b
commit 3cb0891c58

View file

@ -38,21 +38,23 @@ final class EventDriverDNS_GAI(Events : EventDriverEvents, Signals : EventDriver
} }
ChoppedVector!Lookup m_lookups; ChoppedVector!Lookup m_lookups;
Events m_events; Events m_events;
EventID m_event; EventID m_event = EventID.invalid;
size_t m_maxHandle; size_t m_maxHandle;
} }
this(Events events, Signals signals) this(Events events, Signals signals)
{ {
m_events = events; m_events = events;
m_event = events.createInternal(); setupEvent();
m_events.wait(m_event, &onDNSSignal);
} }
void dispose() void dispose()
{ {
m_events.cancelWait(m_event, &onDNSSignal); if (m_event != EventID.invalid) {
m_events.releaseRef(m_event); m_events.cancelWait(m_event, &onDNSSignal);
m_events.releaseRef(m_event);
m_event = EventID.invalid;
}
} }
override DNSLookupID lookupHost(string name, DNSLookupCallback on_lookup_finished) override DNSLookupID lookupHost(string name, DNSLookupCallback on_lookup_finished)
@ -61,6 +63,8 @@ final class EventDriverDNS_GAI(Events : EventDriverEvents, Signals : EventDriver
auto handle = getFreeHandle(); auto handle = getFreeHandle();
if (handle > m_maxHandle) m_maxHandle = handle; if (handle > m_maxHandle) m_maxHandle = handle;
setupEvent();
assert(!m_lookups[handle].result); assert(!m_lookups[handle].result);
Lookup* l = () @trusted { return &m_lookups[handle]; } (); Lookup* l = () @trusted { return &m_lookups[handle]; } ();
l.name = name; 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)i;
return cast(DNSLookupID)cast(int)m_lookups.length; 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);
}
}
} }