Line data Source code
1 : #pragma once 2 : 3 : #include <string> 4 : #include <vector> 5 : 6 : #include "envoy/config/typed_config.h" 7 : #include "envoy/http/header_map.h" 8 : #include "envoy/protobuf/message_validator.h" 9 : #include "envoy/server/factory_context.h" 10 : #include "envoy/stream_info/stream_info.h" 11 : #include "envoy/type/v3/ratelimit_unit.pb.h" 12 : 13 : #include "absl/time/time.h" 14 : #include "absl/types/optional.h" 15 : 16 : namespace Envoy { 17 : namespace RateLimit { 18 : 19 : /** 20 : * An optional dynamic override for the rate limit. See ratelimit.proto 21 : */ 22 : struct RateLimitOverride { 23 : uint32_t requests_per_unit_; 24 : envoy::type::v3::RateLimitUnit unit_; 25 : }; 26 : 27 : /** 28 : * A single rate limit request descriptor entry. See ratelimit.proto. 29 : */ 30 : struct DescriptorEntry { 31 : std::string key_; 32 : std::string value_; 33 : 34 0 : friend bool operator==(const DescriptorEntry& lhs, const DescriptorEntry& rhs) { 35 0 : return lhs.key_ == rhs.key_ && lhs.value_ == rhs.value_; 36 0 : } 37 : template <typename H> 38 : friend H AbslHashValue(H h, // NOLINT(readability-identifier-naming) 39 0 : const DescriptorEntry& entry) { 40 0 : return H::combine(std::move(h), entry.key_, entry.value_); 41 0 : } 42 : }; 43 : 44 : /** 45 : * A single rate limit request descriptor. See ratelimit.proto. 46 : */ 47 : struct Descriptor { 48 : std::vector<DescriptorEntry> entries_; 49 : absl::optional<RateLimitOverride> limit_ = absl::nullopt; 50 : }; 51 : 52 : /** 53 : * A single token bucket. See token_bucket.proto. 54 : */ 55 : struct TokenBucket { 56 : uint32_t max_tokens_; 57 : uint32_t tokens_per_fill_; 58 : absl::Duration fill_interval_; 59 : }; 60 : 61 : /** 62 : * A single rate limit request descriptor. See ratelimit.proto. 63 : */ 64 : struct LocalDescriptor { 65 : std::vector<DescriptorEntry> entries_; 66 0 : friend bool operator==(const LocalDescriptor& lhs, const LocalDescriptor& rhs) { 67 0 : return lhs.entries_ == rhs.entries_; 68 0 : } 69 : }; 70 : 71 : /* 72 : * Base interface for generic rate limit descriptor producer. 73 : */ 74 : class DescriptorProducer { 75 : public: 76 4 : virtual ~DescriptorProducer() = default; 77 : 78 : /** 79 : * Potentially fill a descriptor entry to the end of descriptor. 80 : * @param descriptor_entry supplies the descriptor entry to optionally fill. 81 : * @param local_service_cluster supplies the name of the local service cluster. 82 : * @param headers supplies the header for the request. 83 : * @param info stream info associated with the request 84 : * @return true if the producer populated the descriptor. 85 : */ 86 : virtual bool populateDescriptor(DescriptorEntry& descriptor_entry, 87 : const std::string& local_service_cluster, 88 : const Http::RequestHeaderMap& headers, 89 : const StreamInfo::StreamInfo& info) const PURE; 90 : }; 91 : 92 : using DescriptorProducerPtr = std::unique_ptr<DescriptorProducer>; 93 : 94 : /** 95 : * Implemented by each custom rate limit descriptor extension and registered via 96 : * Registry::registerFactory() or the convenience class RegisterFactory. 97 : */ 98 : class DescriptorProducerFactory : public Config::TypedFactory { 99 : public: 100 0 : ~DescriptorProducerFactory() override = default; 101 : 102 : /** 103 : * Creates a particular DescriptorProducer implementation. 104 : * 105 : * @param config supplies the configuration for the descriptor extension. 106 : * @param context supplies the factory context. 107 : * @return DescriptorProducerPtr the rate limit descriptor producer which will be used to 108 : * populate rate limit descriptors. 109 : */ 110 : virtual DescriptorProducerPtr 111 : createDescriptorProducerFromProto(const Protobuf::Message& config, 112 : Server::Configuration::CommonFactoryContext& context) PURE; 113 : 114 4 : std::string category() const override { return "envoy.rate_limit_descriptors"; } 115 : }; 116 : 117 : } // namespace RateLimit 118 : } // namespace Envoy