net/cqueues.lua
author Kim Alvefur <zash@zash.se>
Sat, 23 Mar 2024 20:48:19 +0100
changeset 13465 c673ff1075bd
parent 12978 ba409c67353b
permissions -rw-r--r--
mod_posix: Move everything to util.startup This allows greater control over the order of events. Notably, the internal ordering between daemonization, initialization of libunbound and setup of signal handling is sensitive. libunbound starts a separate thread for processing DNS requests. If this thread is started before signal handling has been set up, it will not inherit the signal handlers and instead behave as it would have before signal handlers were set up, i.e. cause the whole process to immediately exit. libunbound is usually initialized on the first DNS request, usually triggered by an outgoing s2s connection attempt. If daemonization happens before signals have been set up, signals may not be processed at all.

-- Prosody IM
-- Copyright (C) 2014 Daurnimator
--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
-- This module allows you to use cqueues with a net.server mainloop
--

local server = require "prosody.net.server";
local cqueues = require "cqueues";
local timer = require "prosody.util.timer";
assert(cqueues.VERSION >= 20150113, "cqueues newer than 20150113 required")

-- Create a single top level cqueue
local cq;

if server.cq then -- server provides cqueues object
	cq = server.cq;
elseif server.watchfd then
	cq = cqueues.new();
	local timeout = timer.add_task(cq:timeout() or 0, function ()
		-- FIXME It should be enough to reschedule this timeout instead of replacing it, but this does not work.  See https://issues.prosody.im/1572
		assert(cq:loop(0));
		return cq:timeout();
	end);
	server.watchfd(cq:pollfd(), function ()
		assert(cq:loop(0));
		local t = cq:timeout();
		if t then
			timer.stop(timeout);
			timeout = timer.add_task(cq:timeout(), function ()
				assert(cq:loop(0));
				return cq:timeout();
			end);
		end
	end);
else
	error "NYI"
end

return {
	cq = cq;
}