Selaa lähdekoodia

Add an LRU cache to mod_auth_token

master
Sam Whited 8 vuotta sitten
vanhempi
commit
feb1d9d8e1
1 muutettua tiedostoa jossa 29 lisäystä ja 16 poistoa
  1. 29
    16
      prosody-plugins/mod_auth_token.lua

+ 29
- 16
prosody-plugins/mod_auth_token.lua Näytä tiedosto

1
 -- Token authentication
1
 -- Token authentication
2
 -- Copyright (C) 2015 Atlassian
2
 -- Copyright (C) 2015 Atlassian
3
 
3
 
4
-local basexx = require 'basexx'
4
+local basexx = require 'basexx';
5
 local have_async, async = pcall(require, "util.async");
5
 local have_async, async = pcall(require, "util.async");
6
 local formdecode = require "util.http".formdecode;
6
 local formdecode = require "util.http".formdecode;
7
 local generate_uuid = require "util.uuid".generate;
7
 local generate_uuid = require "util.uuid".generate;
25
 local allowEmptyToken = module:get_option_boolean("allow_empty_token");
25
 local allowEmptyToken = module:get_option_boolean("allow_empty_token");
26
 local disableRoomNameConstraints = module:get_option_boolean("disable_room_name_constraints");
26
 local disableRoomNameConstraints = module:get_option_boolean("disable_room_name_constraints");
27
 
27
 
28
+-- TODO: Figure out a less arbitrary default cache size.
29
+local cacheSize = module:get_option_number("jwt_pubkey_cache_size", 128);
30
+local cache = require"util.cache".new(cacheSize);
31
+
28
 if allowEmptyToken == true then
32
 if allowEmptyToken == true then
29
 	module:log("warn", "WARNING - empty tokens allowed");
33
 	module:log("warn", "WARNING - empty tokens allowed");
30
 end
34
 end
82
 	["User-Agent"] = "Prosody ("..prosody.version.."; "..prosody.platform..")"
86
 	["User-Agent"] = "Prosody ("..prosody.version.."; "..prosody.platform..")"
83
 };
87
 };
84
 
88
 
85
 function get_public_key(keyId)
89
 function get_public_key(keyId)
86
-	local wait, done = async.waiter();
87
-	local content, code; --, request, response;
88
-	local function cb(content_, code_, response_, request_)
89
-		content, code = content_, code_;
90
-		done();
91
-	end
92
-	local request = http.request(path.join(asapKeyServer, keyId), {
93
-		headers = http_headers or {},
94
-		method = "GET"
95
-	}, cb);
96
-	-- TODO: Is the done() call racey?
97
-	timer.add_task(http_timeout, function() http.destroy_request(request); done(); end);
98
-	wait();
99
-
100
-	if code == 200 or code == 204 then
90
+	local content = cache:get(keyId);
91
+	if content == nil then
92
+		-- If the key is not found in the cache.
93
+		module:log("debug", "Cache miss for key: "..keyId);
94
+		local code;
95
+		local wait, done = async.waiter();
96
+		local function cb(content_, code_, response_, request_)
97
+			content, code = content_, code_;
98
+			done();
99
+		end
100
+		local request = http.request(path.join(asapKeyServer, keyId), {
101
+			headers = http_headers or {},
102
+			method = "GET"
103
+		}, cb);
104
+		-- TODO: Is the done() call racey?
105
+		timer.add_task(http_timeout, function() http.destroy_request(request); done(); end);
106
+		wait();
107
+
108
+		if code == 200 or code == 204 then
109
+			module:log("debug", "Cache hit for key: "..keyId);
110
+			return content;
111
+		end
112
+	else
113
+		-- If the key is in the cache, use it.
101
 		return content;
114
 		return content;
102
 	end
115
 	end
103
 
116
 

Loading…
Peruuta
Tallenna