signal-desktop/ts/jobs/helpers/sleepForRateLimitRetryAfterTime.ts

33 lines
832 B
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
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';
export async function sleepForRateLimitRetryAfterTime({
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) {
return;
}
2022-01-12 00:50:11 +00:00
const retryAfter = Math.min(findRetryAfterTimeFromError(err), timeRemaining);
log.info(
`Got a 413 or 429 response code. Sleeping for ${retryAfter} millisecond(s)`
);
2023-02-24 19:03:17 +00:00
await sleeper.sleep(
retryAfter,
'sleepForRateLimitRetryAfterTime: Got a 413 or 429 response code',
{ resolveOnShutdown: false }
);
}