Writes should block when maximum cache size is reached #25

This commit is contained in:
Scott E. Graves 2024-12-18 13:58:20 -06:00
parent 80fd52625e
commit f68917c8cc
2 changed files with 18 additions and 7 deletions

View File

@ -38,6 +38,10 @@ E_SIMPLE2(max_cache_size_reached, warn, true,
cache_size_mgr cache_size_mgr::instance_{};
auto cache_size_mgr::expand(std::uint64_t size) -> api_error {
if (size == 0U) {
return api_error::success;
}
unique_mutex_lock lock(mtx_);
if (cfg_ == nullptr) {
return api_error::error;
@ -81,6 +85,10 @@ void cache_size_mgr::initialize(app_config *cfg) {
}
auto cache_size_mgr::shrink(std::uint64_t size) -> api_error {
if (size == 0U) {
return api_error::success;
}
mutex_lock lock(mtx_);
if (cache_size_ >= size) {
cache_size_ -= size;

View File

@ -492,7 +492,7 @@ auto open_file::close() -> bool {
}
if (err != api_error::success || read_state_.all()) {
mgr_.remove_resume(get_api_path(), get_source_path());
mgr_.remove_resume(fsi_.api_path, get_source_path());
}
if (err == api_error::success) {
@ -500,14 +500,17 @@ auto open_file::close() -> bool {
}
auto file = utils::file::file{fsi_.source_path};
auto file_size = file.size().value_or(0U);
if (file.remove()) {
err = cache_size_mgr::instance().shrink(file.size().value_or(0U));
if (err != api_error::success) {
utils::error::raise_api_path_error(function_name, api_path, err,
} "failed to shrink cache");
auto res = cache_size_mgr::instance().shrink(file_size);
if (res != api_error::success) {
utils::error::raise_api_path_error(function_name, fsi_.api_path,
fsi_.source_path, res,
"failed to shrink cache");
}
} else {
utils::error::raise_api_path_error(
function_name, get_api_path(), fsi_.source_path,
function_name, fsi_.api_path, fsi_.source_path,
utils::get_last_error_code(), "failed to delete source file");
}
@ -517,7 +520,7 @@ auto open_file::close() -> bool {
auto res =
provider_.set_item_meta(fsi_.api_path, META_SOURCE, fsi_.source_path);
if (res != api_error::success) {
utils::error::raise_api_path_error(function_name, get_api_path(),
utils::error::raise_api_path_error(function_name, fsi_.api_path,
fsi_.source_path, res,
"failed to set new source path");
}