Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check_only feature to Rate Limit #29541

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/envoy/service/ratelimit/v3/rls.proto
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ message RateLimitRequest {
// Rate limit requests can optionally specify the number of hits a request adds to the matched
// limit. If the value is not set in the message, a request increases the matched limit by 1.
uint32 hits_addend = 3;

// Rate limit requests can optionally specify if they want to check the limit without actually
// consuming a hit. This is useful for clients that want to check if a request is over limit
// without consuming a hit. This is also useful for clients that want to "peek" at the headers
// that would be returned in a response for a given request.
bool check_only = 4;
}

// A response from a ShouldRateLimit call.
Expand Down
4 changes: 3 additions & 1 deletion source/extensions/filters/common/ratelimit/ratelimit.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ class Client {
* @param parent_span source for generating an egress child span as part of the trace.
* @param stream_info supplies the stream info for the request.
* @param hits_addend supplies the number of hits to add to the rate limit counter.
* @param check_only supplies whether to check the limit only without incrementing the counter.
*
*/
virtual void limit(RequestCallbacks& callbacks, const std::string& domain,
const std::vector<Envoy::RateLimit::Descriptor>& descriptors,
Tracing::Span& parent_span, const StreamInfo::StreamInfo& stream_info,
uint32_t hits_addend) PURE;
uint32_t hits_addend,
bool check_only) PURE;
};

using ClientPtr = std::unique_ptr<Client>;
Expand Down
9 changes: 6 additions & 3 deletions source/extensions/filters/common/ratelimit/ratelimit_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ void GrpcClientImpl::cancel() {
void GrpcClientImpl::createRequest(envoy::service::ratelimit::v3::RateLimitRequest& request,
const std::string& domain,
const std::vector<Envoy::RateLimit::Descriptor>& descriptors,
uint32_t hits_addend) {
uint32_t hits_addend,
bool check_only) {
request.set_domain(domain);
request.set_hits_addend(hits_addend);
request.set_check_only(check_only);
for (const Envoy::RateLimit::Descriptor& descriptor : descriptors) {
envoy::extensions::common::ratelimit::v3::RateLimitDescriptor* new_descriptor =
request.add_descriptors();
Expand All @@ -60,12 +62,13 @@ void GrpcClientImpl::createRequest(envoy::service::ratelimit::v3::RateLimitReque
void GrpcClientImpl::limit(RequestCallbacks& callbacks, const std::string& domain,
const std::vector<Envoy::RateLimit::Descriptor>& descriptors,
Tracing::Span& parent_span, const StreamInfo::StreamInfo& stream_info,
uint32_t hits_addend) {
uint32_t hits_addend,
bool check_only) {
ASSERT(callbacks_ == nullptr);
callbacks_ = &callbacks;

envoy::service::ratelimit::v3::RateLimitRequest request;
createRequest(request, domain, descriptors, hits_addend);
createRequest(request, domain, descriptors, hits_addend, check_only);

request_ =
async_client_->send(service_method_, request, *this, parent_span,
Expand Down
6 changes: 4 additions & 2 deletions source/extensions/filters/common/ratelimit/ratelimit_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ class GrpcClientImpl : public Client,
static void createRequest(envoy::service::ratelimit::v3::RateLimitRequest& request,
const std::string& domain,
const std::vector<Envoy::RateLimit::Descriptor>& descriptors,
uint32_t hits_addend);
uint32_t hits_addend,
bool check_only);

// Filters::Common::RateLimit::Client
void cancel() override;
void limit(RequestCallbacks& callbacks, const std::string& domain,
const std::vector<Envoy::RateLimit::Descriptor>& descriptors,
Tracing::Span& parent_span, const StreamInfo::StreamInfo& stream_info,
uint32_t hits_addend = 0) override;
uint32_t hits_addend = 0,
bool check_only = false) override;

// Grpc::AsyncRequestCallbacks
void onCreateInitialMetadata(Http::RequestHeaderMap&) override {}
Expand Down