2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 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';
|
2023-02-24 19:03:17 +00:00
|
|
|
import { sleeper } from '../../util/sleeper';
|
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
|
|
|
);
|
|
|
|
|
2023-02-24 19:03:17 +00:00
|
|
|
await sleeper.sleep(
|
|
|
|
retryAfter,
|
|
|
|
'sleepForRateLimitRetryAfterTime: Got a 413 or 429 response code',
|
|
|
|
{ resolveOnShutdown: false }
|
|
|
|
);
|
2021-09-02 22:31:21 +00:00
|
|
|
}
|