ring buffer selection fixes

This commit is contained in:
Scott E. Graves 2024-12-22 12:42:08 -06:00
parent 6886f7d392
commit b41699af5c
3 changed files with 32 additions and 10 deletions

View File

@ -78,6 +78,10 @@ protected:
} }
public: public:
[[nodiscard]] static auto can_handle_file(std::uint64_t file_size,
std::size_t chunk_size,
std::size_t ring_size) -> bool;
auto close() -> bool override; auto close() -> bool override;
void forward(std::size_t count); void forward(std::size_t count);

View File

@ -404,8 +404,18 @@ auto file_manager::open(
} }
if (not closeable_file) { if (not closeable_file) {
const auto get_download_type = [this, auto buffer_directory =
&fsi](download_type type) -> download_type { utils::path::combine(config_.get_data_directory(), {"buffer"});
auto chunk_size =
utils::encryption::encrypting_reader::get_data_chunk_size();
auto ring_size =
(static_cast<std::uint64_t>(config_.get_ring_buffer_file_size() *
1024UL * 1024UL) /
chunk_size);
const auto get_download_type =
[this, &buffer_directory, &chunk_size, &fsi,
&ring_size](download_type type) -> download_type {
if (fsi.size == 0U) { if (fsi.size == 0U) {
return download_type::fallback; return download_type::fallback;
} }
@ -422,8 +432,12 @@ auto file_manager::open(
} }
} }
auto free_space = utils::file::get_free_drive_space( if (not ring_buffer_open_file::can_handle_file(fsi.size, chunk_size,
utils::path::combine(config_.get_data_directory(), {"buffer"})); ring_size)) {
return download_type::direct;
}
auto free_space = utils::file::get_free_drive_space(buffer_directory);
if (config_.get_ring_buffer_file_size() < free_space) { if (config_.get_ring_buffer_file_size() < free_space) {
return download_type::ring_buffer; return download_type::ring_buffer;
} }
@ -431,8 +445,6 @@ auto file_manager::open(
return download_type::direct; return download_type::direct;
}; };
auto chunk_size =
utils::encryption::encrypting_reader::get_data_chunk_size();
auto chunk_timeout = config_.get_enable_download_timeout() auto chunk_timeout = config_.get_enable_download_timeout()
? config_.get_download_timeout_secs() ? config_.get_download_timeout_secs()
: 0U; : 0U;
@ -444,8 +456,8 @@ auto file_manager::open(
case repertory::download_type::ring_buffer: { case repertory::download_type::ring_buffer: {
closeable_file = std::make_shared<ring_buffer_open_file>( closeable_file = std::make_shared<ring_buffer_open_file>(
utils::path::combine(config_.get_data_directory(), {"buffer"}), buffer_directory, chunk_size, chunk_timeout, fsi, provider_,
chunk_size, chunk_timeout, fsi, provider_); ring_size);
} break; } break;
default: { default: {
@ -454,9 +466,9 @@ auto file_manager::open(
} break; } break;
} }
} }
open_file_lookup_[api_path] = closeable_file; open_file_lookup_[api_path] = closeable_file;
create_and_add_handle(closeable_file); create_and_add_handle(closeable_file);
return api_error::success; return api_error::success;
} }

View File

@ -61,7 +61,7 @@ ring_buffer_open_file::ring_buffer_open_file(std::string buffer_directory,
throw std::runtime_error("ring size must be greater than or equal to 4"); throw std::runtime_error("ring size must be greater than or equal to 4");
} }
if (fsi.size < (ring_state_.size() * chunk_size)) { if (not can_handle_file(fsi.size, chunk_size, ring_size)) {
throw std::runtime_error("file size is less than ring buffer size"); throw std::runtime_error("file size is less than ring buffer size");
} }
@ -103,6 +103,12 @@ ring_buffer_open_file::~ring_buffer_open_file() {
} }
} }
auto ring_buffer_open_file::can_handle_file(std::uint64_t file_size,
std::size_t chunk_size,
std::size_t ring_size) -> bool {
return file_size <= (ring_size * chunk_size);
}
auto ring_buffer_open_file::close() -> bool { auto ring_buffer_open_file::close() -> bool {
stop_requested_ = true; stop_requested_ = true;
return open_file_base::close(); return open_file_base::close();