#587 Add tenant-scoped rate limit for /bookings/search using leaky-bu… - #739
Open
Kappa16 wants to merge 1 commit into
Conversation
…sing leaky-bucket algorithm FIXED
|
@Kappa16 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! 🚀 |
| } | ||
|
|
||
| function hashId(id: string): string { | ||
| return createHash("sha256").update(id, "utf8").digest("hex").slice(0, 32); |
Author
|
@thlpkee20-wq PLS REVIEW |
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.
🔍 Findings (what was discovered in the codebase)
Finding 1 — The referenced file did not exist; the endpoint didn't exist either.
The issue said "Move /bookings/search from a coarse global limit…" — but no
/bookings/searchroute existed in code, docs, or git history. So the fix had to create the endpoint and apply the per-tenant limiter to it, rather than edit an existing route.Finding 2 — The "coarse global limit" is real: one shared fixed window for everyone.
A single
express-rate-limitfixed-window budget is applied per-principal with identical global config — but there's no tenant dimension at all. More importantly, there's an implicit starvation pattern: any endpoint sharing the window/config and any tenant flooding traffic degrades the whole class of users — the noisy-neighbor scenario from the issue. Also observed:fairQueueBurnRateTotalalready misuses a tenant-ish label without real bucketing.Finding 3 —
mainwas already broken upstream (pre-existing, not caused by the fix).tsc --noEmit: 7 syntax errors onorigin/maininmarketplaceSearchSchema.ts/marketplaceSearchService.ts— identical 7 on my branch (proven by direct dual-checkout comparison, and the files are byte-identical between refs).main(missing exports likeFraudReasonCode, undefined helpers likeresetSeniorPool, etc.). My branch: zero regressions against that baseline.Finding 4 — repo conventions to respect (found during the review):
createBudgetedGauge/Counter) → my gauge must be budgeted too (chose 256).rl:) with hashed IPs — my keys followrlb:+ hashing conventions.🛠 Fix features (what was built)
src/middleware/tenantLeakyBucket.tsEVALSHA+EVALfallback onNOSCRIPT), keysrlb:bookings:search:<type>:<id>, idle buckets auto-expire (TTL = 2 drains + 1 s)Retry-Aftercomputed from live bucket state (never a constant) +X-RateLimit-Limit/Remaining/Resetx-tenant-idheader deliberately ignored; identifiers canonicalized/hashed (anti key-injection, bounded cardinality)rate_limit_bucket_burn{tenant}gauge +rate_limit_redis_failures_totalcountersrc/metrics.tsRedisLeakyBucketStoreGET /api/v1/bookings/search(auth, q/status/slotId/date-range filters, pagination, tenant-isolated results), mounted at/api/v1/bookingssrc/routes/bookings.ts,src/app.tsBOOKINGS_SEARCH_RATE_PER_SECOND(60) /BOOKINGS_SEARCH_BURST(120) /BOOKINGS_SEARCH_REDIS_TIMEOUT_MS(250)src/config/env.ts,config.service.ts,.env.exampledocs/api/bookings-search.md,docs/api/bookings-search-test-notes.mdBlast radius:
12 files changed, 2,081 insertions(+), 0 deletions— purely additive; no existing code path was altered, which is why the regression audit shows 0 impact.CLOSE #587