request-hedging
v1.0.1
Published
An implementation of the hedging policy for request retry
Downloads
3
Readme
request-hedging
Request hedging policy in the frontend.
Not requestor, just hedging policy logic implementation.
References
Usage
Install:
npm install request-hedging
# or yarn/pnpm...
Cases
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 });
Options
| 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
|
Local Dev
- node >= 18
- pnpm >= 9
# request-hedging repo
pnpm i
pnpm dev
# Another repo
pnpm link /path/to/request-hedging