mirror of
https://iceshrimp.dev/crimekillz/iceshrimp-161sh.git
synced 2024-11-24 21:19:07 +01:00
40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import Bull from "bull";
|
|
import config from "@/config/index.js";
|
|
|
|
export function initialize<T>(name: string, limitPerSec = -1) {
|
|
return new Bull<T>(name, {
|
|
redis: {
|
|
port: config.redis.port,
|
|
host: config.redis.host,
|
|
family: config.redis.family == null ? 0 : config.redis.family,
|
|
password: config.redis.pass,
|
|
db: config.redis.db || 0,
|
|
},
|
|
prefix: config.redis.prefix ? `${config.redis.prefix}:queue` : "queue",
|
|
limiter:
|
|
limitPerSec > 0
|
|
? {
|
|
max: limitPerSec,
|
|
duration: 1000,
|
|
}
|
|
: undefined,
|
|
settings: {
|
|
stalledInterval: 60,
|
|
maxStalledCount: 2,
|
|
backoffStrategies: {
|
|
apBackoff,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
// 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;
|
|
}
|