2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2019 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
import PQueue from 'p-queue';
|
|
|
|
|
2020-10-13 22:21:42 +00:00
|
|
|
import { sleep } from './sleep';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2022-04-11 17:53:57 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2022-02-25 18:37:15 +00:00
|
|
|
import { clearTimeoutIfNecessary } from './clearTimeoutIfNecessary';
|
2022-06-27 16:46:43 +00:00
|
|
|
import { MINUTE } from './durations';
|
2022-12-21 18:41:48 +00:00
|
|
|
import { drop } from './drop';
|
2020-10-13 22:21:42 +00:00
|
|
|
|
2020-09-14 21:56:35 +00:00
|
|
|
declare global {
|
2021-01-14 18:07:05 +00:00
|
|
|
// We want to extend `window`'s properties, so we need an interface.
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2020-09-14 21:56:35 +00:00
|
|
|
interface Window {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
batchers: Array<BatcherType<any>>;
|
|
|
|
waitForAllBatchers: () => Promise<unknown>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
window.batchers = [];
|
|
|
|
|
|
|
|
window.waitForAllBatchers = async () => {
|
2022-04-11 17:53:57 +00:00
|
|
|
log.info('batcher#waitForAllBatchers');
|
|
|
|
try {
|
|
|
|
await Promise.all(window.batchers.map(item => item.flushAndWait()));
|
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
'waitForAllBatchers: error flushing all',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
2019-09-26 19:56:31 +00:00
|
|
|
};
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
export type BatcherOptionsType<ItemType> = {
|
2021-03-26 00:00:03 +00:00
|
|
|
name: string;
|
2019-09-26 19:56:31 +00:00
|
|
|
wait: number;
|
|
|
|
maxSize: number;
|
2021-01-27 21:13:33 +00:00
|
|
|
processBatch: (items: Array<ItemType>) => void | Promise<void>;
|
2019-09-26 19:56:31 +00:00
|
|
|
};
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
export type BatcherType<ItemType> = {
|
2019-09-26 19:56:31 +00:00
|
|
|
add: (item: ItemType) => void;
|
2021-11-01 23:38:08 +00:00
|
|
|
removeAll: (needle: ItemType) => void;
|
2019-09-26 19:56:31 +00:00
|
|
|
anyPending: () => boolean;
|
|
|
|
onIdle: () => Promise<void>;
|
2019-10-25 18:48:28 +00:00
|
|
|
flushAndWait: () => Promise<void>;
|
2019-09-26 19:56:31 +00:00
|
|
|
unregister: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function createBatcher<ItemType>(
|
|
|
|
options: BatcherOptionsType<ItemType>
|
|
|
|
): BatcherType<ItemType> {
|
|
|
|
let batcher: BatcherType<ItemType>;
|
2020-09-14 21:56:35 +00:00
|
|
|
let timeout: NodeJS.Timeout | null;
|
2019-09-26 19:56:31 +00:00
|
|
|
let items: Array<ItemType> = [];
|
2021-11-23 22:01:03 +00:00
|
|
|
const queue = new PQueue({
|
|
|
|
concurrency: 1,
|
2022-06-27 16:46:43 +00:00
|
|
|
timeout: MINUTE * 30,
|
2021-11-23 22:01:03 +00:00
|
|
|
throwOnTimeout: true,
|
|
|
|
});
|
2019-09-26 19:56:31 +00:00
|
|
|
|
|
|
|
function _kickBatchOff() {
|
2022-02-25 18:37:15 +00:00
|
|
|
clearTimeoutIfNecessary(timeout);
|
|
|
|
timeout = null;
|
2021-06-25 18:25:50 +00:00
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
const itemsRef = items;
|
|
|
|
items = [];
|
2022-12-21 18:41:48 +00:00
|
|
|
drop(
|
|
|
|
queue.add(async () => {
|
|
|
|
await options.processBatch(itemsRef);
|
|
|
|
})
|
|
|
|
);
|
2019-09-26 19:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function add(item: ItemType) {
|
|
|
|
items.push(item);
|
|
|
|
|
2021-03-26 00:00:03 +00:00
|
|
|
if (items.length === 1) {
|
|
|
|
// Set timeout once when we just pushed the first item so that the wait
|
|
|
|
// time is bounded by `options.wait` and not extended by further pushes.
|
2021-06-25 18:25:50 +00:00
|
|
|
timeout = setTimeout(_kickBatchOff, options.wait);
|
2021-03-26 00:00:03 +00:00
|
|
|
} else if (items.length >= options.maxSize) {
|
|
|
|
_kickBatchOff();
|
2019-09-26 19:56:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 23:38:08 +00:00
|
|
|
function removeAll(needle: ItemType) {
|
|
|
|
items = items.filter(item => item !== needle);
|
|
|
|
}
|
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
function anyPending(): boolean {
|
|
|
|
return queue.size > 0 || queue.pending > 0 || items.length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function onIdle() {
|
|
|
|
while (anyPending()) {
|
|
|
|
if (queue.size > 0 || queue.pending > 0) {
|
2020-09-14 21:56:35 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2019-09-26 19:56:31 +00:00
|
|
|
await queue.onIdle();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (items.length > 0) {
|
2020-09-14 21:56:35 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2019-09-26 19:56:31 +00:00
|
|
|
await sleep(options.wait * 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function unregister() {
|
2020-09-14 21:56:35 +00:00
|
|
|
window.batchers = window.batchers.filter(item => item !== batcher);
|
2019-09-26 19:56:31 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 18:48:28 +00:00
|
|
|
async function flushAndWait() {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`Flushing ${options.name} batcher items.length=${items.length}`);
|
2021-03-26 00:00:03 +00:00
|
|
|
|
|
|
|
while (anyPending()) {
|
2019-10-25 18:48:28 +00:00
|
|
|
_kickBatchOff();
|
|
|
|
|
2021-03-26 00:00:03 +00:00
|
|
|
if (queue.size > 0 || queue.pending > 0) {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await queue.onIdle();
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`Flushing complete ${options.name} for batcher`);
|
2019-10-25 18:48:28 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
batcher = {
|
|
|
|
add,
|
2021-11-01 23:38:08 +00:00
|
|
|
removeAll,
|
2019-09-26 19:56:31 +00:00
|
|
|
anyPending,
|
|
|
|
onIdle,
|
2019-10-25 18:48:28 +00:00
|
|
|
flushAndWait,
|
2019-09-26 19:56:31 +00:00
|
|
|
unregister,
|
|
|
|
};
|
|
|
|
|
|
|
|
window.batchers.push(batcher);
|
|
|
|
|
|
|
|
return batcher;
|
|
|
|
}
|