aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2021-01-31 17:56:49 +0100
committerKim Alvefur <zash@zash.se>2021-01-31 17:56:49 +0100
commit1af0932ecfbe5936e172dd4caf02257def14540e (patch)
tree98ee00028b07d97e8f187ae1dacf745e1a2471a6
parent12a72e28dcbd2dd27f9f90d957bd8528c4658c1d (diff)
downloadprosody-1af0932ecfbe5936e172dd4caf02257def14540e.tar.gz
prosody-1af0932ecfbe5936e172dd4caf02257def14540e.zip
mod_http_file_share: Cache quotas to avoid hitting storage
-rw-r--r--plugins/mod_http_file_share.lua14
1 files changed, 12 insertions, 2 deletions
diff --git a/plugins/mod_http_file_share.lua b/plugins/mod_http_file_share.lua
index 47a060a1..95e2f1c5 100644
--- a/plugins/mod_http_file_share.lua
+++ b/plugins/mod_http_file_share.lua
@@ -60,6 +60,7 @@ local upload_errors = errors.init(module.name, namespace, {
});
local upload_cache = cache.new(1024);
+local quota_cache = cache.new(1024);
-- Convenience wrapper for logging file sizes
local function B(bytes) return hi.format(bytes, "B", "b"); end
@@ -68,17 +69,23 @@ local function get_filename(slot, create)
return dm.getpath(slot, module.host, module.name, "bin", create)
end
--- TODO cache
function get_daily_quota(uploader)
local now = os.time();
local max_age = now - 86400;
+ local cached = quota_cache:get(uploader);
+ if cached and cached.time > max_age then
+ return cached.size;
+ end
local iter, err = uploads:find(nil, {with = uploader; start = max_age });
if not iter then return iter, err; end
local total_bytes = 0;
- for _, slot in iter do
+ local oldest_upload;
+ for _, slot, when in iter do
local size = tonumber(slot.attr.size);
if size then total_bytes = total_bytes + size; end
+ if not oldest_upload then oldest_upload = when; end
end
+ quota_cache:set(uploader, { time = oldest_upload or now, size = total_bytes });
return total_bytes;
end
@@ -160,6 +167,9 @@ function handle_slot_request(event)
return true;
end
+ -- Invalidate cache
+ quota_cache:set(uploader, nil);
+
local authz = get_authz(uploader, filename, filesize, filetype, slot);
local slot_url = get_url(slot, filename);
local upload_url = slot_url;