userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
dynamic_redis.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/storages/redis/dynamic_redis.hpp
4
/// @brief @copybrief storages::redis::DynamicRedis
5
6
#
include
<
memory
>
7
#
include
<
string
>
8
#
include
<
unordered_map
>
9
#
include
<
unordered_set
>
10
11
#
include
<
userver
/
concurrent
/
variable
.
hpp
>
12
#
include
<
userver
/
dynamic_config
/
source
.
hpp
>
13
#
include
<
userver
/
engine
/
shared_mutex
.
hpp
>
14
#
include
<
userver
/
storages
/
redis
/
base
.
hpp
>
15
#
include
<
userver
/
storages
/
redis
/
fwd
.
hpp
>
16
#
include
<
userver
/
storages
/
redis
/
sharding_strategies
.
hpp
>
17
#
include
<
userver
/
storages
/
redis
/
wait_connected_mode
.
hpp
>
18
#
include
<
userver
/
testsuite
/
redis_control
.
hpp
>
19
#
include
<
userver
/
utils
/
not_null
.
hpp
>
20
#
include
<
userver
/
utils
/
statistics
/
entry
.
hpp
>
21
22
USERVER_NAMESPACE_BEGIN
23
24
namespace
storages::
redis
{
25
26
/// @brief Dynamic configuration settings for Redis clients
27
struct
DynamicSettings
final
{
28
struct
HostPort
{
29
std::string host;
30
int
port;
31
32
explicit
HostPort(std::string host = {},
int
port = 0)
noexcept
: host(std::move(host)), port(port) {}
33
};
34
35
std::vector<std::string> shards;
36
std::vector<
HostPort
> sentinels;
37
/// Username for ACL-based auth (Redis 6+). Leave empty for legacy AUTH.
38
std::string
username
;
39
/// Password for nodes
40
storages::
redis
::Password
password
{std::string()};
41
/// Username for sentinel ACL-based auth (Redis 6+). Leave empty for legacy AUTH.
42
std::string
sentinel_username
;
43
/// Password for sentinels. Available since Redis 5.0.1. For early versions should be always empty.
44
storages::
redis
::Password
sentinel_password
{std::string()};
45
storages::
redis
::ConnectionSecurity secure_connection{storages::
redis
::ConnectionSecurity::kNone};
46
std::size_t database_index{0};
47
ShardingStrategy
sharding_strategy;
48
bool
allow_reads_from_master =
false
;
49
};
50
51
class
ClientImpl;
52
53
namespace
impl {
54
class
Sentinel;
55
class
ThreadPools;
56
}
// namespace impl
57
58
/// @brief Manages dynamically created Redis clients
59
class
DynamicRedis
{
60
public
:
61
DynamicRedis() =
default
;
62
63
void
Init(std::shared_ptr<impl::ThreadPools> thread_pools,
testsuite
::
RedisControl
testsuite_redis_control);
64
65
~DynamicRedis();
66
67
/// @brief Adds a new client with the specified name and settings.
68
///
69
/// @param name the name of the client
70
/// @param settings the dynamic settings for the client
71
/// @param config dynamic config source used by the client
72
/// @return true if the client was added, false if a client with the same name already exists
73
bool
AddClient
(
const
std::string& name,
const
DynamicSettings& settings, dynamic_config::Source& config);
74
75
/// @brief Removes a client with the specified name.
76
///
77
/// @param name the name of the client to remove
78
/// @return true if the client was removed, false if no client with the specified name exists
79
bool
RemoveClient
(
const
std::string& name);
80
81
/// @brief Retrieves a dynamically added client by name.
82
///
83
/// @param name the name of the client
84
/// @param wait_connected wait mode for the client connection
85
/// @return shared pointer to the client
86
/// throws std::out_of_range exception if there is no client with requested name
87
utils
::
SharedRef
<
Client
>
GetDynamicClient
(
88
const
std::string& name,
89
storages::
redis
::
RedisWaitConnected
wait_connected = {}
90
)
const
;
91
92
/// @brief Lists the names of all dynamically added clients.
93
///
94
/// @return a set of client names
95
std::unordered_set<std::string>
ListClients
()
const
;
96
97
/// @brief Writes statistics for all dynamic clients.
98
///
99
/// @param writer statistics writer
100
/// @param settings metrics settings
101
void
WriteStatistics
(
utils
::
statistics
::Writer& writer,
const
MetricsSettings
& settings)
const
;
102
103
void
OnConfigUpdate(
const
dynamic_config::Snapshot& cfg);
104
105
private
:
106
std::shared_ptr<impl::ThreadPools> thread_pools_;
107
testsuite
::
RedisControl
testsuite_redis_control_;
108
109
concurrent
::Variable<std::unordered_map<std::string, std::shared_ptr<ClientImpl>>, engine::SharedMutex>
110
dynamic_clients_;
111
};
112
113
}
// namespace storages::redis
114
115
USERVER_NAMESPACE_END
userver
storages
redis
dynamic_redis.hpp
Generated on
for userver by
Doxygen
1.17.0