userver: userver/utils/impl/userver_experiments.hpp Source File
Loading...
Searching...
No Matches
userver_experiments.hpp
1#pragma once
2
3#include <stdexcept>
4#include <string>
5#include <unordered_set>
6#include <vector>
7
8#include <userver/utils/not_null.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace utils::impl {
13
14class UserverExperiment final {
15 public:
16 // Setting 'userver_experiments_force_enabled: true' in the static config
17 // root results in batch-enabling the experiments created with
18 // 'force_enabling_allowed = true'.
19 explicit UserverExperiment(std::string name,
20 bool force_enabling_allowed = false);
21
22 UserverExperiment(UserverExperiment&&) = delete;
23 UserverExperiment& operator=(UserverExperiment&&) = delete;
24
25 bool IsEnabled() const noexcept { return enabled_; }
26 bool IsForceEnablingAllowed() const { return force_enabling_allowed_; }
27 const std::string& GetName() const { return name_; }
28
29 private:
30 friend struct UserverExperimentSetter;
31
32 std::string name_;
33 bool enabled_{false};
34 bool force_enabling_allowed_{false};
35};
36
37class InvalidUserverExperiments final : public std::runtime_error {
38 using std::runtime_error::runtime_error;
39};
40
41using UserverExperimentSet = std::unordered_set<std::string>;
42
43/// Reverts all changes to experiments in the destructor
44class UserverExperimentsScope final {
45 public:
46 UserverExperimentsScope();
47
48 UserverExperimentsScope(UserverExperimentsScope&&) = delete;
49 UserverExperimentsScope& operator=(UserverExperimentsScope&&) = delete;
50 ~UserverExperimentsScope();
51
52 void Set(UserverExperiment& experiment, bool value) noexcept;
53
54 /// @throws InvalidUserverExperiments on name mismatch
55 void EnableOnly(const UserverExperimentSet& enabled_experiments,
56 bool force_enable = false);
57
58 private:
59 const std::vector<utils::NotNull<UserverExperiment*>> old_enabled_;
60};
61
62// TODO move to userver/mongo once the issues with linker are resolved.
63extern UserverExperiment kRedisClusterAutoTopologyExperiment;
64extern UserverExperiment kRedisRetryBudgetExperiment;
65
66} // namespace utils::impl
67
68USERVER_NAMESPACE_END