Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 0491af9

Browse files
committed
fix: async for cortexso request
1 parent fe3f051 commit 0491af9

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

engine/services/model_source_service.cc

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "model_source_service.h"
22
#include <chrono>
3+
#include <future>
34
#include <unordered_set>
45
#include "database/models.h"
56
#include "json/json.h"
@@ -381,6 +382,7 @@ cpp::result<bool, std::string> ModelSourceService::AddCortexsoOrg(
381382
cpp::result<bool, std::string> ModelSourceService::AddCortexsoRepo(
382383
const std::string& model_source, const std::string& author,
383384
const std::string& model_name) {
385+
auto begin = std::chrono::system_clock::now();
384386
auto branches =
385387
huggingface_utils::GetModelRepositoryBranches("cortexso", model_name);
386388
if (branches.has_error()) {
@@ -397,19 +399,25 @@ cpp::result<bool, std::string> ModelSourceService::AddCortexsoRepo(
397399
if (!readme.has_error()) {
398400
desc = readme.value();
399401
}
400-
// Get models from db
401402

403+
// Get models from db
402404
auto model_list_before = db_service_->GetModels(model_source)
403405
.value_or(std::vector<cortex::db::ModelEntry>{});
404406
std::unordered_set<std::string> updated_model_list;
405-
407+
std::vector<std::future<std::string>> tasks;
406408
for (auto const& [branch, _] : branches.value()) {
407-
CTL_INF(branch);
408-
auto add_res = AddCortexsoRepoBranch(model_source, author, model_name,
409-
branch, repo_info->metadata, desc)
410-
.value_or(std::unordered_set<std::string>{});
411-
for (auto const& a : add_res) {
412-
updated_model_list.insert(a);
409+
CTL_DBG(branch);
410+
tasks.push_back(std::async(std::launch::async, [&, branch] {
411+
return AddCortexsoRepoBranch(model_source, author, model_name, branch,
412+
repo_info->metadata, desc)
413+
.value_or(std::string{});
414+
}));
415+
}
416+
417+
for (auto& task : tasks) {
418+
auto add_res = task.get();
419+
if (!add_res.empty()) {
420+
updated_model_list.insert(add_res);
413421
}
414422
}
415423

@@ -422,18 +430,19 @@ cpp::result<bool, std::string> ModelSourceService::AddCortexsoRepo(
422430
}
423431
}
424432
}
433+
434+
auto end = std::chrono::system_clock::now();
435+
CTL_INF(
436+
"Duration ms: " << std::chrono::duration_cast<std::chrono::milliseconds>(
437+
end - begin)
438+
.count());
425439
return true;
426440
}
427441

428-
cpp::result<std::unordered_set<std::string>, std::string>
429-
ModelSourceService::AddCortexsoRepoBranch(const std::string& model_source,
430-
const std::string& author,
431-
const std::string& model_name,
432-
const std::string& branch,
433-
const std::string& metadata,
434-
const std::string& desc) {
435-
std::unordered_set<std::string> res;
436-
442+
cpp::result<std::string, std::string> ModelSourceService::AddCortexsoRepoBranch(
443+
const std::string& model_source, const std::string& author,
444+
const std::string& model_name, const std::string& branch,
445+
const std::string& metadata, const std::string& desc) {
437446
url_parser::Url url = {
438447
.protocol = "https",
439448
.host = kHuggingFaceHost,
@@ -488,9 +497,9 @@ ModelSourceService::AddCortexsoRepoBranch(const std::string& model_source,
488497
}
489498
}
490499
}
491-
res.insert(model_id);
500+
return model_id;
492501
}
493-
return res;
502+
return {};
494503
}
495504

496505
void ModelSourceService::SyncModelSource() {

engine/services/model_source_service.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class ModelSourceService {
7575
const std::string& model_source, const std::string& author,
7676
const std::string& model_name);
7777

78-
cpp::result<std::unordered_set<std::string>, std::string>
78+
cpp::result<std::string, std::string>
7979
AddCortexsoRepoBranch(const std::string& model_source,
8080
const std::string& author,
8181
const std::string& model_name,

0 commit comments

Comments
 (0)