Repository navigation
fix: avoid extra 100ms sleep after deadline in blockUntilReady - #177
Open
Tyagiquamar wants to merge 1 commit into
Open
Tyagiquamar wants to merge 1 commit into
Tyagiquamar wants to merge 1 commit into
Conversation
Contributor
|
@Tyagiquamar is attempting to deploy a commit to the Upstash Team on Vercel. A member of the Team first needs to authorize it. |
This branch has not been deployed
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.
Summary
Prevent
Ratelimit.blockUntilReadyfrom sleeping an extra 100ms (or running an unnecessary retry) once thetimeoutdeadline has been reached.Root Cause & Problem
In
src/ratelimit.ts,blockUntilReadycomputedwait = Math.min(res.reset, deadline) - Date.now()and slept forwait > 0 ? wait : 100before checkingif (Date.now() > deadline):this.limit(identifier)itself took longer thantimeout(orDate.now()reacheddeadline),wait <= 0, soblockUntilReadyunconditionally slept 100ms even though the deadline had already expired.res.reset <= Date.now()(past reset / clock skew) whileremainingTimeout < 100, the 100ms fallback sleep overslept pastdeadline.Date.now() > deadlineinstead ofDate.now() >= deadlineallowed an extra iteration (and anotherlimit()call + 100ms sleep) when waking up exactly atdeadline.Technical Solution
remainingTimeout = deadline - Date.now()before sleeping andbreakimmediately whenremainingTimeout <= 0.Math.min(100, remainingTimeout)so it never oversleeps pastdeadline.Date.now() >= deadlinefor the post-sleep deadline check.src/block-until-ready-deadline.test.ts.Test Plan
bun test src/block-until-ready-deadline.test.ts src/block-until-ready-denylist.test.ts: 5 pass, 0 fail (19 expect() calls).bunx eslint src/ratelimit.ts src/block-until-ready-deadline.test.ts --quiet: passed with 0 errors.bun run build: CJS, ESM, and DTS build succeeded.