-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(limits): merge window and rate limits, fix streaming headers (#134)
Signed-off-by: Tomas Pilar <[email protected]>
- Loading branch information
1 parent
dbefee0
commit e997180
Showing
5 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { FastifyReply } from 'fastify'; | ||
|
||
import { dayjs, getLatestDailyFixedTime } from './datetime'; | ||
|
||
import { ensureRequestContextData } from '@/context'; | ||
|
||
function getNumericHeader(res: FastifyReply, header: string, fallback: number) { | ||
const value = res.getHeader(header); | ||
if (value === undefined) return fallback; | ||
if (typeof value !== 'number') throw new Error('Invalid header type'); | ||
return value; | ||
} | ||
|
||
const RateLimitHeaders = { | ||
LIMIT: 'ratelimit-limit', | ||
REMAINING: 'ratelimit-remaining', | ||
RESET: 'ratelimit-reset', | ||
RETRY: 'retry-after' | ||
} as const; | ||
|
||
export function updateRateLimitHeadersWithDailyQuota({ | ||
quota, | ||
used | ||
}: { | ||
quota: number; | ||
used: number; | ||
}) { | ||
const res = ensureRequestContextData('res'); | ||
res.header( | ||
RateLimitHeaders.LIMIT, | ||
Math.min(getNumericHeader(res, RateLimitHeaders.LIMIT, Infinity), quota) | ||
); | ||
res.header( | ||
RateLimitHeaders.REMAINING, | ||
Math.min(getNumericHeader(res, RateLimitHeaders.REMAINING, Infinity), quota - used) | ||
); | ||
if (quota === used) { | ||
const reset = Math.max( | ||
getNumericHeader(res, RateLimitHeaders.RESET, 0), | ||
getLatestDailyFixedTime().add(1, 'day').unix() - dayjs().unix() | ||
); | ||
res.header(RateLimitHeaders.RESET, reset); | ||
res.header(RateLimitHeaders.RETRY, reset); | ||
} | ||
} |
This file contains 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