Skip to content

Commit

Permalink
Updated rate limit message. (dart-lang#7448)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Feb 1, 2024
1 parent fefe86d commit c99648b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 6 additions & 0 deletions app/lib/service/rate_limit/rate_limit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Future<void> _verifyRateLimit({
List<AuditLogRecord>? auditEntriesFromLastDay;

Future<void> check({
required String operation,
required Duration window,
required int? maxCount,
required String windowAsText,
Expand All @@ -94,6 +95,7 @@ Future<void> _verifyRateLimit({
final current = await entry.get();
if (current != null && current.isAfter(clock.now())) {
throw RateLimitException(
operation: operation,
maxCount: maxCount,
window: window,
windowAsText: windowAsText,
Expand All @@ -117,6 +119,7 @@ Future<void> _verifyRateLimit({
.reduce((a, b) => a.isBefore(b) ? a : b);
await entry.set(firstTimestamp.add(window), window);
throw RateLimitException(
operation: operation,
maxCount: maxCount,
window: window,
windowAsText: windowAsText,
Expand All @@ -125,16 +128,19 @@ Future<void> _verifyRateLimit({
}

await check(
operation: rateLimit.operation,
window: Duration(minutes: 2),
maxCount: rateLimit.burst,
windowAsText: 'last few minutes',
);
await check(
operation: rateLimit.operation,
window: Duration(hours: 1),
maxCount: rateLimit.hourly,
windowAsText: 'last hour',
);
await check(
operation: rateLimit.operation,
window: Duration(days: 1),
maxCount: rateLimit.daily,
windowAsText: 'last day',
Expand Down
7 changes: 5 additions & 2 deletions app/lib/shared/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,18 @@ class OperationForbiddenException extends ResponseException {
/// Thrown when the operation is rejected because a rate limit is reached.
class RateLimitException extends ResponseException {
RateLimitException({
required String operation,
required int maxCount,
required String windowAsText,
required Duration window,
}) : super._(
429,
'RateLimit',
'The operation is blocked, as rate limit in the current window '
'The "$operation" operation is blocked, as its rate limit '
'has been reached ($maxCount in the $windowAsText). '
'Please try again later.',
'Please try again later. '
'Please report the issue on https://github.com/dart-lang/pub-dev/issues/new '
'if you think the limit should be increased to accommodate your use case.',
headers: {
'Retry-After': '${window.inSeconds}',
},
Expand Down

0 comments on commit c99648b

Please sign in to comment.