From: Petr Špaček Date: Tue, 16 Apr 2019 10:13:43 +0000 (+0200) Subject: http: handle socket closure from net.close() X-Git-Tag: v4.0.0~4^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1305ee14e90d94ff7e5a0c8c0ae3eebf7f15cab0;p=thirdparty%2Fknot-resolver.git http: handle socket closure from net.close() --- diff --git a/modules/http/http.lua.in b/modules/http/http.lua.in index dd3fc3e40..33e80fa6e 100644 --- a/modules/http/http.lua.in +++ b/modules/http/http.lua.in @@ -316,12 +316,13 @@ M.templates.builtin.onerror = function(myserver, context, op, err, errno) -- lua end end --- @function Listen on given HTTP(s) host -function M.add_socket(socket, kind) - assert(M.servers[socket] == nil, 'socket is already served by an HTTP instance') +-- @function Listen on given socket +-- using configuration for specific "kind" of HTTP server +function add_socket(fd, kind) + assert(M.servers[fd] == nil, 'socket is already served by an HTTP instance') local conf, crt, key conf = mergeconf(M.templates.builtin, M.templates.default, M.templates[kind] or {}) - conf.socket = cqueues.socket.fdopen(socket) + conf.socket = cqueues.socket.fdopen(fd) if conf.tls ~= false then -- Check if a cert file was specified -- Read or create self-signed x509 certificate @@ -367,7 +368,7 @@ function M.add_socket(socket, kind) if err then panic('failed to listen on %s: %s', addr_str, err) end - M.servers[socket] = {kind = kind, server = s, config = conf} + M.servers[fd] = {kind = kind, server = s, config = conf} -- Create certificate renewal timer if ephemeral if crt and conf.ephemeral then local _, expiry = crt:getLifetime() @@ -381,6 +382,16 @@ function M.add_socket(socket, kind) end end +-- @function Stop listening on given socket +function remove_socket(fd) + local instance = M.servers[fd] + assert(instance, 'HTTP module is not listening on given socket') + + instance.server:close() + M.servers[fd] = nil + -- TODO stop refresh timer +end + -- @function Init module function M.init() -- collect and merge metrics only on leader @@ -393,10 +404,8 @@ end -- @function Cleanup module function M.deinit() - for socket, instance in pairs(M.servers) do - instance.server:close() - M.servers[socket] = nil - -- TODO stop refresh timer + for fd, _ in pairs(M.servers) do + remove_socket(fd) end prometheus.deinit() net.register_endpoint_kind('doh') @@ -409,7 +418,11 @@ function cb_socket(...) endpoint = ffi.cast('struct endpoint **', endpoint)[0] local kind = ffi.string(endpoint.flags.kind) local socket = endpoint.fd - M.add_socket(socket, kind) + if added then + return add_socket(socket, kind) + else + return remove_socket(socket) + end end -- @function Configure module, i.e. store configuration template and restart servers