Skip to content

Commit

Permalink
misc: update messages
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKolarik committed Sep 30, 2024
1 parent f2d7175 commit ecc3259
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion public/v1/components/responses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ components:
value:
error:
type: too_many_requests
message: Too Many Requests.
message: Too many requests. Please retry in 4 seconds.
probes200:
description: |
A successful request returns status `200 OK` and a body containing a list of all probes currently online and their metadata.
Expand Down
3 changes: 3 additions & 0 deletions public/v1/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ paths:
1. Request the measurement to retrieve its status.
2. If the status is `in-progress`, wait 500 milliseconds and start again at step 1. Note that it's important to wait 500 ms *after* receiving the response rather than using an "every 500ms" interval as for large measurements, the request itself may take a few hundred milliseconds to complete.
3. If the status is anything **other** than `in-progress`, stop. The measurement is no longer running, and its results are final.
> **Important**: Do not query the results of a single measurement more often than every 500 milliseconds. Sending more than two
requests per second may trigger a rate limit and prevent you from accessing the results for a few seconds.
responses:
'200':
$ref: 'components/responses.yaml#/components/responses/measurement200'
Expand Down
7 changes: 5 additions & 2 deletions src/lib/rate-limiter/rate-limiter-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ export const getMeasurementRateLimit = async (ctx: ExtendedContext, next: Next)
await rateLimiter.consume(id);
} catch (error) {
if (error instanceof RateLimiterRes) {
const retryAfter = Math.ceil(error.msBeforeNext / 1000);
const units = retryAfter === 1 ? 'second' : 'seconds';

setRateLimitHeaders(ctx, error);
throw createHttpError(429, 'Too Many Requests.', { type: 'too_many_requests' });
throw createHttpError(429, `Too many requests. Please retry in ${retryAfter} ${units}.`, { type: 'too_many_requests' });
}

throw createHttpError(500);
Expand All @@ -40,5 +43,5 @@ export const getMeasurementRateLimit = async (ctx: ExtendedContext, next: Next)
};

const setRateLimitHeaders = (ctx: ExtendedContext, error: RateLimiterRes) => {
ctx.set('Retry-After', `${Math.round(error.msBeforeNext / 1000)}`);
ctx.set('Retry-After', `${Math.ceil(error.msBeforeNext / 1000)}`);
};

0 comments on commit ecc3259

Please sign in to comment.