FoundKey/packages/backend/src/queue/initialize.ts
Johann150 2218936af3
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint-sw Pipeline failed
ci/woodpecker/push/lint-foundkey-js Pipeline was successful
ci/woodpecker/push/lint-client Pipeline failed
ci/woodpecker/push/lint-backend Pipeline failed
ci/woodpecker/push/test Pipeline failed
server: better performace for mass delivery
This should hopefully relieve some of the massive hammering on the
database when mass delivery jobs are running.

However, this also means that instance blocks are applied with a
slight delay on delivery queue. Since the settings page in the
native frontend already warns about this, I think it should be fine.
And the maximum time an instance block would be delayed would be
somewhere around 5min which IMO is also tolerable.

Changelog: Changed
2024-03-30 16:41:55 +01:00

44 lines
1.1 KiB
TypeScript

import Bull from 'bull';
import { SECOND, MINUTE, HOUR } from '@/const.js';
import config from '@/config/index.js';
import { getRedisOptions } from '@/config/redis.js';
export function initialize<T>(name: string, limitPerSec = -1): Bull.Queue<T> {
return new Bull<T>(name, {
redis: getRedisOptions(),
prefix: config.redis.prefix ? `${config.redis.prefix}:queue` : 'queue',
limiter: limitPerSec > 0 ? {
max: limitPerSec,
duration: SECOND,
} : undefined,
settings: {
backoffStrategies: {
apBackoff,
},
},
});
}
// ref. https://github.com/misskey-dev/misskey/pull/7635#issue-971097019
function apBackoff(attemptsMade: number /*, err: Error */): number {
const baseDelay = MINUTE;
const maxBackoff = 8 * HOUR;
/*
attempt | average seconds + up to 2% random offset
0 | 0
1 | 60 = 1min
2 | 180 = 3min
3 | 420 = 7min
4 | 900 = 15min
5 | 1860 = 31min
6 | 3780 = 63min
7 | 7620 = 127min ~= 2.1h
8 | 15300 = 4.25h
>8 | 28800 = 8h
*/
let backoff = (Math.pow(2, attemptsMade) - 1) * baseDelay;
backoff = Math.min(backoff, maxBackoff);
backoff += Math.round(backoff * Math.random() * 0.2);
return backoff;
}