Request hedging policy in the frontend.
Not requestor, just hedging policy logic implementation.
Hedging policy can be regarded as an aggressive retry policy. A complete request may send multiple identical requests until one of them returns successfully. We don't need to wait for the previous request to time out before sending a new hedging request, which makes the hedging policy have a better optimization effect for long-tail requests.
Hedging policy in the backend:
npm install request-hedging
# or yarn/pnpm...
Single request hedging:
import { hedging } from 'request-hedging';
const result = await hedging(() => fetch('https://example.com/apis/getData'), {
maxAttempts: 3, // Up to three times attempted (not required)
});
Multiple requests hedging:
import { hedging } from 'request-hedging';
const result = await hedging([
() => fetch('https://example.com/apis/getData'),
() => fetch('https://example.com/backup-apis/getData'),
]);
Use useHedging
:
import { useHedging } from 'request-hedging';
const hedging = useHedging({
maxAttempts: 3,
hedgingDelay: 500,
});
const result1 = await hedging(() => fetch('https://example.com/apis/getData'));
const result2 = await hedging([
() => fetch('https://example.com/apis/getData'),
() => fetch('https://example.com/backup-apis/getData'),
], { maxAttempts: 2 });
name | type | default value |
---|---|---|
maxAttempts | number |
Math.max(this.targets.length, 2) |
hedgingDelay | number (ms) |
1000 |
timeout | number (ms) |
Infinity |
retryableError | boolean | ((error: unknown) => boolean) |
true |
- node >= 18
- pnpm >= 9
# request-hedging repo
pnpm i
pnpm dev
# Another repo
pnpm link /path/to/request-hedging