fix(retry): avoid CPU busy wait
This commit is contained in:
1 parent
f0d5802b6f
commit
20c9ced3d3
2 files changed
+5
-15
No files matched your search
+2
-14
@@ -169,20 +169,8 @@ export async function checkRateLimit(
|
||||
* @param seconds - Number of seconds to delay
|
||||
* @returns Promise that resolves after the specified delay
|
||||
*/
|
||||
export async function delayRequest(seconds: number): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
// Use a simple delay implementation for Workers environment
|
||||
const start = Date.now();
|
||||
const checkTime = () => {
|
||||
if (Date.now() - start >= seconds * 1000) {
|
||||
resolve();
|
||||
} else {
|
||||
// Use minimal delay to prevent blocking
|
||||
Promise.resolve().then(checkTime);
|
||||
}
|
||||
};
|
||||
checkTime();
|
||||
});
|
||||
export function delayRequest(seconds: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,7 +16,7 @@ describe("Rate Limit Module", () => {
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe("getClientIP", () => {
|
||||
@@ -90,10 +90,12 @@ describe("Rate Limit Module", () => {
|
||||
});
|
||||
|
||||
it("should handle zero delay", async () => {
|
||||
const timer = jest.spyOn(globalThis, "setTimeout");
|
||||
const startTime = Date.now();
|
||||
await delayRequest(0);
|
||||
const endTime = Date.now();
|
||||
|
||||
expect(timer).toHaveBeenCalledTimes(1);
|
||||
expect(endTime - startTime).toBeLessThan(50);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user