UNPKG

probot

Version:

A framework for building GitHub Apps to automate and improve your workflow

39 lines (38 loc) 1.49 kB
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 connection = new Bottleneck.IORedisConnection({ client: await getRedisClient(options), }); connection.on("error", (error) => { log.error(Object.assign(error, { source: "bottleneck" })); }); throttlingOptions.Bottleneck = Bottleneck; throttlingOptions.connection = connection; return throttlingOptions; } let Redis = null; async function getRedisClient({ redisConfig }) { Redis ??= (await npxImport("ioredis@5.8.1", { onlyPackageRunner: true })) .Redis; if (redisConfig) return new Redis(redisConfig); }