libfetchers: asyncify Cache::lookup{,Expired}

Change-Id: I0c2b364ebf7051b3118366b4e8c2b52860d5ce09
This commit is contained in:
eldritch horrors
2025-02-08 17:26:19 +00:00
parent 8bfd2d4bb1
commit 70026b1696
5 changed files with 28 additions and 21 deletions
+16 -10
View File
@@ -1,10 +1,12 @@
#include "lix/libfetchers/cache.hh"
#include "lix/libstore/sqlite.hh"
#include "lix/libutil/async.hh"
#include "lix/libutil/sync.hh"
#include "lix/libstore/store-api.hh"
#include "lix/libutil/users.hh"
#include <nlohmann/json.hpp>
#include <optional>
namespace nix::fetchers {
@@ -72,23 +74,25 @@ struct CacheImpl : Cache
(time(0)).exec();
}
std::optional<std::pair<Attrs, StorePath>> lookup(
kj::Promise<Result<std::optional<std::pair<Attrs, StorePath>>>> lookup(
ref<Store> store,
const Attrs & inAttrs) override
{
if (auto res = lookupExpired(store, inAttrs)) {
try {
if (auto res = TRY_AWAIT(lookupExpired(store, inAttrs))) {
if (!res->expired)
return std::make_pair(std::move(res->infoAttrs), std::move(res->storePath));
co_return std::make_pair(std::move(res->infoAttrs), std::move(res->storePath));
debug("ignoring expired cache entry '%s'",
attrsToJSON(inAttrs).dump());
}
return {};
co_return std::nullopt;
} catch (...) {
co_return result::current_exception();
}
std::optional<Result> lookupExpired(
kj::Promise<Result<std::optional<LookupResult>>> lookupExpired(
ref<Store> store,
const Attrs & inAttrs) override
{
try {
auto state(_state.lock());
auto inAttrsJSON = attrsToJSON(inAttrs).dump();
@@ -96,7 +100,7 @@ struct CacheImpl : Cache
auto stmt(state->lookup.use()(inAttrsJSON));
if (!stmt.next()) {
debug("did not find cache entry for '%s'", inAttrsJSON);
return {};
co_return std::nullopt;
}
auto infoJSON = stmt.getStr(0);
@@ -108,17 +112,19 @@ struct CacheImpl : Cache
if (!store->isValidPath(storePath)) {
// FIXME: we could try to substitute 'storePath'.
debug("ignoring disappeared cache entry '%s'", inAttrsJSON);
return {};
co_return std::nullopt;
}
debug("using cache entry '%s' -> '%s', '%s'",
inAttrsJSON, infoJSON, store->printStorePath(storePath));
return Result {
co_return LookupResult {
.expired = !locked && (settings.tarballTtl.get() == 0 || timestamp + settings.tarballTtl < time(0)),
.infoAttrs = jsonToAttrs(nlohmann::json::parse(infoJSON)),
.storePath = std::move(storePath)
};
} catch (...) {
co_return result::current_exception();
}
};
+3 -3
View File
@@ -17,18 +17,18 @@ struct Cache
const StorePath & storePath,
bool locked) = 0;
virtual std::optional<std::pair<Attrs, StorePath>> lookup(
virtual kj::Promise<Result<std::optional<std::pair<Attrs, StorePath>>>> lookup(
ref<Store> store,
const Attrs & inAttrs) = 0;
struct Result
struct LookupResult
{
bool expired = false;
Attrs infoAttrs;
StorePath storePath;
};
virtual std::optional<Result> lookupExpired(
virtual kj::Promise<Result<std::optional<LookupResult>>> lookupExpired(
ref<Store> store,
const Attrs & inAttrs) = 0;
};
+3 -3
View File
@@ -491,7 +491,7 @@ struct GitInputScheme : InputScheme
};
if (input.getRev()) {
if (auto res = getCache()->lookup(store, getLockedAttrs()))
if (auto res = TRY_AWAIT(getCache()->lookup(store, getLockedAttrs())))
co_return makeResult(res->first, std::move(res->second));
}
@@ -547,7 +547,7 @@ struct GitInputScheme : InputScheme
}
}
if (auto res = getCache()->lookup(store, unlockedAttrs)) {
if (auto res = TRY_AWAIT(getCache()->lookup(store, unlockedAttrs))) {
auto rev2 = Hash::parseAny(getStrAttr(res->first, "rev"), HashType::SHA1);
if (!input.getRev() || input.getRev() == rev2) {
input.attrs.insert_or_assign("rev", rev2.gitRev());
@@ -686,7 +686,7 @@ struct GitInputScheme : InputScheme
/* Now that we know the ref, check again whether we have it in
the store. */
if (auto res = getCache()->lookup(store, getLockedAttrs()))
if (auto res = TRY_AWAIT(getCache()->lookup(store, getLockedAttrs())))
co_return makeResult(res->first, std::move(res->second));
Path tmpDir = createTempDir();
+4 -3
View File
@@ -1,6 +1,7 @@
#include "lix/libfetchers/fetchers.hh"
#include "lix/libfetchers/cache.hh"
#include "lix/libfetchers/builtin-fetchers.hh"
#include "lix/libutil/async.hh"
#include "lix/libutil/processes.hh"
#include "lix/libstore/store-api.hh"
#include "lix/libstore/temporary-dir.hh"
@@ -243,7 +244,7 @@ struct MercurialInputScheme : InputScheme
};
if (input.getRev()) {
if (auto res = getCache()->lookup(store, getLockedAttrs()))
if (auto res = TRY_AWAIT(getCache()->lookup(store, getLockedAttrs())))
co_return makeResult(res->first, std::move(res->second));
}
@@ -256,7 +257,7 @@ struct MercurialInputScheme : InputScheme
{"ref", *input.getRef()},
});
if (auto res = getCache()->lookup(store, unlockedAttrs)) {
if (auto res = TRY_AWAIT(getCache()->lookup(store, unlockedAttrs))) {
auto rev2 = Hash::parseAny(getStrAttr(res->first, "rev"), HashType::SHA1);
if (!input.getRev() || input.getRev() == rev2) {
input.attrs.insert_or_assign("rev", rev2.gitRev());
@@ -302,7 +303,7 @@ struct MercurialInputScheme : InputScheme
auto revCount = std::stoull(tokens[1]);
input.attrs.insert_or_assign("ref", tokens[2]);
if (auto res = getCache()->lookup(store, getLockedAttrs()))
if (auto res = TRY_AWAIT(getCache()->lookup(store, getLockedAttrs())))
co_return makeResult(res->first, std::move(res->second));
Path tmpDir = createTempDir();
+2 -2
View File
@@ -28,7 +28,7 @@ try {
{"name", name},
});
auto cached = getCache()->lookupExpired(store, inAttrs);
auto cached = TRY_AWAIT(getCache()->lookupExpired(store, inAttrs));
auto useCached = [&]() -> DownloadFileResult
{
@@ -135,7 +135,7 @@ try {
{"name", name},
});
auto cached = getCache()->lookupExpired(store, inAttrs);
auto cached = TRY_AWAIT(getCache()->lookupExpired(store, inAttrs));
if (cached && !cached->expired)
co_return DownloadTarballResult{