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

rlqs: add logging around token bucket #32612

Merged
merged 4 commits into from
Mar 4, 2024
Merged
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
19 changes: 13 additions & 6 deletions source/extensions/filters/http/rate_limit_quota/client_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ void RateLimitClientImpl::onReceiveMessage(RateLimitQuotaResponsePtr&& response)

// Get the hash id value from BucketId in the response.
const size_t bucket_id = MessageUtil::hash(action.bucket_id());
ENVOY_LOG(trace,
"Received a response for bucket id proto :\n {}, and generated "
"the associated hashed bucket id: {}",
action.bucket_id().DebugString(), bucket_id);
if (quota_buckets_.find(bucket_id) == quota_buckets_.end()) {
// The response should be matched to the report we sent.
ENVOY_LOG(error,
"Received a response, but but it is not matched any quota "
"cache entry: ",
ENVOY_LOG(error, "The received response is not matched to any quota cache entry: ",
response->ShortDebugString());
} else {
quota_buckets_[bucket_id]->bucket_action = action;
Expand All @@ -98,9 +100,14 @@ void RateLimitClientImpl::onReceiveMessage(RateLimitQuotaResponsePtr&& response)
double fill_rate_per_sec =
static_cast<double>(rate_limit_strategy.token_bucket().tokens_per_fill().value()) /
fill_interval_sec;

quota_buckets_[bucket_id]->token_bucket_limiter = std::make_unique<TokenBucketImpl>(
rate_limit_strategy.token_bucket().max_tokens(), time_source_, fill_rate_per_sec);
uint32_t max_tokens = rate_limit_strategy.token_bucket().max_tokens();
ENVOY_LOG(
trace,
"Created the token bucket limiter for hashed bucket id: {}, with max_tokens: {}; "
"fill_interval_sec: {}; fill_rate_per_sec: {}.",
bucket_id, max_tokens, fill_interval_sec, fill_rate_per_sec);
quota_buckets_[bucket_id]->token_bucket_limiter =
std::make_unique<TokenBucketImpl>(max_tokens, time_source_, fill_rate_per_sec);
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions source/extensions/filters/http/rate_limit_quota/filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Http::FilterHeadersStatus RateLimitQuotaFilter::decodeHeaders(Http::RequestHeade

BucketId bucket_id_proto = ret.value();
const size_t bucket_id = MessageUtil::hash(bucket_id_proto);
ENVOY_LOG(trace, "Generated the associated hashed bucket id: {} for bucket id proto:\n {}",
bucket_id, bucket_id_proto.DebugString());
if (quota_buckets_.find(bucket_id) == quota_buckets_.end()) {
// For first matched request, create a new bucket in the cache and sent the report to RLQS
// server immediately.
Expand Down Expand Up @@ -190,9 +192,13 @@ Http::FilterHeadersStatus RateLimitQuotaFilter::processCachedBucket(size_t bucke
if (limiter->consume(1, /*allow_partial=*/false)) {
// Request is allowed.
quota_buckets_[bucket_id]->quota_usage.num_requests_allowed += 1;
ENVOY_LOG(trace, "Request with hashed bucket_id {} is allowed by token bucket limiter.",
bucket_id);
} else {
// Request is throttled.
quota_buckets_[bucket_id]->quota_usage.num_requests_denied += 1;
ENVOY_LOG(trace, "Request with hashed bucket_id {} is throttled by token bucket limiter",
bucket_id);
// TODO(tyxia) Build the customized response based on `DenyResponseSettings` if it is
// configured.
callbacks_->sendLocalReply(Envoy::Http::Code::TooManyRequests, "", nullptr, absl::nullopt,
Expand Down
Loading