Software /
code /
prosody
Comparison
core/moduleapi.lua @ 10214:f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
All cleanup in one spot instead of two, and at the end which fits with
cleanup happening afterwards.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 21 Aug 2019 23:18:08 +0200 |
parent | 10213:ee62754b0233 |
child | 10575:5cf8e96575b3 |
comparison
equal
deleted
inserted
replaced
10213:ee62754b0233 | 10214:f864e685e618 |
---|---|
372 iq_cache = cache.new(256, function (_, iq) | 372 iq_cache = cache.new(256, function (_, iq) |
373 iq.reject(errutil.new({ | 373 iq.reject(errutil.new({ |
374 type = "wait", condition = "resource-constraint", | 374 type = "wait", condition = "resource-constraint", |
375 text = "evicted from iq tracking cache" | 375 text = "evicted from iq tracking cache" |
376 })); | 376 })); |
377 self:unhook(iq.result_event, iq.result_handler); | |
378 self:unhook(iq.error_event, iq.error_handler); | |
379 end); | 377 end); |
380 self._iq_cache = iq_cache; | 378 self._iq_cache = iq_cache; |
381 end | 379 end |
382 return promise.new(function (resolve, reject) | 380 |
383 local event_type; | 381 local event_type; |
384 if stanza.attr.from == self.host then | 382 if stanza.attr.from == self.host then |
385 event_type = "host"; | 383 event_type = "host"; |
386 else -- assume bare since we can't hook full jids | 384 else -- assume bare since we can't hook full jids |
387 event_type = "bare"; | 385 event_type = "bare"; |
388 end | 386 end |
389 local result_event = "iq-result/"..event_type.."/"..stanza.attr.id; | 387 local result_event = "iq-result/"..event_type.."/"..stanza.attr.id; |
390 local error_event = "iq-error/"..event_type.."/"..stanza.attr.id; | 388 local error_event = "iq-error/"..event_type.."/"..stanza.attr.id; |
391 local cache_key = event_type.."/"..stanza.attr.id; | 389 local cache_key = event_type.."/"..stanza.attr.id; |
392 | 390 |
391 local p = promise.new(function (resolve, reject) | |
393 local function result_handler(event) | 392 local function result_handler(event) |
394 if event.stanza.attr.from == stanza.attr.to then | 393 if event.stanza.attr.from == stanza.attr.to then |
395 resolve(event); | 394 resolve(event); |
396 return true; | 395 return true; |
397 end | 396 end |
418 local timeout_handle = self:add_timer(timeout or 120, function () | 417 local timeout_handle = self:add_timer(timeout or 120, function () |
419 reject(errutil.new({ | 418 reject(errutil.new({ |
420 type = "wait", condition = "remote-server-timeout", | 419 type = "wait", condition = "remote-server-timeout", |
421 text = "IQ stanza timed out", | 420 text = "IQ stanza timed out", |
422 })); | 421 })); |
423 self:unhook(result_event, result_handler); | |
424 self:unhook(error_event, error_handler); | |
425 iq_cache:set(cache_key, nil); | |
426 end); | 422 end); |
427 | 423 |
428 local ok = iq_cache:set(cache_key, { | 424 local ok = iq_cache:set(cache_key, { |
429 reject = reject, resolve = resolve, | 425 reject = reject, resolve = resolve, |
430 timeout_handle = timeout_handle, | 426 timeout_handle = timeout_handle, |
431 result_event = result_event, error_event = error_event, | |
432 result_handler = result_handler, error_handler = error_handler; | 427 result_handler = result_handler, error_handler = error_handler; |
433 }); | 428 }); |
434 | 429 |
435 if not ok then | 430 if not ok then |
436 reject(errutil.new({ | 431 reject(errutil.new({ |
440 return; | 435 return; |
441 end | 436 end |
442 | 437 |
443 self:send(stanza, origin); | 438 self:send(stanza, origin); |
444 end); | 439 end); |
440 | |
441 p:finally(function () | |
442 local iq = iq_cache:get(cache_key); | |
443 if iq then | |
444 self:unhook(result_event, iq.result_handler); | |
445 self:unhook(error_event, iq.error_handler); | |
446 iq.timeout_handle:stop(); | |
447 iq_cache:set(cache_key, nil); | |
448 end | |
449 end); | |
450 | |
451 return p; | |
445 end | 452 end |
446 | 453 |
447 function api:broadcast(jids, stanza, iter) | 454 function api:broadcast(jids, stanza, iter) |
448 for jid in (iter or it.values)(jids) do | 455 for jid in (iter or it.values)(jids) do |
449 local new_stanza = st.clone(stanza); | 456 local new_stanza = st.clone(stanza); |