2022-01-12 00:50:11 +00:00
|
|
|
// Copyright 2021-2022 Signal Messenger, LLC
|
2021-09-02 22:31:21 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
import type { LoggerType } from '../../types/Logging';
|
2021-09-02 22:31:21 +00:00
|
|
|
import { sleep } from '../../util/sleep';
|
2022-01-12 00:50:11 +00:00
|
|
|
import { findRetryAfterTimeFromError } from './findRetryAfterTimeFromError';
|
2021-09-02 22:31:21 +00:00
|
|
|
|
2022-02-25 00:26:58 +00:00
|
|
|
export async function sleepForRateLimitRetryAfterTime({
|
2021-09-02 22:31:21 +00:00
|
|
|
err,
|
|
|
|
log,
|
|
|
|
timeRemaining,
|
|
|
|
}: Readonly<{
|
|
|
|
err: unknown;
|
|
|
|
log: Pick<LoggerType, 'info'>;
|
|
|
|
timeRemaining: number;
|
|
|
|
}>): Promise<void> {
|
2021-09-27 14:44:09 +00:00
|
|
|
if (timeRemaining <= 0) {
|
2021-09-02 22:31:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-12 00:50:11 +00:00
|
|
|
const retryAfter = Math.min(findRetryAfterTimeFromError(err), timeRemaining);
|
2021-09-02 22:31:21 +00:00
|
|
|
|
|
|
|
log.info(
|
2022-02-25 00:26:58 +00:00
|
|
|
`Got a 413 or 429 response code. Sleeping for ${retryAfter} millisecond(s)`
|
2021-09-02 22:31:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await sleep(retryAfter);
|
|
|
|
}
|