Sleep until the GitHub Search rate-limit resets if we're nearly empty.
(res: Response, label: string)
| 111 | |
| 112 | /** Sleep until the GitHub Search rate-limit resets if we're nearly empty. */ |
| 113 | async function respectRateLimit(res: Response, label: string) { |
| 114 | const remaining = Number(res.headers.get("x-ratelimit-remaining") ?? "30"); |
| 115 | if (remaining > 1) return; |
| 116 | const reset = Number(res.headers.get("x-ratelimit-reset") ?? "0") * 1000; |
| 117 | const wait = Math.max(reset - Date.now(), 0) + 1000; |
| 118 | if (wait > 0) { |
| 119 | console.warn( |
| 120 | ` ${label}: rate budget exhausted (remaining=${remaining}), sleeping ${Math.ceil(wait / 1000)}s`, |
| 121 | ); |
| 122 | await sleep(wait); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | async function githubSearch<T>( |
| 127 | endpoint: "code" | "repositories", |