comparison net/unbound.lua @ 14209:c4aa9b2b4787 13.0

net.unbound: Simplify by removing cancel() Not aware that it is used anywhere. Removes potential "leak" by forgetting to remove the entry from the now deleted table.
author Kim Alvefur <zash@zash.se>
date Sat, 30 May 2026 19:21:08 +0200
parents 9c062f243d3a
children 41fa97090264
comparison
equal deleted inserted replaced
14208:9c062f243d3a 14209:c4aa9b2b4787
80 local _string = t_concat(t, "\n"); 80 local _string = t_concat(t, "\n");
81 self._string = _string; 81 self._string = _string;
82 return _string; 82 return _string;
83 end; 83 end;
84 }; 84 };
85
86 local waiting_queries = {};
87 85
88 local function prep_answer(a) 86 local function prep_answer(a)
89 if not a then return end 87 if not a then return end
90 local status = errors[a.rcode]; 88 local status = errors[a.rcode];
91 local qclass = classes[a.qclass]; 89 local qclass = classes[a.qclass];
115 qtype = qtype and s_upper(qtype) or "A"; 113 qtype = qtype and s_upper(qtype) or "A";
116 qclass = qclass and s_upper(qclass) or "IN"; 114 qclass = qclass and s_upper(qclass) or "IN";
117 local ntype, nclass = types[qtype], classes[qclass]; 115 local ntype, nclass = types[qtype], classes[qclass];
118 116
119 local m; 117 local m;
120 local ret;
121 local log_query = logger.init("unbound.query"..new_id()); 118 local log_query = logger.init("unbound.query"..new_id());
122 local function callback_wrapper(a, err) 119 local function callback_wrapper(a, err)
123 m(); 120 m();
124 waiting_queries[ret] = nil;
125 if a then 121 if a then
126 prep_answer(a); 122 prep_answer(a);
127 log_query("debug", "Results for %s %s %s: %s (%s)", qname, qclass, qtype, a.rcode == 0 and (#a .. " items") or a.status, 123 log_query("debug", "Results for %s %s %s: %s (%s)", qname, qclass, qtype, a.rcode == 0 and (#a .. " items") or a.status,
128 a.secure and "Secure" or a.bogus or "Insecure"); -- Insecure as in unsigned 124 a.secure and "Secure" or a.bogus or "Insecure"); -- Insecure as in unsigned
129 else 125 else
132 local ok, cerr = pcall(callback, a, err); 128 local ok, cerr = pcall(callback, a, err);
133 if not ok then log_query("error", "Error in callback: %s", cerr); end 129 if not ok then log_query("error", "Error in callback: %s", cerr); end
134 end 130 end
135 log_query("debug", "Resolve %s %s %s", qname, qclass, qtype); 131 log_query("debug", "Resolve %s %s %s", qname, qclass, qtype);
136 m = measure(qclass, qtype); 132 m = measure(qclass, qtype);
137 local err; 133 local ret, err = unbound:resolve_async(callback_wrapper, qname, ntype, nclass);
138 ret, err = unbound:resolve_async(callback_wrapper, qname, ntype, nclass); 134 if not ret then
139 if ret then
140 waiting_queries[ret] = callback;
141 else
142 log_query("error", "Resolver error: %s", err); 135 log_query("error", "Resolver error: %s", err);
143 end 136 end
144 return ret, err; 137 return ret, err;
145 end 138 end
146 139
152 local a, err = unbound:resolve(qname, ntype, nclass); 145 local a, err = unbound:resolve(qname, ntype, nclass);
153 if not a then return a, err; end 146 if not a then return a, err; end
154 return prep_answer(a); 147 return prep_answer(a);
155 end 148 end
156 149
157 local function cancel(id)
158 local cb = waiting_queries[id];
159 unbound:cancel(id);
160 if cb then
161 cb(nil, "canceled");
162 waiting_queries[id] = nil;
163 end
164 return true;
165 end
166
167 -- Reinitiate libunbound context, drops cache 150 -- Reinitiate libunbound context, drops cache
168 local function purge() 151 local function purge()
169 if server_conn then 152 if server_conn then
170 local old_unbound, old_server_conn = unbound, server_conn; 153 local old_unbound, old_server_conn = unbound, server_conn;
171 timer.add_task(30, function() 154 timer.add_task(30, function()
172 old_server_conn:close(); 155 old_server_conn:close();
173 -- Blocking wait for any straggler queries to prevent "leak" of waiting_queries[] entries 156 if old_unbound.cancelall then
174 old_unbound:wait(); 157 old_unbound:cancelall();
158 end
175 end) 159 end)
176 end 160 end
177 initialize(); 161 initialize();
178 return true; 162 return true;
179 end 163 end
192 error "not implemented"; 176 error "not implemented";
193 end 177 end
194 -- Public API 178 -- Public API
195 local _M = { 179 local _M = {
196 lookup = lookup; 180 lookup = lookup;
197 cancel = cancel; 181 cancel = not_implemented;
198 new_async_socket = not_implemented; 182 new_async_socket = not_implemented;
199 dns = { 183 dns = {
200 lookup = lookup_sync; 184 lookup = lookup_sync;
201 cancel = cancel; 185 cancel = not_implemented;
202 cache = noop; 186 cache = noop;
203 socket_wrapper_set = noop; 187 socket_wrapper_set = noop;
204 settimeout = noop; 188 settimeout = noop;
205 query = noop; 189 query = noop;
206 purge = purge; 190 purge = purge;