From c77bd88259dac6e6b7bcb29425905467aa2ef66f Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Sat, 27 Apr 2024 19:55:54 +0200 Subject: [PATCH] libstore: de-callback-ify BinaryCacheStore::getFile Change-Id: I36b3eb9f645aa04058151e7b2353e15e6f29057b --- src/libstore/binary-cache-store.cc | 69 +++++++------------------ src/libstore/binary-cache-store.hh | 10 +--- src/libstore/http-binary-cache-store.cc | 33 ++++-------- 3 files changed, 29 insertions(+), 83 deletions(-) diff --git a/src/libstore/binary-cache-store.cc b/src/libstore/binary-cache-store.cc index 502fac05f..bf7265c35 100644 --- a/src/libstore/binary-cache-store.cc +++ b/src/libstore/binary-cache-store.cc @@ -67,26 +67,9 @@ void BinaryCacheStore::upsertFile(const std::string & path, upsertFile(path, std::make_shared(std::move(data)), mimeType); } -void BinaryCacheStore::getFile(const std::string & path, - Callback> callback) noexcept -{ - try { - callback(getFile(path)); - } catch (...) { callback.rethrow(); } -} - void BinaryCacheStore::getFile(const std::string & path, Sink & sink) { - std::promise> promise; - getFile(path, - {[&](std::future> result) { - try { - promise.set_value(result.get()); - } catch (...) { - promise.set_exception(std::current_exception()); - } - }}); - sink(*promise.get_future().get()); + sink(*getFile(path)); } std::optional BinaryCacheStore::getFile(const std::string & path) @@ -377,25 +360,17 @@ void BinaryCacheStore::queryPathInfoUncached(const StorePath & storePath, auto narInfoFile = narInfoFileFor(storePath); - auto callbackPtr = std::make_shared(std::move(callback)); + try { + auto data = getFile(narInfoFile); - getFile(narInfoFile, - {[=,this](std::future> fut) { - try { - auto data = fut.get(); + if (!data) return callback({}); - if (!data) return (*callbackPtr)({}); + stats.narInfoRead++; - stats.narInfoRead++; - - (*callbackPtr)((std::shared_ptr) - std::make_shared(*this, *data, narInfoFile)); - - (void) act; // force Activity into this lambda to ensure it stays alive - } catch (...) { - callbackPtr->rethrow(); - } - }}); + callback(std::make_shared(*this, *data, narInfoFile)); + } catch (...) { + callback.rethrow(); + } } StorePath BinaryCacheStore::addToStore( @@ -477,24 +452,16 @@ void BinaryCacheStore::queryRealisationUncached(const DrvOutput & id, { auto outputInfoFilePath = realisationsPrefix + "/" + id.to_string() + ".doi"; - auto callbackPtr = std::make_shared(std::move(callback)); + try { + auto data = getFile(outputInfoFilePath); + if (!data) return callback({}); - Callback> newCallback = { - [=](std::future> fut) { - try { - auto data = fut.get(); - if (!data) return (*callbackPtr)({}); - - auto realisation = Realisation::fromJSON( - nlohmann::json::parse(*data), outputInfoFilePath); - return (*callbackPtr)(std::make_shared(realisation)); - } catch (...) { - callbackPtr->rethrow(); - } - } - }; - - getFile(outputInfoFilePath, std::move(newCallback)); + auto realisation = Realisation::fromJSON( + nlohmann::json::parse(*data), outputInfoFilePath); + return callback(std::make_shared(realisation)); + } catch (...) { + callback.rethrow(); + } } void BinaryCacheStore::registerDrvOutput(const Realisation& info) { diff --git a/src/libstore/binary-cache-store.hh b/src/libstore/binary-cache-store.hh index 218a888e3..af2b50084 100644 --- a/src/libstore/binary-cache-store.hh +++ b/src/libstore/binary-cache-store.hh @@ -85,15 +85,7 @@ public: */ virtual void getFile(const std::string & path, Sink & sink); - /** - * Fetch the specified file and call the specified callback with - * the result. A subclass may implement this asynchronously. - */ - virtual void getFile( - const std::string & path, - Callback> callback) noexcept; - - std::optional getFile(const std::string & path); + virtual std::optional getFile(const std::string & path); public: diff --git a/src/libstore/http-binary-cache-store.cc b/src/libstore/http-binary-cache-store.cc index 9b980d81f..764f97fc1 100644 --- a/src/libstore/http-binary-cache-store.cc +++ b/src/libstore/http-binary-cache-store.cc @@ -165,33 +165,20 @@ protected: } } - void getFile(const std::string & path, - Callback> callback) noexcept override + std::optional getFile(const std::string & path) override { - try { - checkEnabled(); - } catch (...) { - callback.rethrow(); - return; - } + checkEnabled(); auto request(makeRequest(path)); - auto callbackPtr = std::make_shared(std::move(callback)); - - getFileTransfer()->enqueueFileTransfer(request, - {[callbackPtr, this](std::future result) { - try { - (*callbackPtr)(std::move(result.get().data)); - } catch (FileTransferError & e) { - if (e.error == FileTransfer::NotFound || e.error == FileTransfer::Forbidden) - return (*callbackPtr)({}); - maybeDisable(); - callbackPtr->rethrow(); - } catch (...) { - callbackPtr->rethrow(); - } - }}); + try { + return std::move(getFileTransfer()->enqueueFileTransfer(request).get().data); + } catch (FileTransferError & e) { + if (e.error == FileTransfer::NotFound || e.error == FileTransfer::Forbidden) + return {}; + maybeDisable(); + throw; + } } /**