userver: userver/storages/redis/impl/base.hpp Source File
Loading...
Searching...
No Matches
base.hpp
1#pragma once
2
3#include <atomic>
4#include <chrono>
5#include <optional>
6#include <string>
7#include <vector>
8
9#include <userver/logging/fwd.hpp>
10#include <userver/utils/strong_typedef.hpp>
11
12#include <userver/storages/redis/command_control.hpp>
13#include <userver/storages/redis/impl/types.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace redis {
18
19using Password = utils::NonLoggable<class PasswordTag, std::string>;
20
21enum class ConnectionSecurity { kNone, kTLS };
22
23struct ConnectionInfo {
24 std::string host = "localhost";
25 int port = 26379;
26 Password password;
27 bool read_only = false;
28 ConnectionSecurity connection_security = ConnectionSecurity::kNone;
29 using HostVector = std::vector<std::string>;
30
31 ConnectionInfo() = default;
32 ConnectionInfo(std::string host, int port, Password password,
33 bool read_only = false,
34 ConnectionSecurity security = ConnectionSecurity::kNone)
35 : host{std::move(host)},
36 port{port},
37 password{std::move(password)},
38 read_only{read_only},
39 connection_security(security) {}
40};
41
42struct Stat {
43 double tps = 0.0;
44 double queue = 0.0;
45 double inprogress = 0.0;
46 double timeouts = 0.0;
47};
48
49class CmdArgs {
50 public:
51 using CmdArgsArray = std::vector<std::string>;
53
54 CmdArgs() = default;
55
56 template <typename... Args>
57 CmdArgs(Args&&... _args) {
58 Then(std::forward<Args>(_args)...);
59 }
60
61 CmdArgs(const CmdArgs& o) = delete;
62 CmdArgs(CmdArgs&& o) = default;
63
64 CmdArgs& operator=(const CmdArgs& o) = delete;
65 CmdArgs& operator=(CmdArgs&& o) = default;
66
67 template <typename... Args>
68 CmdArgs& Then(Args&&... _args);
69
70 CmdArgs Clone() const {
71 CmdArgs r;
72 r.args = args;
73 return r;
74 }
75
76 CmdArgsChain args;
77};
78
79logging::LogHelper& operator<<(logging::LogHelper& os, const CmdArgs& v);
80
81using ScanCursor = int64_t;
82
83template <typename Arg>
84typename std::enable_if<std::is_arithmetic<Arg>::value, void>::type PutArg(
85 CmdArgs::CmdArgsArray& args_, const Arg& arg) {
86 args_.emplace_back(std::to_string(arg));
87}
88
89void PutArg(CmdArgs::CmdArgsArray& args_, const char* arg);
90
91void PutArg(CmdArgs::CmdArgsArray& args_, const std::string& arg);
92
93void PutArg(CmdArgs::CmdArgsArray& args_, std::string&& arg);
94
95void PutArg(CmdArgs::CmdArgsArray& args_, const std::vector<std::string>& arg);
96
97void PutArg(CmdArgs::CmdArgsArray& args_,
98 const std::vector<std::pair<std::string, std::string>>& arg);
99
100void PutArg(CmdArgs::CmdArgsArray& args_,
101 const std::vector<std::pair<double, std::string>>& arg);
102
103template <typename... Args>
104CmdArgs& CmdArgs::Then(Args&&... _args) {
105 args.emplace_back();
106 auto& new_args = args.back();
107 new_args.reserve(sizeof...(Args));
108 (PutArg(new_args, std::forward<Args>(_args)), ...);
109 return *this;
110}
111
113 bool buffering_enabled{false};
114 size_t commands_buffering_threshold{0};
115 std::chrono::microseconds watch_command_timer_interval{0};
116
117 constexpr bool operator==(const CommandsBufferingSettings& o) const {
118 return buffering_enabled == o.buffering_enabled &&
119 commands_buffering_threshold == o.commands_buffering_threshold &&
120 watch_command_timer_interval == o.watch_command_timer_interval;
121 }
122};
123
124enum class ConnectionMode {
125 kCommands,
126 kSubscriber,
127};
128
129struct MetricsSettings {
130 enum class Level { kCluster, kShard, kInstance };
131
132 struct DynamicSettings {
133 bool timings_enabled{true};
134 bool command_timings_enabled{false};
135 bool request_sizes_enabled{false};
136 bool reply_sizes_enabled{false};
137
138 constexpr bool operator==(const DynamicSettings& rhs) const {
139 return timings_enabled == rhs.timings_enabled &&
140 command_timings_enabled == rhs.command_timings_enabled &&
141 request_sizes_enabled == rhs.request_sizes_enabled &&
142 reply_sizes_enabled == rhs.reply_sizes_enabled;
143 }
144
145 constexpr bool operator!=(const DynamicSettings& rhs) const {
146 return !(*this == rhs);
147 }
148 };
149
150 struct StaticSettings {
151 Level level{Level::kInstance};
152
153 constexpr bool operator==(const StaticSettings& rhs) const {
154 return level == rhs.level;
155 }
156
157 constexpr bool operator!=(const StaticSettings& rhs) const {
158 return !(*this == rhs);
159 }
160 };
161
162 StaticSettings static_settings;
163 DynamicSettings dynamic_settings;
164
165 MetricsSettings(const DynamicSettings& dynamic_settings,
166 const StaticSettings& static_settings)
167 : static_settings(static_settings), dynamic_settings(dynamic_settings) {}
168 MetricsSettings() = default;
169 MetricsSettings(const MetricsSettings&) = default;
170 MetricsSettings(MetricsSettings&&) = default;
171 MetricsSettings& operator=(const MetricsSettings&) = default;
172 MetricsSettings& operator=(MetricsSettings&&) = default;
173
174 constexpr bool operator==(const MetricsSettings& rhs) const {
175 return static_settings == rhs.static_settings &&
176 dynamic_settings == rhs.dynamic_settings;
177 }
178
179 constexpr bool operator!=(const MetricsSettings& rhs) const {
180 return !(*this == rhs);
181 }
182
183 Level GetMetricsLevel() const { return static_settings.level; }
184 bool IsTimingsEnabled() const { return dynamic_settings.timings_enabled; }
185 bool IsCommandTimingsEnabled() const {
186 return dynamic_settings.command_timings_enabled;
187 }
188 bool IsRequestSizesEnabled() const {
189 return dynamic_settings.request_sizes_enabled;
190 }
191 bool IsReplySizesEnabled() const {
192 return dynamic_settings.reply_sizes_enabled;
193 }
194};
195
197 bool per_shard_stats_enabled{true};
198
199 constexpr bool operator==(const PubsubMetricsSettings& rhs) const {
200 return per_shard_stats_enabled == rhs.per_shard_stats_enabled;
201 }
202
203 constexpr bool operator!=(const PubsubMetricsSettings& rhs) const {
204 return !(*this == rhs);
205 }
206};
207
209 bool enable_monitoring{false};
210 bool restrict_requests{false};
211};
212
213struct PublishSettings {
214 size_t shard{0};
215 bool master{true};
217};
218
219} // namespace redis
220
221USERVER_NAMESPACE_END