Skip to content

Commit 6a97b36

Browse files
committed
Load motd asynchronously
1 parent 0e0eae1 commit 6a97b36

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/client/component/dvars.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,4 +463,4 @@ namespace dvars
463463
};
464464
}
465465

466-
REGISTER_COMPONENT(dvars::component)
466+
REGISTER_COMPONENT(dvars::component)

src/client/component/motd.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ namespace motd
3636
cached_file_header header{};
3737
std::string data;
3838
};
39+
40+
std::atomic_bool killed;
3941

4042
std::filesystem::path get_cache_folder()
4143
{
@@ -147,6 +149,11 @@ namespace motd
147149

148150
std::optional<std::string> download_image(const std::string& url)
149151
{
152+
if (killed)
153+
{
154+
return {};
155+
}
156+
150157
const auto cached = read_cached_file(url);
151158
if (cached.has_value())
152159
{
@@ -368,12 +375,25 @@ namespace motd
368375
});
369376
}
370377

378+
std::thread init_thread;
379+
371380
class component final : public component_interface
372381
{
373382
public:
383+
void post_start() override
384+
{
385+
init_thread = std::thread([]
386+
{
387+
init();
388+
});
389+
}
390+
374391
void post_unpack() override
375392
{
376-
init();
393+
if (init_thread.joinable())
394+
{
395+
init_thread.join();
396+
}
377397

378398
command::add("reloadmotd", []()
379399
{
@@ -385,6 +405,15 @@ namespace motd
385405
init(false);
386406
});
387407
}
408+
409+
void pre_destroy() override
410+
{
411+
killed = true;
412+
if (init_thread.joinable())
413+
{
414+
init_thread.join();
415+
}
416+
}
388417
};
389418
}
390419

0 commit comments

Comments
 (0)