Software /
code /
prosody-modules
Annotate
mod_graceful_shutdown/mod_graceful_shutdown.lua @ 4892:8dbaa5e753f3
mod_graceful_shutdown: Deactivate direct TLS c2s along with plain c2s
Stops clients from reconnecting that way after their sessions are closed
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 11 Feb 2022 16:11:55 +0100 |
parent | 2170:4652a112a4ba |
child | 4893:d4ce29c772ac |
rev | line source |
---|---|
2170
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- luacheck: ignore 122/prosody 113/prosody |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local timer = require "util.timer"; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local portman = require "core.portmanager"; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local server = require "net.server"; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 module:set_global(); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local orig_shutdown = prosody.shutdown; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local pause = module:get_option_number("shutdown_pause", 1); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 function module.unload() |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 prosody.shutdown = orig_shutdown; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 end |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 prosody.shutdown = coroutine.wrap(function (reason, code) |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 prosody.shutdown_reason = reason; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 prosody.shutdown_code = code; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 timer.add_task(pause, prosody.shutdown); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 coroutine.yield(true, "shutdown initiated"); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 -- Close c2s ports, stop accepting new connections |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 portman.deactivate("c2s"); |
4892
8dbaa5e753f3
mod_graceful_shutdown: Deactivate direct TLS c2s along with plain c2s
Kim Alvefur <zash@zash.se>
parents:
2170
diff
changeset
|
23 portman.deactivate("legacy_ssl"); |
8dbaa5e753f3
mod_graceful_shutdown: Deactivate direct TLS c2s along with plain c2s
Kim Alvefur <zash@zash.se>
parents:
2170
diff
changeset
|
24 portman.deactivate("c2s_direct_tls"); |
8dbaa5e753f3
mod_graceful_shutdown: Deactivate direct TLS c2s along with plain c2s
Kim Alvefur <zash@zash.se>
parents:
2170
diff
changeset
|
25 |
2170
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 -- Close all c2s sessions |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 for _, sess in pairs(prosody.full_sessions) do |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 sess:close{ condition = "system-shutdown", text = reason } |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 end |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 -- Wait for notifications to be sent |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 coroutine.yield(pause); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 -- Event for everything else to shut down |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 prosody.events.fire_event("server-stopping", { |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 reason = reason; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 code = code; |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 }); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 -- And wait |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 coroutine.yield(pause); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 -- And stop main event loop |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 server.setquitting(true); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 -- And wait for death |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 coroutine.yield(pause * 3); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 -- you came back? die zombie! |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 os.exit(1); |
4652a112a4ba
mod_graceful_shutdown: Experiment in improving the shutdown experience
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 end); |