From: Vsevolod Stakhov Date: Sun, 16 Oct 2022 20:31:31 +0000 (+0100) Subject: [Rework] Store the current worker, so other libraries could use this information X-Git-Tag: 3.4~48 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ff6390071ddb37e7ddeb9e058ef0831636e75841;p=rspamd.git [Rework] Store the current worker, so other libraries could use this information --- diff --git a/src/libserver/hyperscan_tools.cxx b/src/libserver/hyperscan_tools.cxx index 3dedad52d..c861a4668 100644 --- a/src/libserver/hyperscan_tools.cxx +++ b/src/libserver/hyperscan_tools.cxx @@ -23,6 +23,7 @@ #include "libutil/cxx/locked_file.hxx" #include "hs.h" #include "logger.h" +#include "worker_util.h" #include /* for glob */ #include /* for unlink */ @@ -49,13 +50,12 @@ private: ankerl::svector cache_dirs; ankerl::svector cache_extensions; ankerl::unordered_dense::set known_cached_files; - bool need_cleanup = false; private: hs_known_files_cache() = default; virtual ~hs_known_files_cache() { // Cleanup cache dir - if (need_cleanup) { + if (rspamd_current_worker != nullptr && rspamd_worker_is_primary_controller(rspamd_current_worker)) { auto cleanup_dir = [&](std::string_view dir) -> void { for (const auto &ext : cache_extensions) { glob_t globbuf; @@ -101,12 +101,11 @@ public: hs_known_files_cache(const hs_known_files_cache &) = delete; hs_known_files_cache(hs_known_files_cache &&) = delete; - static auto get(bool need_cleanup) -> hs_known_files_cache& { + static auto get() -> hs_known_files_cache& { static hs_known_files_cache *singleton = nullptr; if (singleton == nullptr) { singleton = new hs_known_files_cache; - singleton->need_cleanup = need_cleanup; } return *singleton; @@ -169,9 +168,9 @@ hs_shared_from_serialized(raii_mmaped_file &&map) -> tl::expected{tl::in_place, target}; } -auto load_cached_hs_file(const char *fname, bool need_cleanup) -> tl::expected +auto load_cached_hs_file(const char *fname) -> tl::expected { - auto &hs_cache = hs_known_files_cache::get(need_cleanup); + auto &hs_cache = hs_known_files_cache::get(); return raii_mmaped_file::mmap_shared(fname, O_RDONLY, PROT_READ) .and_then([&](T &&cached_serialized) -> tl::expected { diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c index f9d089a9b..6fd2e6164 100644 --- a/src/libserver/worker_util.c +++ b/src/libserver/worker_util.c @@ -64,6 +64,8 @@ #include "contrib/libev/ev.h" #include "libstat/stat_api.h" +struct rspamd_worker *rspamd_current_worker = NULL; + /* Forward declaration */ static void rspamd_worker_heartbeat_start (struct rspamd_worker *, struct ev_loop *); @@ -1276,6 +1278,7 @@ rspamd_fork_worker (struct rspamd_main *rspamd_main, switch (wrk->pid) { case 0: + rspamd_current_worker = wrk; rspamd_handle_child_fork (wrk, rspamd_main, cf, listen_sockets); break; case -1: diff --git a/src/libserver/worker_util.h b/src/libserver/worker_util.h index 677f8fcbf..c215f9523 100644 --- a/src/libserver/worker_util.h +++ b/src/libserver/worker_util.h @@ -36,6 +36,8 @@ typedef void (*rspamd_sig_handler_t) (gint, siginfo_t *, void *); struct rspamd_worker; struct rspamd_worker_signal_handler; +extern struct rspamd_worker *rspamd_current_worker; + /** * Init basic signals for a worker * @param worker