9.2.1 • Published 2 days ago

@octokit/plugin-throttling v9.2.1

Weekly downloads
39,368
License
MIT
Repository
github
Last release
2 days ago

plugin-throttling.js

Octokit plugin for GitHub’s recommended request throttling

@latest Build Status

Implements all recommended best practices to prevent hitting secondary rate limits.

Usage

Load @octokit/plugin-throttling and @octokit/core (or core-compatible module) directly from esm.sh

<script type="module">
  import { Octokit } from "https://esm.sh/@octokit/core";
  import { throttling } from "https://esm.sh/@octokit/plugin-throttling";
</script>

Install with npm install @octokit/core @octokit/plugin-throttling. Optionally replace @octokit/core with a core-compatible module.

Note: If you use it with @octokit/rest v16, install @octokit/core as a devDependency. This is only temporary and will no longer be necessary with @octokit/rest v17.

import { Octokit } from "@octokit/core";
import { throttling } from "@octokit/plugin-throttling";

!IMPORTANT As we use conditional exports, you will need to adapt your tsconfig.json by setting "moduleResolution": "node16", "module": "node16".

See the TypeScript docs on package.json "exports". See this helpful guide on transitioning to ESM from @sindresorhus

The code below creates a "Hello, world!" issue on every repository in a given organization. Without the throttling plugin it would send many requests in parallel and would hit rate limits very quickly. But the @octokit/plugin-throttling slows down your requests according to the official guidelines, so you don't get blocked before your quota is exhausted.

The throttle.onSecondaryRateLimit and throttle.onRateLimit options are required. Return true to automatically retry the request after retryAfter seconds.

const MyOctokit = Octokit.plugin(throttling);

const octokit = new MyOctokit({
  auth: `secret123`,
  throttle: {
    onRateLimit: (retryAfter, options, octokit, retryCount) => {
      octokit.log.warn(
        `Request quota exhausted for request ${options.method} ${options.url}`,
      );

      if (retryCount < 1) {
        // only retries once
        octokit.log.info(`Retrying after ${retryAfter} seconds!`);
        return true;
      }
    },
    onSecondaryRateLimit: (retryAfter, options, octokit) => {
      // does not retry, only logs a warning
      octokit.log.warn(
        `SecondaryRateLimit detected for request ${options.method} ${options.url}`,
      );
    },
  },
});

async function createIssueOnAllRepos(org) {
  const repos = await octokit.paginate(
    octokit.repos.listForOrg.endpoint({ org }),
  );
  return Promise.all(
    repos.map(({ name }) =>
      octokit.issues.create({
        owner,
        repo: name,
        title: "Hello, world!",
      }),
    ),
  );
}

Pass { throttle: { enabled: false } } to disable this plugin.

Clustering

Enabling Clustering support ensures that your application will not go over rate limits across Octokit instances and across Nodejs processes.

First install either redis or ioredis:

# NodeRedis (https://github.com/NodeRedis/node_redis)
npm install --save redis

# or ioredis (https://github.com/luin/ioredis)
npm install --save ioredis

Then in your application:

import Bottleneck from "bottleneck";
import Redis from "redis";

const client = Redis.createClient({
  /* options */
});
const connection = new Bottleneck.RedisConnection({ client });
connection.on("error", err => console.error(err));

const octokit = new MyOctokit({
  auth: 'secret123'
  throttle: {
    onSecondaryRateLimit: (retryAfter, options, octokit) => {
      /* ... */
    },
    onRateLimit: (retryAfter, options, octokit) => {
      /* ... */
    },

    // The Bottleneck connection object
    connection,

    // A "throttling ID". All octokit instances with the same ID
    // using the same Redis server will share the throttling.
    id: "my-super-app",

    // Otherwise the plugin uses a lighter version of Bottleneck without Redis support
    Bottleneck
  }
});

// To close the connection and allow your application to exit cleanly:
await connection.disconnect();

To use the ioredis library instead:

import Redis from "ioredis";
const client = new Redis({
  /* options */
});
const connection = new Bottleneck.IORedisConnection({ client });
connection.on("error", (err) => console.error(err));

Options

LICENSE

MIT

@balena/jellyfish-plugin-github@code.gov/code-gov-integrations@everything-registry/sub-chunk-676ebieslint-suggestion-action@octoherd/octokitoctokitoctogit@semantic-release-extras/github-comment-specific@semantic-release-extras/verified-git-commit@semantic-release/githubpackage-adoption@probot-ng/coreupload_github_release_assets_actionvstest-oct1norinode-package-release-actionunion-github-crawlerupdate-my-badgesupdate-existing-release@skynet1024/probot@release-drafter/core@salesforce/plugin-release-management@pkgjs/statusboard@stead/clipr-release@swsdk/github@signalk/github-create-releasewhat-broke@stoe/action-permissions-cli@stoe/action-reporting-cli@stoe/action-uses-cliprobotprobot-app-monorepo-syncghgh-org-scangithub-default-branchgithub-csv-toolsgithub-bulk-unsubscribergather-pull-request-metadatahexo-generator-issuesgenerator-easy-ui5github_exporterghost-storage-githubghost-storage-github-forkgithub-sync-labels-milestonesgithub-release-writergood-samaritanmutate-github-repositories-climaintservaccept-to-ship-action@atomist/aspect-sdm@atomist/samples@broadshield/github-actions-octokit-hydrated@broadshield/github-actions-workflow-marie-kondo@auto-it/core@auto-canary/core@binarymuse/probot@zephraph/services@achingbrain/semantic-release-github@unitednations/clipurescript-spaghetto@un/cli@carbon/cliremove-github-forksasari@chancethedev/cli@denizhoxha/probot@cloudnativetoolkit/git-client@artsy/dupe-reportaction-uses-cli@wfp/cli0z_exportbitbucket-github-migrator@jessitron/reporank@johnlindquist/kitsupersetbot@atomist/sdm-pack-lifecycle@atomist/sdm-pack-lifecycle-github@atomist/sdm-pack-rcca-github@bolt/build-tools@jupiterone/github-repo-standard@jupiterone/graph-github@hackney/mtfh-cli@hickorytechnology/semantic-release-githubsilverstripe-cms-dashboard@fdm-monster/servercodeboostcontribution-streak-maintainersemantic-release-github-milestones@gr2m/semantic-release-github-487bas-app-generatorscriptkit-octokit@gitops-toolbox/github-toolscherpcerebro-clisimple-octokit@elastic/snyk-github-issue-creator@dvcorg/cml@eng-automation/integrations
9.2.1

2 days ago

9.2.0

10 days ago

9.0.4

22 days ago

9.1.0

22 days ago

9.0.3

2 months ago

9.0.2

2 months ago

8.2.0

2 months ago

9.0.1

2 months ago

9.0.0

2 months ago

8.1.0

6 months ago

8.1.2

6 months ago

8.1.1

6 months ago

8.1.3

5 months ago

7.0.0

10 months ago

8.0.1

6 months ago

8.0.0

7 months ago

6.1.0

11 months ago

5.2.3

11 months ago

5.2.2

11 months ago

5.2.1

12 months ago

5.2.0

12 months ago

6.0.1

11 months ago

6.0.0

11 months ago

5.1.1

1 year ago

5.1.0

1 year ago

5.0.1

1 year ago

5.0.0

1 year ago

4.3.2

1 year ago

4.3.1

2 years ago

4.3.0

2 years ago

3.7.0

2 years ago

4.1.0

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.2.0

2 years ago

4.1.1

2 years ago

3.6.2

2 years ago

3.6.1

2 years ago

3.6.0

2 years ago

3.5.2

3 years ago

3.5.1

3 years ago

3.4.3

3 years ago

3.4.2

3 years ago

3.5.0

3 years ago

3.4.1

3 years ago

3.4.0

3 years ago

3.3.4

3 years ago

3.3.3

3 years ago

3.3.2

4 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.2

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.0

4 years ago

3.0.0

4 years ago

2.7.1

4 years ago

2.7.0

5 years ago

2.6.0

5 years ago

2.5.0

5 years ago

2.4.0

5 years ago

2.3.0

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.0.0

5 years ago