probot
Version:
A framework for building GitHub Apps to automate and improve your workflow
46 lines (45 loc) • 1.63 kB
JavaScript
import { npxImport } from "npx-import-light";
import Bottleneck from "bottleneck";
export async function getOctokitThrottleOptions(options) {
const { log, redisConfig } = options;
const throttlingOptions = {
onRateLimit: (retryAfter, options) => {
log.warn(`Request quota exhausted for request ${options.method} ${options.url}`);
// Retry twice after hitting a rate limit error, then give up
if (options.request.retryCount <= 2) {
log.info(`Retrying after ${retryAfter} seconds!`);
return true;
}
return false;
},
onSecondaryRateLimit: (_retryAfter, options) => {
// does not retry, only logs a warning
log.warn(`Secondary quota detected for request ${options.method} ${options.url}`);
},
};
if (!redisConfig)
return throttlingOptions;
const { client, Redis } = await getRedisClient(redisConfig);
const connection = new Bottleneck.IORedisConnection({
client,
Redis,
});
connection.on("error", (error) => {
log.error(Object.assign(error, { source: "bottleneck" }));
});
throttlingOptions.Bottleneck = Bottleneck;
throttlingOptions.connection = connection;
return throttlingOptions;
}
let Redis;
async function getRedisClient(redisConfig) {
Redis ??= (await npxImport("ioredis@5.10.1", {
onlyPackageRunner: true,
})).Redis;
return {
client: typeof redisConfig === "string"
? new Redis(redisConfig)
: new Redis(redisConfig),
Redis,
};
}