userver: userver/storages/redis/dynamic_component.hpp Source File
Loading...
Searching...
No Matches
dynamic_component.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/redis/dynamic_component.hpp
4/// @brief @copybrief components::DynamicRedis
5
6#include <memory>
7#include <string>
8
9#include <userver/components/component_base.hpp>
10#include <userver/components/component_fwd.hpp>
11#include <userver/concurrent/variable.hpp>
12#include <userver/dynamic_config/source.hpp>
13#include <userver/engine/shared_mutex.hpp>
14#include <userver/rcu/rcu.hpp>
15#include <userver/storages/redis/base.hpp>
16#include <userver/storages/redis/dynamic_redis.hpp>
17#include <userver/storages/redis/fwd.hpp>
18#include <userver/storages/redis/sharding_strategies.hpp>
19#include <userver/storages/redis/wait_connected_mode.hpp>
20#include <userver/storages/secdist/secdist.hpp>
21#include <userver/testsuite/redis_control.hpp>
22#include <userver/utils/statistics/entry.hpp>
23
24USERVER_NAMESPACE_BEGIN
25
26/// Components, clients and helpers for different databases and storages
27namespace storages {}
28
29/// Valkey and Redis client and helpers
30namespace storages::redis {
31
32class SubscribeClientImpl;
33
34namespace impl {
35class Sentinel;
36class ThreadPools;
37} // namespace impl
38} // namespace storages::redis
39
40namespace components {
41
42/// @ingroup userver_components
43///
44/// @brief Valkey and Redis dynamic client component, that does not require secdist
45///
46/// Provides access to a valkey or redis cluster.
47///
48/// ## Dynamic options:
49/// Same as for @ref components::Redis
50///
51/// ## Static options of components::Redis :
52/// @include{doc} scripts/docs/en/components_schema/redis/src/storages/redis/dynamic_component.md
53///
54/// Options inherited from @ref components::ComponentBase :
55/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
57public:
58 DynamicRedis(const ComponentConfig& config, const ComponentContext& component_context);
59
60 ~DynamicRedis() override;
61
62 /// @ingroup userver_component_names
63 /// @brief The default name of components::DynamicRedis
64 static constexpr std::string_view kName = "dynamic-redis";
65
66 /// @brief Adds a new client with the specified name and settings.
67 ///
68 /// @param name the name of the client
69 /// @param settings the dynamic settings for the client
70 /// @return true if the client was added, false if a client with the same name already exists
71 bool AddClient(const std::string& name, const storages::redis::DynamicSettings& settings);
72
73 /// @brief Removes a client with the specified name.
74 ///
75 /// @param name the name of the client to remove
76 /// @return true if the client was removed, false if no client with the specified name exists
77 bool RemoveClient(const std::string& name);
78
79 /// @brief Retrieves a dynamically added client by name.
80 ///
81 /// @param name the name of the client
82 /// @param wait_connected wait mode for the client connection
83 /// @return shared reference to the client or throws out_of_range exception if not found
84 utils::SharedRef<storages::redis::Client> GetDynamicClient(
85 const std::string& name,
86 storages::redis::RedisWaitConnected wait_connected = {}
87 ) const;
88
89 /// @brief Lists the names of all dynamically added clients.
90 ///
91 /// @return a set of client names
92 std::unordered_set<std::string> ListDynamicClients() const;
93
94 static yaml_config::Schema GetStaticConfigSchema();
95
96private:
97 void OnConfigUpdate(const dynamic_config::Snapshot& cfg);
98
99 void WriteStatistics(utils::statistics::Writer& writer);
100
101 std::shared_ptr<storages::redis::impl::ThreadPools> thread_pools_;
102 storages::redis::DynamicRedis dynamic_redis_;
103 testsuite::RedisControl testsuite_redis_control_;
104 dynamic_config::Source config_;
105 concurrent::AsyncEventSubscriberScope config_subscription_;
106
107 utils::statistics::Entry statistics_holder_;
108 rcu::Variable<storages::redis::MetricsSettings> metrics_settings_;
109};
110
111template <>
112inline constexpr bool kHasValidate<DynamicRedis> = true;
113
114} // namespace components
115
116USERVER_NAMESPACE_END