Retry API calls upon RestException
yarn add @rc-ex/retry
import RingCentral from '@rc-ex/core';
import RetryExtension from '@rc-ex/retry';
const rc = new RingCentral(...);
const retryExtension = new RetryExtension(retryOptions);
await rc.installExtension(retryExtension);
RetryExtension
constructor accepts optional RetryOptions
as parameter:
type RetryOptions = {
shouldRetry?: ShouldRetry;
retryInterval?: RetryInterval;
};
ShouldRetry
defines condition about should retry or abort:
type ShouldRetry = (
restException: RestException,
retriesAttempted: number,
) => boolean;
By default, ShouldRetry
returns true when restException.response.status
is
429 or 503 and retriesAttempted
is smaller than 3:
((restException, retriesAttempted) => {
return retriesAttempted < 3 &&
[429, 503].includes(restException.response.status);
});
RetryInterval
defines how long should wait before try:
type RetryInterval = (
restException: RestException,
retriesAttempted: number,
) => number;
By default RetryInterval
is 60 seconds with exponential back off:
((restException, retriesAttempted) => {
return 60 * 1000 * Math.pow(2, retriesAttempted); // exponential back off
});