forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrate-limit.ts
More file actions
46 lines (36 loc) · 1.19 KB
/
rate-limit.ts
File metadata and controls
46 lines (36 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import consola from "consola"
import type { State } from "./state"
import { HTTPError } from "./error"
import { sleep } from "./utils"
export async function checkRateLimit(state: State) {
if (state.rateLimitSeconds === undefined) return
const now = Date.now()
if (!state.lastRequestTimestamp) {
state.lastRequestTimestamp = now
return
}
const elapsedSeconds = (now - state.lastRequestTimestamp) / 1000
if (elapsedSeconds > state.rateLimitSeconds) {
state.lastRequestTimestamp = now
return
}
const waitTimeSeconds = Math.ceil(state.rateLimitSeconds - elapsedSeconds)
if (!state.rateLimitWait) {
consola.warn(
`Rate limit exceeded. Need to wait ${waitTimeSeconds} more seconds.`,
)
throw new HTTPError(
"Rate limit exceeded",
Response.json({ message: "Rate limit exceeded" }, { status: 429 }),
)
}
const waitTimeMs = waitTimeSeconds * 1000
consola.warn(
`Rate limit reached. Waiting ${waitTimeSeconds} seconds before proceeding...`,
)
await sleep(waitTimeMs)
// eslint-disable-next-line require-atomic-updates
state.lastRequestTimestamp = now
consola.info("Rate limit wait completed, proceeding with request")
return
}