1
#pragma once
2

            
3
#include <cstdint>
4
#include <memory>
5
#include <string>
6
#include <vector>
7

            
8
#include "envoy/config/core/v3/base.pb.h"
9
#include "envoy/http/filter.h"
10
#include "envoy/ratelimit/ratelimit.h"
11

            
12
namespace Envoy {
13
namespace Router {
14

            
15
/**
16
 * Base interface for generic rate limit override action.
17
 */
18
class RateLimitOverrideAction {
19
public:
20
4
  virtual ~RateLimitOverrideAction() = default;
21

            
22
  /**
23
   * Potentially populate the descriptors 'limit' property with a RateLimitOverride instance
24
   * @param descriptor supplies the descriptor to optionally fill.
25
   * @param metadata supplies the dynamic metadata for the request.
26
   * @return true if RateLimitOverride was set in the descriptor.
27
   */
28
  virtual bool populateOverride(RateLimit::Descriptor& descriptor,
29
                                const envoy::config::core::v3::Metadata* metadata) const PURE;
30
};
31

            
32
using RateLimitOverrideActionPtr = std::unique_ptr<RateLimitOverrideAction>;
33

            
34
/**
35
 * Rate limit configuration.
36
 */
37
class RateLimitPolicyEntry {
38
public:
39
303
  virtual ~RateLimitPolicyEntry() = default;
40

            
41
  /**
42
   * @return the stage value that the configuration is applicable to.
43
   */
44
  virtual uint64_t stage() const PURE;
45

            
46
  /**
47
   * @return runtime key to be set to disable the configuration.
48
   */
49
  virtual const std::string& disableKey() const PURE;
50

            
51
  /**
52
   * @return true if this rate limit policy should be applied on stream done.
53
   */
54
  virtual bool applyOnStreamDone() const PURE;
55

            
56
  /**
57
   * Potentially populate the descriptor array with new descriptors to query.
58
   * @param descriptors supplies the descriptor array to optionally fill.
59
   * @param local_service_cluster supplies the name of the local service cluster.
60
   * @param headers supplies the header for the request.
61
   * @param info stream info associated with the request
62
   */
63
  virtual void populateDescriptors(std::vector<RateLimit::Descriptor>& descriptors,
64
                                   const std::string& local_service_cluster,
65
                                   const Http::RequestHeaderMap& headers,
66
                                   const StreamInfo::StreamInfo& info) const PURE;
67

            
68
  /**
69
   * Potentially populate the local descriptor array with new descriptors to query.
70
   * @param descriptors supplies the descriptor array to optionally fill.
71
   * @param local_service_cluster supplies the name of the local service cluster.
72
   * @param headers supplies the header for the request.
73
   * @param info stream info associated with the request
74
   */
75
  virtual void populateLocalDescriptors(std::vector<RateLimit::LocalDescriptor>& descriptors,
76
                                        const std::string& local_service_cluster,
77
                                        const Http::RequestHeaderMap& headers,
78
                                        const StreamInfo::StreamInfo& info) const PURE;
79
};
80

            
81
/**
82
 * Rate limiting policy.
83
 */
84
class RateLimitPolicy {
85
public:
86
17051
  virtual ~RateLimitPolicy() = default;
87

            
88
  /**
89
   * @return true if there is no rate limit policy for all stage settings.
90
   */
91
  virtual bool empty() const PURE;
92

            
93
  /**
94
   * @param stage the value for finding applicable rate limit configurations.
95
   * @return set of RateLimitPolicyEntry that are applicable for a stage.
96
   */
97
  virtual const std::vector<std::reference_wrapper<const RateLimitPolicyEntry>>&
98
  getApplicableRateLimit(uint64_t stage) const PURE;
99
};
100

            
101
} // namespace Router
102
} // namespace Envoy