Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ RateLimitQuotaUsageReports RateLimitClientImpl::buildReport(absl::optional<size_
*usage->mutable_bucket_id() = bucket->bucket_id;
usage->set_num_requests_allowed(bucket->quota_usage.num_requests_allowed);
usage->set_num_requests_denied(bucket->quota_usage.num_requests_denied);

auto now = std::chrono::duration_cast<std::chrono::nanoseconds>(
time_source_.monotonicTime().time_since_epoch());
// For the newly created bucket (i.e., `bucket_id` input is not null), its time
Expand All @@ -48,6 +49,10 @@ RateLimitQuotaUsageReports RateLimitClientImpl::buildReport(absl::optional<size_

// Update the last_report time point.
bucket->quota_usage.last_report = now;
// Reset the number of request allowed/denied. The RLQS server expects the client to report
// those two usage numbers only for last report period.
bucket->quota_usage.num_requests_allowed = 0;
bucket->quota_usage.num_requests_denied = 0;
}

// Set the domain name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,16 @@ TEST_P(RateLimitQuotaIntegrationTest, BasicFlowPeriodicalReport) {
// reports should be built in filter.cc
envoy::service::rate_limit_quota::v3::RateLimitQuotaUsageReports reports;
ASSERT_TRUE(rlqs_stream_->waitForGrpcMessage(*dispatcher_, reports));

// Verify the usage report content.
ASSERT_THAT(reports.bucket_quota_usages_size(), 1);
const auto& usage = reports.bucket_quota_usages(0);
// We only send single downstream client request and it is allowed.
EXPECT_EQ(usage.num_requests_allowed(), 1);
EXPECT_EQ(usage.num_requests_denied(), 0);
// It is first report so the time_elapsed is 0.
EXPECT_EQ(Protobuf::util::TimeUtil::DurationToSeconds(usage.time_elapsed()), 0);

rlqs_stream_->startGrpcStream();

// Build the response.
Expand Down Expand Up @@ -408,14 +418,16 @@ TEST_P(RateLimitQuotaIntegrationTest, BasicFlowPeriodicalReport) {
ASSERT_TRUE(rlqs_stream_->waitForGrpcMessage(*dispatcher_, reports));

// Verify the usage report content.
for (const auto& usage : reports.bucket_quota_usages()) {
// We only send single downstream client request and it is allowed.
EXPECT_EQ(usage.num_requests_allowed(), 1);
EXPECT_EQ(usage.num_requests_denied(), 0);
// time_elapsed equals to periodical reporting interval.
EXPECT_EQ(Protobuf::util::TimeUtil::DurationToSeconds(usage.time_elapsed()),
report_interval_sec);
}
ASSERT_THAT(reports.bucket_quota_usages_size(), 1);
const auto& usage = reports.bucket_quota_usages(0);
// Report only represents the usage since last report.
// In the periodical report case here, the number of request allowed and denied is 0 since no
// new requests comes in.
EXPECT_EQ(usage.num_requests_allowed(), 0);
EXPECT_EQ(usage.num_requests_denied(), 0);
// time_elapsed equals to periodical reporting interval.
EXPECT_EQ(Protobuf::util::TimeUtil::DurationToSeconds(usage.time_elapsed()),
report_interval_sec);

// Build the rlqs server response.
envoy::service::rate_limit_quota::v3::RateLimitQuotaResponse rlqs_response2;
Expand Down