2021-05-08 04:56:21 -05:00
|
|
|
import * as Bull from 'bull';
|
2021-08-19 07:55:45 -05:00
|
|
|
import config from '@/config/index';
|
2021-03-17 21:17:05 -05:00
|
|
|
|
2021-05-08 04:56:21 -05:00
|
|
|
export function initialize<T>(name: string, limitPerSec = -1) {
|
|
|
|
return new Bull<T>(name, {
|
2021-03-17 21:17:05 -05:00
|
|
|
redis: {
|
|
|
|
port: config.redis.port,
|
|
|
|
host: config.redis.host,
|
|
|
|
password: config.redis.pass,
|
|
|
|
db: config.redis.db || 0,
|
|
|
|
},
|
|
|
|
prefix: config.redis.prefix ? `${config.redis.prefix}:queue` : 'queue',
|
|
|
|
limiter: limitPerSec > 0 ? {
|
2021-08-20 07:59:03 -05:00
|
|
|
max: limitPerSec,
|
2021-12-09 08:58:30 -06:00
|
|
|
duration: 1000,
|
2021-08-20 07:59:03 -05:00
|
|
|
} : undefined,
|
|
|
|
settings: {
|
|
|
|
backoffStrategies: {
|
2021-12-09 08:58:30 -06:00
|
|
|
apBackoff,
|
|
|
|
},
|
|
|
|
},
|
2021-03-17 21:17:05 -05:00
|
|
|
});
|
|
|
|
}
|
2021-08-20 07:59:03 -05:00
|
|
|
|
|
|
|
// ref. https://github.com/misskey-dev/misskey/pull/7635#issue-971097019
|
|
|
|
function apBackoff(attemptsMade: number, err: Error) {
|
|
|
|
const baseDelay = 60 * 1000; // 1min
|
|
|
|
const maxBackoff = 8 * 60 * 60 * 1000; // 8hours
|
|
|
|
let backoff = (Math.pow(2, attemptsMade) - 1) * baseDelay;
|
|
|
|
backoff = Math.min(backoff, maxBackoff);
|
|
|
|
backoff += Math.round(backoff * Math.random() * 0.2);
|
|
|
|
return backoff;
|
|
|
|
}
|