Переглянути джерело

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

j8
damencho 5 роки тому
джерело
коміт
895c92217a

+ 8
- 3
resources/prosody-plugins/mod_muc_allowners.lua Переглянути файл

@@ -1,6 +1,8 @@
1 1
 local jid = require "util.jid";
2 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 7
 local moderated_subdomains;
6 8
 local moderated_rooms;
@@ -22,11 +24,14 @@ end
22 24
 --      -> true, room_name, subdomain
23 25
 --      -> true, room_name, nil (if no subdomain is used for the room)
24 26
 local function is_moderated(room_jid)
27
+    if #moderated_subdomains == 0 and #moderated_rooms == 0 then
28
+        return false;
29
+    end
30
+
25 31
     local room_node = jid.node(room_jid);
26 32
     -- parses bare room address, for multidomain expected format is:
27 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 35
     if target_subdomain then
31 36
         if moderated_subdomains:contains(target_subdomain) then
32 37
             return true, target_room_name, target_subdomain;

+ 2
- 1
resources/prosody-plugins/mod_muc_call.lua Переглянути файл

@@ -1,5 +1,6 @@
1 1
 local ext_events = module:require "ext_events"
2 2
 local jid = require "util.jid"
3
+local extract_subdomain = module:require "util".extract_subdomain;
3 4
 
4 5
 -- Options and configuration
5 6
 local poltergeist_component = module:get_option_string(
@@ -33,7 +34,7 @@ local function url_from_room_jid(room_jid)
33 34
     local node, _, _ = jid.split(room_jid)
34 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 39
     if not(target_node or target_subdomain) then
39 40
         return "https://"..muc_domain_base.."/"..node

+ 4
- 2
resources/prosody-plugins/token/util.lib.lua Переглянути файл

@@ -9,7 +9,9 @@ local jid = require "util.jid";
9 9
 local json_safe = require "cjson.safe";
10 10
 local path = require "util.paths";
11 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 16
 local nr_retries = 3;
15 17
 
@@ -350,7 +352,7 @@ function Util:verify_room(session, room_address)
350 352
     local room_node = jid.node(room_address);
351 353
     -- parses bare room address, for multidomain expected format is:
352 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 357
     -- if we have '*' as room name in token, this means all rooms are allowed
356 358
     -- so we will use the actual name of the room when constructing strings

+ 21
- 1
resources/prosody-plugins/util.lib.lua Переглянути файл

@@ -33,6 +33,12 @@ local roomless_iqs = {};
33 33
 -- (e.g. from room1@conference.foo.example.com/res returns (room1, example.com, res, foo))
34 34
 local function room_jid_split_subdomain(room_jid)
35 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 42
     local target_subdomain = host and host:match(target_subdomain_pattern);
37 43
     return node, host, resource, target_subdomain
38 44
 end
@@ -80,11 +86,13 @@ local function internal_room_jid_match_rewrite(room_jid, stanza)
80 86
 
81 87
         return room_jid;
82 88
     end
83
-    local target_subdomain, target_node = node:match("^%[([^%]]+)%](.+)$");
89
+
90
+    local target_subdomain, target_node = extract_subdomain(node);
84 91
     if not (target_node and target_subdomain) then
85 92
         -- module:log("debug", "Not rewriting... unexpected node format: %s", node);
86 93
         return room_jid;
87 94
     end
95
+
88 96
     -- Ok, rewrite room_jid address to pretty format
89 97
     local new_node, new_host, new_resource = target_node, muc_domain_prefix..".".. target_subdomain.."."..muc_domain_base, resource;
90 98
     room_jid = jid.join(new_node, new_host, new_resource);
@@ -226,6 +234,17 @@ function is_feature_allowed(session, feature)
226 234
     end
227 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 248
 function starts_with(str, start)
230 249
     return str:sub(1, #start) == start
231 250
 end
@@ -306,6 +325,7 @@ function http_get_with_retry(url, retry)
306 325
 end
307 326
 
308 327
 return {
328
+    extract_subdomain = extract_subdomain;
309 329
     is_feature_allowed = is_feature_allowed;
310 330
     is_healthcheck_room = is_healthcheck_room;
311 331
     get_room_from_jid = get_room_from_jid;

Завантаження…
Відмінити
Зберегти