refactor
This commit is contained in:
@ -42,32 +42,40 @@ E_SIMPLE2(max_cache_size_reached, warn, true,
|
|||||||
|
|
||||||
cache_size_mgr cache_size_mgr::instance_{};
|
cache_size_mgr cache_size_mgr::instance_{};
|
||||||
|
|
||||||
// TODO add timeout
|
|
||||||
auto cache_size_mgr::expand(std::uint64_t size) -> api_error {
|
auto cache_size_mgr::expand(std::uint64_t size) -> api_error {
|
||||||
if (size == 0U) {
|
|
||||||
return api_error::success;
|
|
||||||
}
|
|
||||||
|
|
||||||
unique_mutex_lock lock(mtx_);
|
unique_mutex_lock lock(mtx_);
|
||||||
|
|
||||||
if (cfg_ == nullptr) {
|
if (cfg_ == nullptr) {
|
||||||
|
notify_.notify_all();
|
||||||
return api_error::cache_not_initialized;
|
return api_error::cache_not_initialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (size == 0U) {
|
||||||
|
notify_.notify_all();
|
||||||
|
return api_error::success;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto last_cache_size{cache_size_};
|
||||||
cache_size_ += size;
|
cache_size_ += size;
|
||||||
|
|
||||||
auto max_cache_size = cfg_->get_max_cache_size_bytes();
|
auto max_cache_size{cfg_->get_max_cache_size_bytes()};
|
||||||
|
|
||||||
auto cache_dir = utils::file::directory{cfg_->get_cache_directory()};
|
auto cache_dir{
|
||||||
|
utils::file::directory{cfg_->get_cache_directory()},
|
||||||
|
};
|
||||||
while (not get_stop_requested() && cache_size_ > max_cache_size &&
|
while (not get_stop_requested() && cache_size_ > max_cache_size &&
|
||||||
cache_dir.count() > 1U) {
|
cache_dir.count() > 1U) {
|
||||||
event_system::instance().raise<max_cache_size_reached>(cache_size_,
|
if (last_cache_size != cache_size_) {
|
||||||
max_cache_size);
|
event_system::instance().raise<max_cache_size_reached>(cache_size_,
|
||||||
notify_.wait(lock);
|
max_cache_size);
|
||||||
|
last_cache_size = cache_size_;
|
||||||
|
}
|
||||||
|
notify_.wait(lock, 5s);
|
||||||
}
|
}
|
||||||
|
|
||||||
notify_.notify_all();
|
notify_.notify_all();
|
||||||
|
|
||||||
return api_error::success;
|
return get_stop_requested() ? api_error::error : api_error::success;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto cache_size_mgr::get_stop_requested() const -> bool {
|
auto cache_size_mgr::get_stop_requested() const -> bool {
|
||||||
@ -84,7 +92,9 @@ void cache_size_mgr::initialize(app_config *cfg) {
|
|||||||
|
|
||||||
stop_requested_ = false;
|
stop_requested_ = false;
|
||||||
|
|
||||||
auto cache_dir = utils::file::directory{cfg_->get_cache_directory()};
|
auto cache_dir{
|
||||||
|
utils::file::directory{cfg_->get_cache_directory()},
|
||||||
|
};
|
||||||
if (not cache_dir.create_directory()) {
|
if (not cache_dir.create_directory()) {
|
||||||
throw startup_exception(fmt::format("failed to create cache directory|{}",
|
throw startup_exception(fmt::format("failed to create cache directory|{}",
|
||||||
cache_dir.get_path()));
|
cache_dir.get_path()));
|
||||||
|
Reference in New Issue
Block a user