# HG changeset patch
# User Kim Alvefur <zash@zash.se>
# Date 1609879206 -3600
# Node ID 919e7b962f0b9968842dae8a3576a7f25579e1f6
# Parent  ba335004ca6098768678cdde717163762e4e41aa
net.unbound: Delay loading until server has started or first query

Shouldn't need a DNS resolver until later anyways. Might even be
sensible to only initialize if a query is actually attempted.

diff -r ba335004ca60 -r 919e7b962f0b net/unbound.lua
--- a/net/unbound.lua	Tue Jan 05 21:36:04 2021 +0100
+++ b/net/unbound.lua	Tue Jan 05 21:40:06 2021 +0100
@@ -60,7 +60,9 @@
 	unbound = libunbound.new(unbound_config);
 	server_conn = connect_server(unbound, net_server);
 end
-initialize();
+if prosody then
+	prosody.events.add_handler("server-started", initialize);
+end
 
 local answer_mt = {
 	__tostring = function(self)
@@ -105,6 +107,7 @@
 end
 
 local function lookup(callback, qname, qtype, qclass)
+	if not unbound then initialize(); end
 	qtype = qtype and s_upper(qtype) or "A";
 	qclass = qclass and s_upper(qclass) or "IN";
 	local ntype, nclass = types[qtype], classes[qclass];