Commit 4fd4a7aa authored by Ivan Vilata-i-Balaguer's avatar Ivan Vilata-i-Balaguer
Browse files

Add cancellation support to HTTP store `for_each`.

Some operations on the whole store may take a while.
parent 598af387
......@@ -65,7 +65,7 @@ struct GarbageCollector {
auto k = keep(std::move(rr), y[e]);
if (cancel) ec = asio::error::operation_aborted;
return or_throw(y, e, k);
}, yield[ec]);
}, cancel, yield[ec]);
if (ec) LOG_WARN( "Bep5HTTP: Garbage collection round failed: "
, ec.message());
LOG_DEBUG("Bep5HTTP: End garbage collection round");
......@@ -189,8 +189,7 @@ struct Client::Impl {
sys::error_code ec;
http_store->for_each([&] (auto, auto) {
return false; // remove all entries
}, yield[ec]);
if (cancel) ec = asio::error::operation_aborted;
}, cancel, yield[ec]);
if (ec) {
LOG_ERROR("Bep5HTTP: Purging local cache: failed"
" ec:", ec.message());
......@@ -616,7 +615,7 @@ struct Client::Impl {
http_store->for_each([&] (auto rr, auto yield) {
return keep_cache_entry(std::move(rr), yield);
}, y[e]);
}, cancel, y[e]);
if (e) return or_throw(y, e);
for (auto dht_group : _dht_groups->groups()) {
......
......@@ -862,7 +862,8 @@ v0_recently_updated(const fs::path& path)
}
void
HttpStoreV0::for_each(keep_func keep, asio::yield_context yield)
HttpStoreV0::for_each( keep_func keep
, Cancel cancel, asio::yield_context yield)
{
for (auto& p : fs::directory_iterator(path)) {
if (!fs::is_regular_file(p)) {
......@@ -897,7 +898,8 @@ HttpStoreV0::for_each(keep_func keep, asio::yield_context yield)
assert(rr);
auto keep_entry = keep(std::move(rr), yield[ec]);
if (ec == asio::error::operation_aborted) return;
if (cancel) ec = asio::error::operation_aborted;
if (ec == asio::error::operation_aborted) return or_throw(yield, ec);
if (ec) {
_WARN("Failed to check cached response: ", p, " ec:", ec.message());
v0_try_remove(p); continue;
......@@ -998,7 +1000,8 @@ v1_recently_updated(const fs::path& path)
}
void
HttpStoreV1::for_each(keep_func keep, asio::yield_context yield)
HttpStoreV1::for_each( keep_func keep
, Cancel cancel, asio::yield_context yield)
{
for (auto& pp : fs::directory_iterator(path)) { // iterate over `DIGEST[:2]` dirs
if (!fs::is_directory(pp)) {
......@@ -1045,7 +1048,8 @@ HttpStoreV1::for_each(keep_func keep, asio::yield_context yield)
assert(rr);
auto keep_entry = keep(std::move(rr), yield[ec]);
if (ec == asio::error::operation_aborted) return;
if (cancel) ec = asio::error::operation_aborted;
if (ec == asio::error::operation_aborted) return or_throw(yield, ec);
if (ec) {
_WARN("Failed to check cached response: ", p, " ec:", ec.message());
v1_try_remove(p); continue;
......
......@@ -142,7 +142,7 @@ public:
// Other maintenance may be performed too.
virtual
void
for_each(keep_func, asio::yield_context) = 0;
for_each(keep_func, Cancel, asio::yield_context) = 0;
virtual
void
......@@ -172,7 +172,7 @@ public:
~HttpStoreV0() override;
void
for_each(keep_func, asio::yield_context) override;
for_each(keep_func, Cancel, asio::yield_context) override;
void
store( const std::string& key, http_response::AbstractReader&
......@@ -203,7 +203,7 @@ public:
~HttpStoreV1() override;
void
for_each(keep_func, asio::yield_context) override;
for_each(keep_func, Cancel, asio::yield_context) override;
void
store( const std::string& key, http_response::AbstractReader&
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment