diff options
author | Kim Alvefur <zash@zash.se> | 2021-01-31 17:56:49 +0100 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2021-01-31 17:56:49 +0100 |
commit | 864708efb707c0553c874c7e6f0ed4a07ef1b845 (patch) | |
tree | 98ee00028b07d97e8f187ae1dacf745e1a2471a6 /plugins/mod_http_file_share.lua | |
parent | 855542e2ca65ed12635ff0e4835365b971070033 (diff) | |
download | prosody-864708efb707c0553c874c7e6f0ed4a07ef1b845.tar.gz prosody-864708efb707c0553c874c7e6f0ed4a07ef1b845.zip |
mod_http_file_share: Cache quotas to avoid hitting storage
Diffstat (limited to 'plugins/mod_http_file_share.lua')
-rw-r--r-- | plugins/mod_http_file_share.lua | 14 |
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; |