Enable more specific AttachmentDownload prioritization

This commit is contained in:
trevor-signal 2024-04-15 20:11:48 -04:00 committed by GitHub
parent 87ea909ae9
commit fc02762588
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 2245 additions and 817 deletions

View file

@ -5,6 +5,7 @@ import * as durations from './durations';
const BACKOFF_FACTOR = 1.9;
const MAX_BACKOFF = 15 * durations.MINUTE;
const FIRST_BACKOFF = 100 * BACKOFF_FACTOR;
/**
* For a given attempt, how long should we sleep (in milliseconds)?
@ -16,12 +17,29 @@ const MAX_BACKOFF = 15 * durations.MINUTE;
*
* [0]: https://github.com/signalapp/Signal-iOS/blob/6069741602421744edfb59923d2fb3a66b1b23c1/SignalServiceKit/src/Util/OWSOperation.swift
*/
export function exponentialBackoffSleepTime(attempt: number): number {
const failureCount = attempt - 1;
if (failureCount === 0) {
export type ExponentialBackoffOptionsType = {
maxBackoffTime: number;
multiplier: number;
firstBackoffTime: number;
};
export function exponentialBackoffSleepTime(
attempt: number,
options: ExponentialBackoffOptionsType = {
maxBackoffTime: MAX_BACKOFF,
multiplier: BACKOFF_FACTOR,
firstBackoffTime: FIRST_BACKOFF,
}
): number {
if (attempt === 1) {
return 0;
}
return Math.min(MAX_BACKOFF, 100 * BACKOFF_FACTOR ** failureCount);
return Math.min(
options.maxBackoffTime,
(options.firstBackoffTime / options.multiplier) *
options.multiplier ** (attempt - 1)
);
}
/**
@ -31,7 +49,8 @@ export function exponentialBackoffSleepTime(attempt: number): number {
* `desiredDurationMs` should be at least 1.
*/
export function exponentialBackoffMaxAttempts(
desiredDurationMs: number
desiredDurationMs: number,
options?: ExponentialBackoffOptionsType
): number {
let attempts = 0;
let total = 0;
@ -39,7 +58,7 @@ export function exponentialBackoffMaxAttempts(
// fast even for giant numbers, and is typically called just once at startup.
do {
attempts += 1;
total += exponentialBackoffSleepTime(attempts);
total += exponentialBackoffSleepTime(attempts, options);
} while (total < desiredDurationMs);
return attempts;
}