Annotate

net/server.lua @ 5359:43517e649c53

util.pposix: Allow fetching RLIMIT_NICE when available
author Matthew Wild <mwild1@gmail.com>
date Fri, 22 Mar 2013 11:23:40 +0000
parent 5284:5d2282a29387
child 5383:143670deee3e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2925
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
1 -- Prosody IM
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
4 --
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
6 -- COPYING file in the source package for more information.
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
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
3461
447634564356 net.server: Don't load configmanager if not running inside Prosody, assume server_select
Matthew Wild <mwild1@gmail.com>
parents: 2968
diff changeset
9 local use_luaevent = prosody and require "core.configmanager".get("*", "core", "use_libevent");
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
10
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
11 if use_luaevent then
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
12 use_luaevent = pcall(require, "luaevent.core");
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
13 if not use_luaevent then
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
14 log("error", "libevent not found, falling back to select()");
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
15 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
16 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
17
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
18 local server;
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
19
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
20 if use_luaevent then
4811
1d1fdfa29f06 net.server: server_select != server_event (thanks Nulani, Florob)
Matthew Wild <mwild1@gmail.com>
parents: 4808
diff changeset
21 server = require "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
22
2435
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
23 -- Overwrite signal.signal() because we need to ask libevent to
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
24 -- handle them instead
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
25 local ok, signal = pcall(require, "util.signal");
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
26 if ok and signal then
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
27 local _signal_signal = signal.signal;
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
28 function signal.signal(signal_id, handler)
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
29 if type(signal_id) == "string" then
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
30 signal_id = signal[signal_id:upper()];
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
31 end
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
32 if type(signal_id) ~= "number" then
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
33 return false, "invalid-signal";
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
34 end
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
35 return server.hook_signal(signal_id, handler);
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
36 end
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
37 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
38 else
5198
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
39 use_luaevent = false;
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
40 server = require "net.server_select";
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
41 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
42
5198
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
43 if prosody then
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
44 local config_get = require "core.configmanager".get;
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
45 local function load_config()
5200
cfc038f1f923 net.server: Fix when no network_settings set and using libevent
Kim Alvefur <zash@zash.se>
parents: 5198
diff changeset
46 local settings = config_get("*", "core", "network_settings") or {};
5198
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
47 if use_luaevent then
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
48 local event_settings = {
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
49 ACCEPT_DELAY = settings.event_accept_retry_interval;
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
50 CLEAR_DELAY = settings.event_clear_interval;
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
51 CONNECT_TIMEOUT = settings.connect_timeout;
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
52 DEBUG = settings.debug;
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
53 HANDSHAKE_TIMEOUT = settings.ssl_handshake_timeout;
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
54 MAX_CONNECTIONS = settings.max_connections;
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
55 MAX_HANDSHAKE_ATTEMPTS = settings.max_ssl_handshake_roundtrips;
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
56 MAX_READ_LENGTH = settings.max_receive_buffer_size;
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
57 MAX_SEND_LENGTH = settings.max_send_buffer_size;
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
58 READ_TIMEOUT = settings.read_timeout;
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
59 WRITE_TIMEOUT = settings.send_timeout;
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
60 };
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
61
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
62 for k, v in pairs(event_settings) do
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
63 server.cfg[k] = v;
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
64 end
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
65 else
5284
5d2282a29387 net.server: Don't return anything from config-reloaded hook, fixes logrotate heisenbug
Kim Alvefur <zash@zash.se>
parents: 5200
diff changeset
66 server.changesettings(settings);
5198
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
67 end
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
68 end
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
69 load_config();
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
70 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
71 end
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
72
2136
23c687039652 net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents: 2105
diff changeset
73 -- require "net.server" shall now forever return this,
23c687039652 net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents: 2105
diff changeset
74 -- 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
75 return server;