diff options
author | a-romanov <Anton.Romanov@ydb.tech> | 2023-05-15 20:12:16 +0300 |
---|---|---|
committer | a-romanov <Anton.Romanov@ydb.tech> | 2023-05-15 20:12:16 +0300 |
commit | 0228ff692d85e807b26befef2e14c91210ec449e (patch) | |
tree | 5a98ca9698789620be7af0b681b5509b21920629 | |
parent | 74a0e756f9e807d9090eedabbb510abd6567e414 (diff) | |
download | ydb-0228ff692d85e807b26befef2e14c91210ec449e.tar.gz |
YQL-15602 Fix race on load resources.
-rw-r--r-- | ydb/library/yql/public/udf/udf_helpers.cpp | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/ydb/library/yql/public/udf/udf_helpers.cpp b/ydb/library/yql/public/udf/udf_helpers.cpp index 260ff3e544b..91e2c1d779f 100644 --- a/ydb/library/yql/public/udf/udf_helpers.cpp +++ b/ydb/library/yql/public/udf/udf_helpers.cpp @@ -2,23 +2,30 @@ #include <library/cpp/resource/resource.h> -#include <util/generic/hash.h> #include <util/generic/singleton.h> +#include <mutex> +#include <unordered_map> namespace NYql { namespace NUdf { +namespace { + struct TLoadedResources { TString Get(TStringBuf resourceId) { - if (auto p = Resources.FindPtr(resourceId)) { - return *p; - } - return Resources.emplace(resourceId, NResource::Find(resourceId)).first->second; + const std::unique_lock lock(Sync); + const auto ins = Resources.emplace(resourceId, ""); + if (ins.second) + ins.first->second = NResource::Find(resourceId); + return ins.first->second; } - THashMap<TString, TString> Resources; + std::mutex Sync; + std::unordered_map<TString, TString> Resources; }; +} + TString LoadResourceOnce(TStringBuf resourceId) { return Singleton<TLoadedResources>()->Get(resourceId); } |