moonbridge
changeset 279:dec24bd2592c
Close idle HTTP connections on SIGTERM
author | jbe |
---|---|
date | Tue Jun 06 20:19:09 2017 +0200 (2017-06-06) |
parents | d4c82c90d244 |
children | 1a4f89f4c712 |
files | moonbridge_http.lua |
line diff
1.1 --- a/moonbridge_http.lua Tue Jun 06 20:01:29 2017 +0200 1.2 +++ b/moonbridge_http.lua Tue Jun 06 20:19:09 2017 +0200 1.3 @@ -203,9 +203,15 @@ 1.4 local response_timeout = default("response_timeout", 3600) 1.5 local drain_timeout = default("drain_timeout", 2) 1.6 local poll = options.poll_function or moonbridge_io.poll 1.7 + -- install SIGTERM handler: 1.8 + local sigterm_socket = moonbridge_io.signalsocket("TERM") 1.9 -- return socket handler: 1.10 return function(socket) 1.11 local socket_set = {[socket] = true} -- used for poll function 1.12 + local socket_term_set = { -- used for poll function 1.13 + [socket] = true, 1.14 + [sigterm_socket] = true -- also wakeup if SIGTERM has been received 1.15 + } 1.16 local survive = true -- set to false if process shall be terminated later 1.17 local consume -- can be set to function that reads some input if possible 1.18 -- function that may be used as "consume" function 1.19 @@ -1094,9 +1100,12 @@ 1.20 return true -- success 1.21 end 1.22 -- wait for input: 1.23 - if not poll(socket_set, nil, idle_timeout) then 1.24 + if not poll(socket_term_set, nil, idle_timeout) then 1.25 return request_error(false, "408 Request Timeout", "Idle connection timed out") 1.26 end 1.27 + if #assert(sigterm_socket:read_nb()) > 0 then 1.28 + return request_error(false, "408 Request Timeout", "Server shutdown") 1.29 + end 1.30 -- read headers (with timeout): 1.31 do 1.32 local coro = coroutine.wrap(read_headers)