No description
  • TypeScript 69.2%
  • JavaScript 30.8%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Benjamin Chrobot b56bd0689d
docs: fix global bucket example name (#7)
Reviewed-by: @ajohn25 
Reviewed-by: @hiemanshu
2023-08-01 06:23:59 -04:00
.github/workflows ci: add tests (#2) 2023-07-29 09:38:49 -04:00
perfTest style: fix styling 2022-08-08 16:05:44 -04:00
src ci: add tests (#2) 2023-07-29 09:38:49 -04:00
.eslintignore style: fix styling 2022-08-08 16:05:44 -04:00
.eslintrc.js style: fix styling 2022-08-08 16:05:44 -04:00
.gitignore chore: expand gitignore 2021-10-19 07:25:26 -04:00
.prettierrc.js style: fix styling 2022-08-08 16:05:44 -04:00
.versionrc.js chore: add standard-version 2022-08-08 15:51:44 -04:00
CHANGELOG.md chore(release): 1.0.3 2023-07-29 09:41:23 -04:00
jest.config.js ci: add tests (#2) 2023-07-29 09:38:49 -04:00
package.json build: ignore unnecessary files 2023-07-29 09:52:39 -04:00
README.md docs: fix global bucket example name (#7) 2023-08-01 06:23:59 -04:00
tsconfig.json build: skip building tests 2023-07-29 09:58:03 -04:00
yarn.lock ci: add tests (#2) 2023-07-29 09:38:49 -04:00

Graphile Worker Rate Limiter

A set of utilities to add rate limiting to graphile-worker. For context, see this issue.

Usage

Although the interfaces exist to support other rate limiters, right now a Redis based implementation of the Leaky Bucket algorithm is provided.

import { run } from 'graphile-worker';
import * as Redis from 'ioredis';
import { getLeakyBucketRateLimiter } from 'graphile-worker-rate-limiter';

const redis = new Redis();
const rateLimiter = getLeakyBucketRateLimiter({
  redis,
  bucketTypes: {
    'send-message-global': {
      capacity: 3000,
      drainInterval: 30 * 1000,
      drainCount: 1500,
    },
    'send-message-client': {
      capacity: 1000,
      drainInterval: 15 * 1000,
      drainCount: 500,
    },
    'send-message-phone': {
      capacity: 6,
      drainInterval: 500,
      drainCount: 3,
    },
  },
});

const worker = await run({
  // graphile worker options as normal, except...
  forbiddenFlags: rateLimiter.getForbiddenFlags,
  taskList: {
    'send-message': rateLimiter.wrapTask(sendMessage),
  },
});

Now, you can do:

await worker.addJob('send-message', payload, {
  flags: [
    'send-message-global:global',
    'send-message-client:b412d632-e004-11ea-87d0-0242ac130003',
    'send-message-phone:+12025550320',
  ],
});

Or:

perform graphile_worker.add_job(
  'send-message',
  payload,
  flags => ARRAY[
    'send-message-global:global',
    'send-message-client:b412d632-e004-11ea-87d0-0242ac130003',
    'send-message-phone:+12025550320'
  ]
);

And graphile-worker-rate-limiter will ensure your jobs do not run more often than allowed by your bucket specification.