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(name: string, limitPerSec = -1): Bull.Queue { return new Bull(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; }