浏览代码

fix: Optimizes hot paths in prosody modules, string comparisons.

j8
damencho 5 年前
父节点
当前提交
895c92217a

+ 8
- 3
resources/prosody-plugins/mod_muc_allowners.lua 查看文件

1
 local jid = require "util.jid";
1
 local jid = require "util.jid";
2
 local um_is_admin = require "core.usermanager".is_admin;
2
 local um_is_admin = require "core.usermanager".is_admin;
3
-local is_healthcheck_room = module:require "util".is_healthcheck_room;
3
+local util = module:require "util";
4
+local is_healthcheck_room = util.is_healthcheck_room;
5
+local extract_subdomain = util.extract_subdomain;
4
 
6
 
5
 local moderated_subdomains;
7
 local moderated_subdomains;
6
 local moderated_rooms;
8
 local moderated_rooms;
22
 --      -> true, room_name, subdomain
24
 --      -> true, room_name, subdomain
23
 --      -> true, room_name, nil (if no subdomain is used for the room)
25
 --      -> true, room_name, nil (if no subdomain is used for the room)
24
 local function is_moderated(room_jid)
26
 local function is_moderated(room_jid)
27
+    if #moderated_subdomains == 0 and #moderated_rooms == 0 then
28
+        return false;
29
+    end
30
+
25
     local room_node = jid.node(room_jid);
31
     local room_node = jid.node(room_jid);
26
     -- parses bare room address, for multidomain expected format is:
32
     -- parses bare room address, for multidomain expected format is:
27
     -- [subdomain]roomName@conference.domain
33
     -- [subdomain]roomName@conference.domain
28
-    local target_subdomain, target_room_name = room_node:match("^%[([^%]]+)%](.+)$");
29
-
34
+    local target_subdomain, target_room_name = extract_subdomain(room_node);
30
     if target_subdomain then
35
     if target_subdomain then
31
         if moderated_subdomains:contains(target_subdomain) then
36
         if moderated_subdomains:contains(target_subdomain) then
32
             return true, target_room_name, target_subdomain;
37
             return true, target_room_name, target_subdomain;

+ 2
- 1
resources/prosody-plugins/mod_muc_call.lua 查看文件

1
 local ext_events = module:require "ext_events"
1
 local ext_events = module:require "ext_events"
2
 local jid = require "util.jid"
2
 local jid = require "util.jid"
3
+local extract_subdomain = module:require "util".extract_subdomain;
3
 
4
 
4
 -- Options and configuration
5
 -- Options and configuration
5
 local poltergeist_component = module:get_option_string(
6
 local poltergeist_component = module:get_option_string(
33
     local node, _, _ = jid.split(room_jid)
34
     local node, _, _ = jid.split(room_jid)
34
     if not node then return nil end
35
     if not node then return nil end
35
 
36
 
36
-    local target_subdomain, target_node = node:match("^%[([^%]]+)%](.+)$")
37
+    local target_subdomain, target_node = extract_subdomain(node);
37
 
38
 
38
     if not(target_node or target_subdomain) then
39
     if not(target_node or target_subdomain) then
39
         return "https://"..muc_domain_base.."/"..node
40
         return "https://"..muc_domain_base.."/"..node

+ 4
- 2
resources/prosody-plugins/token/util.lib.lua 查看文件

9
 local json_safe = require "cjson.safe";
9
 local json_safe = require "cjson.safe";
10
 local path = require "util.paths";
10
 local path = require "util.paths";
11
 local sha256 = require "util.hashes".sha256;
11
 local sha256 = require "util.hashes".sha256;
12
-local http_get_with_retry = module:require "util".http_get_with_retry;
12
+local main_util = module:require "util";
13
+local http_get_with_retry = main_util.http_get_with_retry;
14
+local extract_subdomain = main_util.extract_subdomain;
13
 
15
 
14
 local nr_retries = 3;
16
 local nr_retries = 3;
15
 
17
 
350
     local room_node = jid.node(room_address);
352
     local room_node = jid.node(room_address);
351
     -- parses bare room address, for multidomain expected format is:
353
     -- parses bare room address, for multidomain expected format is:
352
     -- [subdomain]roomName@conference.domain
354
     -- [subdomain]roomName@conference.domain
353
-    local target_subdomain, target_room = room_node:match("^%[([^%]]+)%](.+)$");
355
+    local target_subdomain, target_room = extract_subdomain(room_node);
354
 
356
 
355
     -- if we have '*' as room name in token, this means all rooms are allowed
357
     -- if we have '*' as room name in token, this means all rooms are allowed
356
     -- so we will use the actual name of the room when constructing strings
358
     -- so we will use the actual name of the room when constructing strings

+ 21
- 1
resources/prosody-plugins/util.lib.lua 查看文件

33
 -- (e.g. from room1@conference.foo.example.com/res returns (room1, example.com, res, foo))
33
 -- (e.g. from room1@conference.foo.example.com/res returns (room1, example.com, res, foo))
34
 local function room_jid_split_subdomain(room_jid)
34
 local function room_jid_split_subdomain(room_jid)
35
     local node, host, resource = jid.split(room_jid);
35
     local node, host, resource = jid.split(room_jid);
36
+
37
+    -- optimization, skip matching if there is no subdomain or it is not the muc component address at all
38
+    if host == muc_domain or not starts_with(host, muc_domain_prefix) then
39
+        return node, host, resource;
40
+    end
41
+
36
     local target_subdomain = host and host:match(target_subdomain_pattern);
42
     local target_subdomain = host and host:match(target_subdomain_pattern);
37
     return node, host, resource, target_subdomain
43
     return node, host, resource, target_subdomain
38
 end
44
 end
80
 
86
 
81
         return room_jid;
87
         return room_jid;
82
     end
88
     end
83
-    local target_subdomain, target_node = node:match("^%[([^%]]+)%](.+)$");
89
+
90
+    local target_subdomain, target_node = extract_subdomain(node);
84
     if not (target_node and target_subdomain) then
91
     if not (target_node and target_subdomain) then
85
         -- module:log("debug", "Not rewriting... unexpected node format: %s", node);
92
         -- module:log("debug", "Not rewriting... unexpected node format: %s", node);
86
         return room_jid;
93
         return room_jid;
87
     end
94
     end
95
+
88
     -- Ok, rewrite room_jid address to pretty format
96
     -- Ok, rewrite room_jid address to pretty format
89
     local new_node, new_host, new_resource = target_node, muc_domain_prefix..".".. target_subdomain.."."..muc_domain_base, resource;
97
     local new_node, new_host, new_resource = target_node, muc_domain_prefix..".".. target_subdomain.."."..muc_domain_base, resource;
90
     room_jid = jid.join(new_node, new_host, new_resource);
98
     room_jid = jid.join(new_node, new_host, new_resource);
226
     end
234
     end
227
 end
235
 end
228
 
236
 
237
+--- Extracts the subdomain and room name from internal jid node [foo]room1
238
+-- @return subdomain(optional, if extracted or nil), the room name
239
+function extract_subdomain(room_node)
240
+    -- optimization, skip matching if there is no subdomain, no [subdomain] part in the beginning of the node
241
+    if not starts_with(room_node, '[') then
242
+        return room_node;
243
+    end
244
+
245
+    return room_node:match("^%[([^%]]+)%](.+)$");
246
+end
247
+
229
 function starts_with(str, start)
248
 function starts_with(str, start)
230
     return str:sub(1, #start) == start
249
     return str:sub(1, #start) == start
231
 end
250
 end
306
 end
325
 end
307
 
326
 
308
 return {
327
 return {
328
+    extract_subdomain = extract_subdomain;
309
     is_feature_allowed = is_feature_allowed;
329
     is_feature_allowed = is_feature_allowed;
310
     is_healthcheck_room = is_healthcheck_room;
330
     is_healthcheck_room = is_healthcheck_room;
311
     get_room_from_jid = get_room_from_jid;
331
     get_room_from_jid = get_room_from_jid;

正在加载...
取消
保存