Improve handling of 413 HTTP responses
This commit is contained in:
parent
8b98035cbf
commit
9791fa43ef
7 changed files with 188 additions and 78 deletions
27
ts/jobs/helpers/getHttpErrorCode.ts
Normal file
27
ts/jobs/helpers/getHttpErrorCode.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
// Copyright 2021 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { isRecord } from '../../util/isRecord';
|
||||
import { parseIntWithFallback } from '../../util/parseIntWithFallback';
|
||||
|
||||
/**
|
||||
* Looks for an HTTP code. First tries the top level error, then looks at its `httpError`
|
||||
* property.
|
||||
*/
|
||||
export function getHttpErrorCode(maybeError: unknown): number {
|
||||
if (!isRecord(maybeError)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const maybeTopLevelCode = parseIntWithFallback(maybeError.code, -1);
|
||||
if (maybeTopLevelCode !== -1) {
|
||||
return maybeTopLevelCode;
|
||||
}
|
||||
|
||||
const { httpError } = maybeError;
|
||||
if (!isRecord(httpError)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return parseIntWithFallback(httpError.code, -1);
|
||||
}
|
|
@ -2,9 +2,8 @@
|
|||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import type { LoggerType } from '../../types/Logging';
|
||||
import { parseIntWithFallback } from '../../util/parseIntWithFallback';
|
||||
import { HTTPError } from '../../textsecure/Errors';
|
||||
import { sleepFor413RetryAfterTimeIfApplicable } from './sleepFor413RetryAfterTimeIfApplicable';
|
||||
import { sleepFor413RetryAfterTime } from './sleepFor413RetryAfterTime';
|
||||
import { getHttpErrorCode } from './getHttpErrorCode';
|
||||
|
||||
export async function handleCommonJobRequestError({
|
||||
err,
|
||||
|
@ -15,17 +14,14 @@ export async function handleCommonJobRequestError({
|
|||
log: LoggerType;
|
||||
timeRemaining: number;
|
||||
}>): Promise<void> {
|
||||
if (!(err instanceof HTTPError)) {
|
||||
throw err;
|
||||
switch (getHttpErrorCode(err)) {
|
||||
case 413:
|
||||
await sleepFor413RetryAfterTime({ err, log, timeRemaining });
|
||||
return;
|
||||
case 508:
|
||||
log.info('server responded with 508. Giving up on this job');
|
||||
return;
|
||||
default:
|
||||
throw err;
|
||||
}
|
||||
|
||||
const code = parseIntWithFallback(err.code, -1);
|
||||
if (code === 508) {
|
||||
log.info('server responded with 508. Giving up on this job');
|
||||
return;
|
||||
}
|
||||
|
||||
await sleepFor413RetryAfterTimeIfApplicable({ err, log, timeRemaining });
|
||||
|
||||
throw err;
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import { parseRetryAfter } from '../../util/parseRetryAfter';
|
|||
import { isRecord } from '../../util/isRecord';
|
||||
import { HTTPError } from '../../textsecure/Errors';
|
||||
|
||||
export async function sleepFor413RetryAfterTimeIfApplicable({
|
||||
export async function sleepFor413RetryAfterTime({
|
||||
err,
|
||||
log,
|
||||
timeRemaining,
|
||||
|
@ -16,17 +16,12 @@ export async function sleepFor413RetryAfterTimeIfApplicable({
|
|||
log: Pick<LoggerType, 'info'>;
|
||||
timeRemaining: number;
|
||||
}>): Promise<void> {
|
||||
if (
|
||||
timeRemaining <= 0 ||
|
||||
!(err instanceof HTTPError) ||
|
||||
err.code !== 413 ||
|
||||
!isRecord(err.responseHeaders)
|
||||
) {
|
||||
if (timeRemaining <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const retryAfter = Math.min(
|
||||
parseRetryAfter(err.responseHeaders['retry-after']),
|
||||
parseRetryAfter(findRetryAfterTime(err)),
|
||||
timeRemaining
|
||||
);
|
||||
|
||||
|
@ -36,3 +31,19 @@ export async function sleepFor413RetryAfterTimeIfApplicable({
|
|||
|
||||
await sleep(retryAfter);
|
||||
}
|
||||
|
||||
function findRetryAfterTime(err: unknown): unknown {
|
||||
if (!isRecord(err)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (isRecord(err.responseHeaders)) {
|
||||
return err.responseHeaders['retry-after'];
|
||||
}
|
||||
|
||||
if (err.httpError instanceof HTTPError) {
|
||||
return err.httpError.responseHeaders?.['retry-after'];
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
|
@ -7,7 +7,7 @@ import PQueue from 'p-queue';
|
|||
import type { LoggerType } from '../types/Logging';
|
||||
import { exponentialBackoffMaxAttempts } from '../util/exponentialBackoff';
|
||||
import { commonShouldJobContinue } from './helpers/commonShouldJobContinue';
|
||||
import { sleepFor413RetryAfterTimeIfApplicable } from './helpers/sleepFor413RetryAfterTimeIfApplicable';
|
||||
import { sleepFor413RetryAfterTime } from './helpers/sleepFor413RetryAfterTime';
|
||||
import type { MessageModel } from '../models/messages';
|
||||
import { getMessageById } from '../messages/getMessageById';
|
||||
import type { ConversationModel } from '../models/conversations';
|
||||
|
@ -20,10 +20,8 @@ import { getSendOptions } from '../util/getSendOptions';
|
|||
import { SignalService as Proto } from '../protobuf';
|
||||
import { handleMessageSend } from '../util/handleMessageSend';
|
||||
import type { CallbackResultType } from '../textsecure/Types.d';
|
||||
import { HTTPError } from '../textsecure/Errors';
|
||||
import { isSent } from '../messages/MessageSendState';
|
||||
import { getLastChallengeError, isOutgoing } from '../state/selectors/message';
|
||||
import { parseIntWithFallback } from '../util/parseIntWithFallback';
|
||||
import * as Errors from '../types/errors';
|
||||
import type {
|
||||
AttachmentType,
|
||||
|
@ -38,6 +36,7 @@ import type { ParsedJob } from './types';
|
|||
import { JobQueue } from './JobQueue';
|
||||
import { jobQueueDatabaseStore } from './JobQueueDatabaseStore';
|
||||
import { Job } from './Job';
|
||||
import { getHttpErrorCode } from './helpers/getHttpErrorCode';
|
||||
|
||||
const {
|
||||
loadAttachmentData,
|
||||
|
@ -338,15 +337,12 @@ export class NormalMessageSendJobQueue extends JobQueue<NormalMessageSendJobData
|
|||
} catch (err: unknown) {
|
||||
const formattedMessageSendErrors: Array<string> = [];
|
||||
let serverAskedUsToStop = false;
|
||||
let maybe413Error: undefined | Error;
|
||||
let retryAfterError: unknown;
|
||||
messageSendErrors.forEach((messageSendError: unknown) => {
|
||||
formattedMessageSendErrors.push(Errors.toLogFormat(messageSendError));
|
||||
if (!(messageSendError instanceof HTTPError)) {
|
||||
return;
|
||||
}
|
||||
switch (parseIntWithFallback(messageSendError.code, -1)) {
|
||||
switch (getHttpErrorCode(messageSendError)) {
|
||||
case 413:
|
||||
maybe413Error ||= messageSendError;
|
||||
retryAfterError ||= messageSendError;
|
||||
break;
|
||||
case 508:
|
||||
serverAskedUsToStop = true;
|
||||
|
@ -370,9 +366,9 @@ export class NormalMessageSendJobQueue extends JobQueue<NormalMessageSendJobData
|
|||
return;
|
||||
}
|
||||
|
||||
if (!isFinalAttempt) {
|
||||
await sleepFor413RetryAfterTimeIfApplicable({
|
||||
err: maybe413Error,
|
||||
if (!isFinalAttempt && retryAfterError) {
|
||||
await sleepFor413RetryAfterTime({
|
||||
err: retryAfterError,
|
||||
log,
|
||||
timeRemaining,
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue