Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/source/extensions/filters/common/ratelimit/ratelimit.h
Line
Count
Source
1
#pragma once
2
3
#include <chrono>
4
#include <memory>
5
#include <string>
6
#include <vector>
7
8
#include "envoy/common/pure.h"
9
#include "envoy/ratelimit/ratelimit.h"
10
#include "envoy/service/ratelimit/v3/rls.pb.h"
11
#include "envoy/singleton/manager.h"
12
#include "envoy/stream_info/stream_info.h"
13
#include "envoy/tracing/tracer.h"
14
15
#include "absl/types/optional.h"
16
17
namespace Envoy {
18
namespace Extensions {
19
namespace Filters {
20
namespace Common {
21
namespace RateLimit {
22
23
/**
24
 * Possible async results for a limit call.
25
 */
26
enum class LimitStatus {
27
  // The request is not over limit.
28
  OK,
29
  // The rate limit service could not be queried.
30
  Error,
31
  // The request is over limit.
32
  OverLimit
33
};
34
35
using DescriptorStatusList =
36
    std::vector<envoy::service::ratelimit::v3::RateLimitResponse_DescriptorStatus>;
37
using DescriptorStatusListPtr = std::unique_ptr<DescriptorStatusList>;
38
using DynamicMetadataPtr = std::unique_ptr<ProtobufWkt::Struct>;
39
40
/**
41
 * Async callbacks used during limit() calls.
42
 */
43
class RequestCallbacks {
44
public:
45
13
  virtual ~RequestCallbacks() = default;
46
47
  /**
48
   * Called when a limit request is complete. The resulting status, response headers
49
   * and request headers to be forwarded to the upstream are supplied.
50
   *
51
   * @status The ratelimit status
52
   * @descriptor_statuses The descriptor statuses
53
   * @response_headers_to_add The headers to add to the downstream response, for non-OK statuses
54
   * @request_headers_to_add The headers to add to the upstream request, if not ratelimited
55
   * @response_body The response body to use for the downstream response, for non-OK statuses. May
56
   * contain non UTF-8 values (e.g. binary data).
57
   */
58
  virtual void complete(LimitStatus status, DescriptorStatusListPtr&& descriptor_statuses,
59
                        Http::ResponseHeaderMapPtr&& response_headers_to_add,
60
                        Http::RequestHeaderMapPtr&& request_headers_to_add,
61
                        const std::string& response_body,
62
                        DynamicMetadataPtr&& dynamic_metadata) PURE;
63
};
64
65
/**
66
 * A client used to query a centralized rate limit service.
67
 */
68
class Client {
69
public:
70
13
  virtual ~Client() = default;
71
72
  /**
73
   * Cancel an inflight limit request.
74
   */
75
  virtual void cancel() PURE;
76
77
  /**
78
   * Request a limit check. Note that this abstract API matches the design of Lyft's GRPC based
79
   * rate limit service. See ratelimit.proto for details. Any other rate limit implementations
80
   * plugged in at this layer should support the same high level API.
81
   * NOTE: It is possible for the completion callback to be called immediately on the same stack
82
   *       frame. Calling code should account for this.
83
   * @param callbacks supplies the completion callbacks.
84
   * @param domain specifies the rate limit domain.
85
   * @param descriptors specifies a list of descriptors to query.
86
   * @param parent_span source for generating an egress child span as part of the trace.
87
   * @param stream_info supplies the stream info for the request.
88
   * @param hits_addend supplies the number of hits to add to the rate limit counter.
89
   *
90
   */
91
  virtual void limit(RequestCallbacks& callbacks, const std::string& domain,
92
                     const std::vector<Envoy::RateLimit::Descriptor>& descriptors,
93
                     Tracing::Span& parent_span, const StreamInfo::StreamInfo& stream_info,
94
                     uint32_t hits_addend) PURE;
95
};
96
97
using ClientPtr = std::unique_ptr<Client>;
98
99
} // namespace RateLimit
100
} // namespace Common
101
} // namespace Filters
102
} // namespace Extensions
103
} // namespace Envoy