Software /
code /
prosody
Diff
plugins/mod_http_file_share.lua @ 11348:f076199b4d38
mod_http_file_share: Cache quotas to avoid hitting storage
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 31 Jan 2021 17:56:49 +0100 |
parent | 11347:5b3ad3c7fe47 |
child | 11349:a219001b449d |
line wrap: on
line diff
--- a/plugins/mod_http_file_share.lua Sun Jan 31 17:56:35 2021 +0100 +++ b/plugins/mod_http_file_share.lua Sun Jan 31 17:56:49 2021 +0100 @@ -60,6 +60,7 @@ }); 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 @@ 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 @@ 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;