1.0.1 • Published 1 year ago

request-hedging v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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

nametypedefault value
maxAttemptsnumberMath.max(this.targets.length, 2)
hedgingDelaynumber (ms)1000
timeoutnumber (ms)Infinity
retryableErrorboolean \| ((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
1.0.1

1 year ago

1.0.0

1 year ago