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