userver: userver/storages/secdist/secdist.hpp Source File
Loading...
Searching...
No Matches
secdist.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <any>
7#include <chrono>
8#include <cstdint>
9#include <functional>
10#include <optional>
11#include <stdexcept>
12#include <string_view>
13#include <typeindex>
14#include <vector>
15
19#include <userver/rcu/rcu.hpp>
23
24USERVER_NAMESPACE_BEGIN
25
27namespace storages::secdist {
28
29class SecdistConfig;
30
31namespace detail {
32
33template <typename T>
34class SecdistModule final {
35public:
36 static const T& Get(const SecdistConfig& config);
37 static std::any Factory(const formats::json::Value& data) { return T(data); }
38
39private:
40 static std::size_t index;
41};
42
43} // namespace detail
44
46enum class SecdistFormat {
47 kJson,
48 kYaml,
49 kYamlConfig,
50};
51
75class SecdistConfig final {
76public:
77 struct Settings {
78 SecdistProvider* provider{nullptr};
79 std::chrono::milliseconds update_period{std::chrono::milliseconds::zero()};
80 };
81
82 SecdistConfig();
83 explicit SecdistConfig(const Settings& settings);
84
85 template <typename T>
86 static std::size_t Register(std::function<std::any(const formats::json::Value&)>&& factory) {
87 return Register(std::move(factory));
88 }
89
90 template <typename T>
91 const T& Get() const {
92 return detail::SecdistModule<T>::Get(*this);
93 }
94
95private:
96 void Init(const formats::json::Value& doc);
97
98 static std::size_t Register(std::function<std::any(const formats::json::Value&)>&& factory);
99 const std::any& Get(const std::type_index& type, std::size_t index) const;
100
101 template <typename T>
102 friend class detail::SecdistModule;
103
104 std::vector<std::any> configs_;
105};
106
111class Secdist final {
112public:
113 explicit Secdist(SecdistConfig::Settings settings);
114 ~Secdist();
115
119
123
136 template <typename Class>
137 void UpdateAndListen(
139 Class* obj,
140 std::string_view name,
141 void (Class::*func)(const storages::secdist::SecdistConfig& secdist)
142 );
143
144 bool IsPeriodicUpdateEnabled() const noexcept;
145
146private:
147 using EventSource = concurrent::AsyncEventSource<const SecdistConfig&>;
148
149 void DoUpdateAndListen(
150 utils::ResourceScopeStorage& scopes,
151 concurrent::FunctionId id,
152 std::string_view name,
153 EventSource::Function&& func
154 );
155
156 class Impl;
157 utils::FastPimpl<Impl, 1280, 16> impl_;
158};
159
160template <typename Class>
161void Secdist::UpdateAndListen(
162 utils::ResourceScopeStorage& scopes,
163 Class* obj,
164 std::string_view name,
165 void (Class::*func)(const storages::secdist::SecdistConfig& secdist)
166) {
167 DoUpdateAndListen(scopes, concurrent::FunctionId(obj), name, [obj, func](const SecdistConfig& config) {
168 (obj->*func)(config);
169 });
170}
171
172namespace detail {
173
174template <typename T>
175const T& SecdistModule<T>::Get(const SecdistConfig& config) {
176 return std::any_cast<const T&>(config.Get(typeid(T), index));
177}
178
179template <typename T>
180std::size_t SecdistModule<T>::index = SecdistConfig::Register<T>(&SecdistModule<T>::Factory);
181
182} // namespace detail
183
184} // namespace storages::secdist
185
186USERVER_NAMESPACE_END