Comparison

plugins/mod_http_file_share.lua @ 11999:9d2eab56f124

mod_http_file_share: Keep track of total storage use across restarts The value needs to be known in order to determine if additional uploads can be accepted.
author Kim Alvefur <zash@zash.se>
date Fri, 03 Dec 2021 08:21:29 +0100
parent 11998:99be6874340b
child 12003:121c0401f83e
comparison
equal deleted inserted replaced
11998:99be6874340b 11999:9d2eab56f124
26 26
27 module:add_identity("store", "file", module:get_option_string("name", "HTTP File Upload")); 27 module:add_identity("store", "file", module:get_option_string("name", "HTTP File Upload"));
28 module:add_feature(namespace); 28 module:add_feature(namespace);
29 29
30 local uploads = module:open_store("uploads", "archive"); 30 local uploads = module:open_store("uploads", "archive");
31 local persist_stats = module:open_store("upload_stats", "map");
31 -- id, <request>, time, owner 32 -- id, <request>, time, owner
32 33
33 local secret = module:get_option_string(module.name.."_secret", require"util.id".long()); 34 local secret = module:get_option_string(module.name.."_secret", require"util.id".long());
34 local external_base_url = module:get_option_string(module.name .. "_base_url"); 35 local external_base_url = module:get_option_string(module.name .. "_base_url");
35 local file_size_limit = module:get_option_number(module.name .. "_size_limit", 10 * 1024 * 1024); -- 10 MB 36 local file_size_limit = module:get_option_number(module.name .. "_size_limit", 10 * 1024 * 1024); -- 10 MB
70 71
71 local measure_upload_cache_size = module:measure("upload_cache", "amount"); 72 local measure_upload_cache_size = module:measure("upload_cache", "amount");
72 local measure_quota_cache_size = module:measure("quota_cache", "amount"); 73 local measure_quota_cache_size = module:measure("quota_cache", "amount");
73 local measure_total_storage_usage = nil; 74 local measure_total_storage_usage = nil;
74 if total_storage_limit then 75 if total_storage_limit then
76 local total, err = persist_stats:get(nil, "total");
77 if not err then total_storage_usage = tonumber(total) or 0; end
75 measure_total_storage_usage = module:measure("total_storage", "amount", { unit = "bytes" }); 78 measure_total_storage_usage = module:measure("total_storage", "amount", { unit = "bytes" });
76 end 79 end
77 80
78 module:hook_global("stats-update", function () 81 module:hook_global("stats-update", function ()
79 measure_upload_cache_size(upload_cache:count()); 82 measure_upload_cache_size(upload_cache:count());
511 end 514 end
512 515
513 if total_storage_usage then 516 if total_storage_usage then
514 total_storage_usage = total_storage_usage - size_sum; 517 total_storage_usage = total_storage_usage - size_sum;
515 module:log("debug", "Global quota %s / %s", B(total_storage_usage), B(total_storage_limit)); 518 module:log("debug", "Global quota %s / %s", B(total_storage_usage), B(total_storage_limit));
519 persist_stats:set(nil, "total", total_storage_usage);
516 end 520 end
517 521
518 if #obsolete_uploads == 0 then 522 if #obsolete_uploads == 0 then
519 module:log("debug", "No metadata to remove"); 523 module:log("debug", "No metadata to remove");
520 else 524 else
545 end 549 end
546 550
547 module:log("info", "Uploaded files total: %s in %d files", B(sum), count); 551 module:log("info", "Uploaded files total: %s in %d files", B(sum), count);
548 total_storage_usage = sum; 552 total_storage_usage = sum;
549 module:log("debug", "Global quota %s / %s", B(total_storage_usage), B(total_storage_limit)); 553 module:log("debug", "Global quota %s / %s", B(total_storage_usage), B(total_storage_limit));
554 persist_stats:set(nil, "total", sum);
550 summary_done(); 555 summary_done();
551 end); 556 end);
552 557
553 end 558 end
554 559