1
#pragma once
2

            
3
#include <string>
4

            
5
#include "envoy/config/core/v3/base.pb.h"
6
#include "envoy/runtime/runtime.h"
7
#include "envoy/type/v3/percent.pb.h"
8

            
9
#include "source/common/protobuf/utility.h"
10

            
11
namespace Envoy {
12
namespace Runtime {
13

            
14
// Helper class for runtime-derived uint32.
15
class UInt32 : Logger::Loggable<Logger::Id::runtime> {
16
public:
17
  UInt32(const envoy::config::core::v3::RuntimeUInt32& uint32_proto, Runtime::Loader& runtime)
18
4
      : runtime_key_(uint32_proto.runtime_key()), default_value_(uint32_proto.default_value()),
19
4
        runtime_(runtime) {}
20

            
21
1
  const std::string& runtimeKey() const { return runtime_key_; }
22

            
23
8
  uint32_t value() const {
24
8
    uint64_t raw_value = runtime_.snapshot().getInteger(runtime_key_, default_value_);
25
8
    if (raw_value > std::numeric_limits<uint32_t>::max()) {
26
2
      ENVOY_LOG_EVERY_POW_2(
27
2
          warn,
28
2
          "parsed runtime value:{} of {} is larger than uint32 max, returning default instead",
29
2
          raw_value, runtime_key_);
30
2
      return default_value_;
31
2
    }
32
6
    return static_cast<uint32_t>(raw_value);
33
8
  }
34

            
35
private:
36
  const std::string runtime_key_;
37
  const uint32_t default_value_;
38
  Runtime::Loader& runtime_;
39
};
40

            
41
// Helper class for runtime-derived boolean feature flags.
42
class FeatureFlag {
43
public:
44
  FeatureFlag(const envoy::config::core::v3::RuntimeFeatureFlag& feature_flag_proto,
45
              Runtime::Loader& runtime)
46
3780
      : runtime_key_(feature_flag_proto.runtime_key()),
47
3780
        default_value_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(feature_flag_proto, default_value, true)),
48
3780
        runtime_(runtime) {}
49

            
50
499201
  bool enabled() const { return runtime_.snapshot().getBoolean(runtime_key_, default_value_); }
51

            
52
private:
53
  const std::string runtime_key_;
54
  const bool default_value_;
55
  Runtime::Loader& runtime_;
56
};
57

            
58
// Helper class for runtime-derived doubles.
59
class Double {
60
public:
61
  Double(const envoy::config::core::v3::RuntimeDouble& double_proto, Runtime::Loader& runtime)
62
32
      : runtime_key_(double_proto.runtime_key()), default_value_(double_proto.default_value()),
63
32
        runtime_(runtime) {}
64
  Double(std::string runtime_key, double default_value, Runtime::Loader& runtime)
65
25
      : runtime_key_(std::move(runtime_key)), default_value_(default_value), runtime_(runtime) {}
66
71
  virtual ~Double() = default;
67

            
68
2
  const std::string& runtimeKey() const { return runtime_key_; }
69

            
70
606508
  virtual double value() const {
71
606508
    return runtime_.snapshot().getDouble(runtime_key_, default_value_);
72
606508
  }
73

            
74
protected:
75
  const std::string runtime_key_;
76
  const double default_value_;
77
  Runtime::Loader& runtime_;
78
};
79

            
80
// Helper class for runtime-derived fractional percent flags.
81
class FractionalPercent {
82
public:
83
  FractionalPercent(
84
      const envoy::config::core::v3::RuntimeFractionalPercent& fractional_percent_proto,
85
      Runtime::Loader& runtime)
86
494
      : runtime_key_(fractional_percent_proto.runtime_key()),
87
494
        default_value_(fractional_percent_proto.default_value()), runtime_(runtime) {}
88

            
89
896
  bool enabled() const { return runtime_.snapshot().featureEnabled(runtime_key_, default_value_); }
90

            
91
private:
92
  const std::string runtime_key_;
93
  const envoy::type::v3::FractionalPercent default_value_;
94
  Runtime::Loader& runtime_;
95
};
96

            
97
// Helper class for runtime-derived percentages.
98
class Percentage : public Double {
99
public:
100
  Percentage(const envoy::config::core::v3::RuntimePercent& percent_proto, Runtime::Loader& runtime)
101
25
      : Double(percent_proto.runtime_key(), percent_proto.default_value().value(), runtime) {}
102

            
103
404222
  double value() const override {
104
404222
    const auto val = Double::value();
105
404222
    if (val <= 100.0 && val >= 0.0) {
106
404216
      return val;
107
404216
    }
108
6
    return default_value_;
109
404222
  }
110
};
111

            
112
} // namespace Runtime
113
} // namespace Envoy