Software /
code /
prosody
Annotate
net/server.lua @ 13221:2a9ce229efac
mod_storage_sql: Allow higher precision timestamps in SQLite3
Since it doesn't actually do strict typing :)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 17 Jul 2023 04:52:50 +0200 |
parent | 13166:e6e76f64ebb6 |
rev | line source |
---|---|
2925 | 1 -- Prosody IM |
2 -- Copyright (C) 2008-2010 Matthew Wild | |
3 -- Copyright (C) 2008-2010 Waqas Hussain | |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5531
diff
changeset
|
4 -- |
2925 | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | |
7 -- | |
2162
22b6b1899a55
net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents:
2136
diff
changeset
|
8 |
13166
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
9 local function log(level, format, ...) |
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
10 print("net.server", level, format:format(...)); |
9215
b087b5047f86
net.server: Throw error when loading outside Prosody or Prosody config not loaded
Matthew Wild <mwild1@gmail.com>
parents:
8711
diff
changeset
|
11 end |
b087b5047f86
net.server: Throw error when loading outside Prosody or Prosody config not loaded
Matthew Wild <mwild1@gmail.com>
parents:
8711
diff
changeset
|
12 |
13166
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
13 local default_backend = "select"; |
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
14 local server_type = default_backend; |
10853
0107ed6744aa
net.server: Switch to epoll backend by default
Matthew Wild <mwild1@gmail.com>
parents:
9215
diff
changeset
|
15 |
13166
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
16 if (prosody and prosody.config_loaded) then |
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
17 default_backend = "epoll"; |
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
18 log = require"prosody.util.logger".init("net.server"); |
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
19 server_type = require"prosody.core.configmanager".get("*", "network_backend") or default_backend; |
9215
b087b5047f86
net.server: Throw error when loading outside Prosody or Prosody config not loaded
Matthew Wild <mwild1@gmail.com>
parents:
8711
diff
changeset
|
20 |
13166
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
21 if require"prosody.core.configmanager".get("*", "use_libevent") then |
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
22 server_type = "event"; |
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
23 end |
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
24 elseif pcall(require, "prosody.util.poll") then |
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
25 server_type = "epoll"; |
6480
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
26 end |
2094
c69cb5c171e0
net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
6480
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
28 if server_type == "event" then |
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
29 if not pcall(require, "luaevent.core") then |
11762
54530085dffe
net.server: Fall back to default backend from libevent instead of always select
Kim Alvefur <zash@zash.se>
parents:
10853
diff
changeset
|
30 log("error", "libevent not found, falling back to %s", default_backend); |
54530085dffe
net.server: Fall back to default backend from libevent instead of always select
Kim Alvefur <zash@zash.se>
parents:
10853
diff
changeset
|
31 server_type = default_backend; |
2162
22b6b1899a55
net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents:
2136
diff
changeset
|
32 end |
22b6b1899a55
net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents:
2136
diff
changeset
|
33 end |
22b6b1899a55
net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents:
2136
diff
changeset
|
34 |
2094
c69cb5c171e0
net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 local server; |
6480
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
36 local set_config; |
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
37 if server_type == "event" then |
12974
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
38 server = require "prosody.net.server_event"; |
4808
07d0a3a75c8a
net.server, net.timer, net.server_select: Rearrange dependencies between these three modules. server.addtimer() is no longer a public function (renamed to _addtimer) and is not available at all from server_event (compat code removed to prevent traceback) (thanks Nulani)
Matthew Wild <mwild1@gmail.com>
parents:
3461
diff
changeset
|
39 |
5385
d663483036db
net.server, _select: Reorganise configuration of server_select to be more like server_event
Kim Alvefur <zash@zash.se>
parents:
5383
diff
changeset
|
40 local defaults = {}; |
6480
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
41 for k,v in pairs(server.cfg) do |
5385
d663483036db
net.server, _select: Reorganise configuration of server_select to be more like server_event
Kim Alvefur <zash@zash.se>
parents:
5383
diff
changeset
|
42 defaults[k] = v; |
d663483036db
net.server, _select: Reorganise configuration of server_select to be more like server_event
Kim Alvefur <zash@zash.se>
parents:
5383
diff
changeset
|
43 end |
6480
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
44 function set_config(settings) |
7543 | 45 local event_settings = { |
46 ACCEPT_DELAY = settings.accept_retry_interval; | |
47 ACCEPT_QUEUE = settings.tcp_backlog; | |
48 CLEAR_DELAY = settings.event_clear_interval; | |
49 CONNECT_TIMEOUT = settings.connect_timeout; | |
50 DEBUG = settings.debug; | |
51 HANDSHAKE_TIMEOUT = settings.ssl_handshake_timeout; | |
52 MAX_CONNECTIONS = settings.max_connections; | |
53 MAX_HANDSHAKE_ATTEMPTS = settings.max_ssl_handshake_roundtrips; | |
54 MAX_READ_LENGTH = settings.max_receive_buffer_size; | |
55 MAX_SEND_LENGTH = settings.max_send_buffer_size; | |
56 READ_TIMEOUT = settings.read_timeout; | |
57 WRITE_TIMEOUT = settings.send_timeout; | |
58 }; | |
5198
430797a8fc81
net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents:
4811
diff
changeset
|
59 |
7543 | 60 for k,default in pairs(defaults) do |
61 server.cfg[k] = event_settings[k] or default; | |
62 end | |
6480
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
63 end |
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
64 elseif server_type == "select" then |
11764
e2650d59db2d
net.server_select: Deprecate and warn about it
Kim Alvefur <zash@zash.se>
parents:
11763
diff
changeset
|
65 -- TODO Remove completely. |
e2650d59db2d
net.server_select: Deprecate and warn about it
Kim Alvefur <zash@zash.se>
parents:
11763
diff
changeset
|
66 log("warn", "select is deprecated, the new default is epoll. For more info see https://prosody.im/doc/network_backend"); |
12974
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
67 server = require "prosody.net.server_select"; |
6480
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
68 |
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
69 local defaults = {}; |
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
70 for k,v in pairs(server.getsettings()) do |
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
71 defaults[k] = v; |
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
72 end |
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
73 function set_config(settings) |
7543 | 74 local select_settings = {}; |
75 for k,default in pairs(defaults) do | |
76 select_settings[k] = settings[k] or default; | |
5198
430797a8fc81
net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents:
4811
diff
changeset
|
77 end |
7543 | 78 server.changesettings(select_settings); |
79 end | |
2094
c69cb5c171e0
net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 else |
12974
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
81 server = require("prosody.net.server_"..server_type); |
7545
0941b3ab77c0
net.server: Allow arbitrary network backends to be configured
Kim Alvefur <zash@zash.se>
parents:
7544
diff
changeset
|
82 set_config = server.set_config; |
7546
9606a99f8617
net.server: Monkey-patch in a fallback get_backend method if missing
Kim Alvefur <zash@zash.se>
parents:
7545
diff
changeset
|
83 if not server.get_backend then |
9606a99f8617
net.server: Monkey-patch in a fallback get_backend method if missing
Kim Alvefur <zash@zash.se>
parents:
7545
diff
changeset
|
84 function server.get_backend() |
9606a99f8617
net.server: Monkey-patch in a fallback get_backend method if missing
Kim Alvefur <zash@zash.se>
parents:
7545
diff
changeset
|
85 return server_type; |
9606a99f8617
net.server: Monkey-patch in a fallback get_backend method if missing
Kim Alvefur <zash@zash.se>
parents:
7545
diff
changeset
|
86 end |
9606a99f8617
net.server: Monkey-patch in a fallback get_backend method if missing
Kim Alvefur <zash@zash.se>
parents:
7545
diff
changeset
|
87 end |
2094
c69cb5c171e0
net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 end |
c69cb5c171e0
net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 |
6482
410067cdeb2f
net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents:
6480
diff
changeset
|
90 -- If server.hook_signal exists, replace signal.signal() |
12974
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
91 local has_signal, signal = pcall(require, "prosody.util.signal"); |
6485
4224abbf0fdd
net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents:
6484
diff
changeset
|
92 if has_signal then |
4224abbf0fdd
net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents:
6484
diff
changeset
|
93 if server.hook_signal then |
6482
410067cdeb2f
net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents:
6480
diff
changeset
|
94 function signal.signal(signal_id, handler) |
410067cdeb2f
net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents:
6480
diff
changeset
|
95 if type(signal_id) == "string" then |
410067cdeb2f
net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents:
6480
diff
changeset
|
96 signal_id = signal[signal_id:upper()]; |
410067cdeb2f
net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents:
6480
diff
changeset
|
97 end |
410067cdeb2f
net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents:
6480
diff
changeset
|
98 if type(signal_id) ~= "number" then |
410067cdeb2f
net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents:
6480
diff
changeset
|
99 return false, "invalid-signal"; |
410067cdeb2f
net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents:
6480
diff
changeset
|
100 end |
410067cdeb2f
net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents:
6480
diff
changeset
|
101 return server.hook_signal(signal_id, handler); |
410067cdeb2f
net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents:
6480
diff
changeset
|
102 end |
6485
4224abbf0fdd
net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents:
6484
diff
changeset
|
103 else |
4224abbf0fdd
net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents:
6484
diff
changeset
|
104 server.hook_signal = signal.signal; |
6482
410067cdeb2f
net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents:
6480
diff
changeset
|
105 end |
410067cdeb2f
net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents:
6480
diff
changeset
|
106 else |
6485
4224abbf0fdd
net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents:
6484
diff
changeset
|
107 if not server.hook_signal then |
4224abbf0fdd
net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents:
6484
diff
changeset
|
108 server.hook_signal = function() |
4224abbf0fdd
net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents:
6484
diff
changeset
|
109 return false, "signal hooking not supported" |
4224abbf0fdd
net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents:
6484
diff
changeset
|
110 end |
4224abbf0fdd
net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents:
6484
diff
changeset
|
111 end |
6482
410067cdeb2f
net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents:
6480
diff
changeset
|
112 end |
410067cdeb2f
net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents:
6480
diff
changeset
|
113 |
7544
fb68aad9dc4d
net.server: Only update configuration if server backend supports changing settings
Kim Alvefur <zash@zash.se>
parents:
7543
diff
changeset
|
114 if prosody and set_config then |
12974
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
115 local config_get = require "prosody.core.configmanager".get; |
5198
430797a8fc81
net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents:
4811
diff
changeset
|
116 local function load_config() |
5383
143670deee3e
net.server: No sections in config anymore
Kim Alvefur <zash@zash.se>
parents:
5284
diff
changeset
|
117 local settings = config_get("*", "network_settings") or {}; |
6480
37b12475f648
net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents:
5776
diff
changeset
|
118 return set_config(settings); |
5198
430797a8fc81
net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents:
4811
diff
changeset
|
119 end |
430797a8fc81
net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents:
4811
diff
changeset
|
120 load_config(); |
430797a8fc81
net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents:
4811
diff
changeset
|
121 prosody.events.add_handler("config-reloaded", load_config); |
430797a8fc81
net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents:
4811
diff
changeset
|
122 end |
430797a8fc81
net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents:
4811
diff
changeset
|
123 |
13166
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
124 if prosody and server.tls_builder then |
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
125 local tls_builder = server.tls_builder; |
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
126 -- resolving the basedir here avoids util.sslconfig depending on |
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
127 -- prosody.paths.config |
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
128 function server.tls_builder() |
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
129 return tls_builder(prosody.paths.config or "") |
e6e76f64ebb6
net.server: Handle loading from outside Prosody (e.g. Verse)
Kim Alvefur <zash@zash.se>
parents:
12974
diff
changeset
|
130 end |
12481
2ee27587fec7
net: refactor sslconfig to not depend on LuaSec
Jonas Schäfer <jonas@wielicki.name>
parents:
11764
diff
changeset
|
131 end |
2ee27587fec7
net: refactor sslconfig to not depend on LuaSec
Jonas Schäfer <jonas@wielicki.name>
parents:
11764
diff
changeset
|
132 |
12974
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
133 -- require "prosody.net.server" shall now forever return this, |
2136
23c687039652
net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents:
2105
diff
changeset
|
134 -- ie. server_select or server_event as chosen above. |
2645
625d02b2a1a0
net.server: Remove redundant commented code and some trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2435
diff
changeset
|
135 return server; |