diff --git a/loader/src/server/Server.cpp b/loader/src/server/Server.cpp index 930387b6b..6577bd944 100644 --- a/loader/src/server/Server.cpp +++ b/loader/src/server/Server.cpp @@ -789,30 +789,14 @@ ServerRequest> server::checkUpdates(Mod const* mo ); } -ServerRequest> server::checkAllUpdates(bool useCache) { - if (useCache) { - return getCache().get(); - } - - auto modIDs = ranges::map>( - Loader::get()->getAllMods(), - [](auto mod) { return mod->getID(); } - ); - - // if there's no mods, the request would just be empty anyways - if (modIDs.empty()) { - // you would think it could infer like literally anything - return ServerRequest>::immediate( - Ok>({}) - ); - } - +ServerRequest> server::batchedCheckUpdates(std::vector const& batch) { auto req = web::WebRequest(); req.userAgent(getServerUserAgent()); req.param("platform", GEODE_PLATFORM_SHORT_IDENTIFIER); req.param("gd", GEODE_GD_VERSION_STR); req.param("geode", Loader::get()->getVersion().toNonVString()); - req.param("ids", ranges::join(modIDs, ";")); + + req.param("ids", ranges::join(batch, ";")); return req.get(formatServerURL("/mods/updates")).map( [](web::WebResponse* response) -> Result, ServerError> { if (response->ok()) { @@ -836,6 +820,80 @@ ServerRequest> server::checkAllUpdates(bool useCach ); } +void server::queueBatches( + ServerRequest>::PostResult const resolve, + std::shared_ptr>> const batches, + std::shared_ptr> accum +) { + // we have to do the copy here, or else our values die + batchedCheckUpdates(batches->back()).listen([resolve, batches, accum](auto result) { + if (result->ok()) { + auto serverValues = result->unwrap(); + + // we should probably do deduplication here as well + accum->reserve(accum->size() + serverValues.size()); + std::copy(serverValues.begin(), serverValues.end(), std::back_inserter(*accum)); + + if (batches->size() > 1) { + batches->pop_back(); + queueBatches(resolve, batches, accum); + } + else { + resolve(Ok(*accum)); + } + } + else { + resolve(*result); + } + }); +} + +ServerRequest> server::checkAllUpdates(bool useCache) { + if (useCache) { + return getCache().get(); + } + + auto modIDs = ranges::map>( + Loader::get()->getAllMods(), + [](auto mod) { return mod->getID(); } + ); + + // if there's no mods, the request would just be empty anyways + if (modIDs.empty()) { + // you would think it could infer like literally anything + return ServerRequest>::immediate( + Ok>({}) + ); + } + + auto modBatches = std::make_shared>>(); + auto modCount = modIDs.size(); + std::size_t maxMods = 200u; // this quite literally affects 0.03% of users + + if (modCount <= maxMods) { + // no tricks needed + return batchedCheckUpdates(modIDs); + } + + // even out the mod count, so a request with 230 mods sends two 115 mod requests + auto batchCount = modCount / maxMods + 1; + auto maxBatchSize = modCount / batchCount + 1; + + for (std::size_t i = 0u; i < modCount; i += maxBatchSize) { + auto end = std::min(modCount, i + maxBatchSize); + modBatches->emplace_back(modIDs.begin() + i, modIDs.begin() + end); + } + + // chain requests to avoid doing too many large requests at once + return ServerRequest>::runWithCallback( + [modBatches](auto finish, auto progress, auto hasBeenCancelled) { + auto accum = std::make_shared>(); + queueBatches(finish, modBatches, std::move(accum)); + }, + "Mod Update Check" + ); +} + void server::clearServerCaches(bool clearGlobalCaches) { getCache<&getMods>().clear(); getCache<&getMod>().clear(); diff --git a/loader/src/server/Server.hpp b/loader/src/server/Server.hpp index 908d4eab4..967b928a6 100644 --- a/loader/src/server/Server.hpp +++ b/loader/src/server/Server.hpp @@ -7,6 +7,7 @@ #include #include #include +#include using namespace geode::prelude; @@ -151,7 +152,15 @@ namespace server { ServerRequest> getTags(bool useCache = true); ServerRequest> checkUpdates(Mod const* mod); + + ServerRequest> batchedCheckUpdates(std::vector const& batch); + void queueBatches( + ServerRequest>::PostResult const finish, + std::shared_ptr>> const batches, + std::shared_ptr> const accum + ); + ServerRequest> checkAllUpdates(bool useCache = true); - + void clearServerCaches(bool clearGlobalCaches = false); }