/proc/self/cwd/source/extensions/filters/common/ratelimit/ratelimit_impl.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <chrono> |
4 | | #include <cstdint> |
5 | | #include <string> |
6 | | #include <vector> |
7 | | |
8 | | #include "envoy/config/core/v3/grpc_service.pb.h" |
9 | | #include "envoy/grpc/async_client.h" |
10 | | #include "envoy/grpc/async_client_manager.h" |
11 | | #include "envoy/ratelimit/ratelimit.h" |
12 | | #include "envoy/server/filter_config.h" |
13 | | #include "envoy/service/ratelimit/v3/rls.pb.h" |
14 | | #include "envoy/stats/scope.h" |
15 | | #include "envoy/tracing/tracer.h" |
16 | | #include "envoy/upstream/cluster_manager.h" |
17 | | |
18 | | #include "source/common/common/logger.h" |
19 | | #include "source/common/grpc/typed_async_client.h" |
20 | | #include "source/common/singleton/const_singleton.h" |
21 | | #include "source/extensions/filters/common/ratelimit/ratelimit.h" |
22 | | |
23 | | namespace Envoy { |
24 | | namespace Extensions { |
25 | | namespace Filters { |
26 | | namespace Common { |
27 | | namespace RateLimit { |
28 | | |
29 | | using RateLimitAsyncCallbacks = |
30 | | Grpc::AsyncRequestCallbacks<envoy::service::ratelimit::v3::RateLimitResponse>; |
31 | | |
32 | | struct ConstantValues { |
33 | | const std::string TraceStatus = "ratelimit_status"; |
34 | | const std::string TraceOverLimit = "over_limit"; |
35 | | const std::string TraceOk = "ok"; |
36 | | }; |
37 | | |
38 | | using Constants = ConstSingleton<ConstantValues>; |
39 | | |
40 | | // TODO(htuch): We should have only one client per thread, but today we create one per filter stack. |
41 | | // This will require support for more than one outstanding request per client (limit() assumes only |
42 | | // one today). |
43 | | class GrpcClientImpl : public Client, |
44 | | public RateLimitAsyncCallbacks, |
45 | | public Logger::Loggable<Logger::Id::config> { |
46 | | public: |
47 | | GrpcClientImpl(const Grpc::RawAsyncClientSharedPtr& async_client, |
48 | | const absl::optional<std::chrono::milliseconds>& timeout); |
49 | | ~GrpcClientImpl() override; |
50 | | |
51 | | static void createRequest(envoy::service::ratelimit::v3::RateLimitRequest& request, |
52 | | const std::string& domain, |
53 | | const std::vector<Envoy::RateLimit::Descriptor>& descriptors, |
54 | | uint32_t hits_addend); |
55 | | |
56 | | // Filters::Common::RateLimit::Client |
57 | | void cancel() override; |
58 | | void limit(RequestCallbacks& callbacks, const std::string& domain, |
59 | | const std::vector<Envoy::RateLimit::Descriptor>& descriptors, |
60 | | Tracing::Span& parent_span, const StreamInfo::StreamInfo& stream_info, |
61 | | uint32_t hits_addend = 0) override; |
62 | | |
63 | | // Grpc::AsyncRequestCallbacks |
64 | 0 | void onCreateInitialMetadata(Http::RequestHeaderMap&) override {} |
65 | | void onSuccess(std::unique_ptr<envoy::service::ratelimit::v3::RateLimitResponse>&& response, |
66 | | Tracing::Span& span) override; |
67 | | void onFailure(Grpc::Status::GrpcStatus status, const std::string& message, |
68 | | Tracing::Span& span) override; |
69 | | |
70 | | private: |
71 | | Grpc::AsyncClient<envoy::service::ratelimit::v3::RateLimitRequest, |
72 | | envoy::service::ratelimit::v3::RateLimitResponse> |
73 | | async_client_; |
74 | | Grpc::AsyncRequest* request_{}; |
75 | | absl::optional<std::chrono::milliseconds> timeout_; |
76 | | RequestCallbacks* callbacks_{}; |
77 | | const Protobuf::MethodDescriptor& service_method_; |
78 | | }; |
79 | | |
80 | | /** |
81 | | * Builds the rate limit client. |
82 | | */ |
83 | | ClientPtr rateLimitClient(Server::Configuration::FactoryContext& context, |
84 | | const Grpc::GrpcServiceConfigWithHashKey& config_with_hash_key, |
85 | | const std::chrono::milliseconds timeout); |
86 | | |
87 | | } // namespace RateLimit |
88 | | } // namespace Common |
89 | | } // namespace Filters |
90 | | } // namespace Extensions |
91 | | } // namespace Envoy |