Repository navigation
Conversation
|
@beebah30 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Author
|
Superseded by #183. Both PRs fix the same issue (#175), and both add
Closing this one in favour of #183. The injectable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
ApiKey.ratePerDaywas stored, returned byPOST /admin/keys, loaded intoreq.apiKey— and never enforced. The global@fastify/rate-limitregistration only readratePerMinover a one-minute window, so a key issued with--per-day 100still got ~86,400 requests a day. This adds a Redis-backed, UTC-day-scoped counter that enforces the daily ceiling, keeps the per-minute limit unchanged, and fails closed when the counter store is unavailable.Related Issue
ratePerDayis stored, returned, advertised — and never enforced (ratePerDayis stored, returned, advertised — and never enforced #175)Changes
API
[ADD]
src/api/dailyQuota.tsconsumeDailyQuota(keyId, limit, store, now)incrementslens:api-key:quota:day:<yyyy-mm-dd>:<keyId>and returns an allow/deny decision plusretryAfterSecondsto the next UTC midnight. Counting happens before the decision, so a rejected request still consumes a slot.registerDailyQuotais afastify-pluginonRequesthook. It skips unauthenticated/public routes, skips keys whoseratePerDayis absent or<= 0, and returns:429 Too Many Requestswith{ statusCode, error, message, retryAfter }(matching the existing limiter error shape) plus aRetry-Afterheader when the day's quota is exceeded —retryAfterpoints at the next UTC day boundary.503 Service Unavailablewhen Redis cannot be reached, so the quota is not silently disabled. The decision (fail closed) is documented in the module and matchessrc/bazaar/rateLimit.ts.[MODIFY]
src/index.tsregisterDailyQuotaafter@fastify/rate-limit. Both useonRequest, and hooks run in registration order, so a request already rejected by the minute limiter never spends a daily slot.Tests
src/__tests__/dailyQuota.test.tssecondsUntilNextUtcDayfor the boundary; fail-closed on store error.retryAfterat the day boundary; counter resets across the UTC day boundary; per-minute limit still 429s; counter survives a fresh app instance sharing the store (process restart); store outage returns 503.Changeset
.changeset/daily-api-key-quota.md(patch).Verification Results
retryAfterpointing at the next day boundary, matching the existing error shape{ statusCode, error, message, retryAfter }429 +Retry-Afterheader;retryAfter= seconds to next UTC midnight@fastify/rate-limituntouched; daily hook registered after it and covered by a testsrc/__tests__/dailyQuota.test.ts(not executed in this API-only workflow)error; documented insrc/api/dailyQuota.tsand the changesetCloses #175