@convex-dev/ratelimiter

中文文档
0.1.7发布于 2024/10/17Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 4805

A rate limiter component for Convex. Define and use application-layer rate limits. Type-safe, transactional, fair, safe, and configurable sharding to scale.

此软件包已被标记为弃用

Package has been renamed to @convex-dev/rate-limiter

原始使用文档
文档目录3 节
  1. Pre-requisite: Convex
  2. Installation
  3. Usage

Convex Rate Limiter Component

npm version

Note: Convex Components are currently in beta

Application-level rate limiting.

  • Type-safe
  • Configurable for fixed window or token bucket algorithms
  • Efficient storage and compute
  • Configurable sharding for scalability
  • Transactional evaluation
  • Fairness guarantees via credit "reservation"
  • Opt-in "rollover" or "burst" allowance via "token bucket" config
  • Fails closed, not open

Definition:

const rateLimiter = new RateLimiter(components.ratelimiter, {
  // One global / singleton rate limit, using a "fixed window" algorithm.
  freeTrialSignUp: { kind: "fixed window", rate: 100, period: HOUR },
  // A per-user limit, allowing one every ~6 seconds.
  // Allows up to 3 in quick succession if they haven't sent many recently.
  sendMessage: { kind: "token bucket", rate: 10, period: MINUTE, capacity: 3 },
  failedLogins: { kind: "token bucket", rate: 10, period: HOUR },
  // Use sharding to increase throughput without compromising on correctness.
  llmTokens: { kind: "token bucket", rate: 40000, period: MINUTE, shards: 100 },
  llmRequests: { kind: "fixed window", rate: 3, period: MINUTE },
});

Usage:

// Restrict how fast free users can sign up to deter bots
const status = await rateLimiter.limit(ctx, "freeTrialSignUp");

// Limit how fast a user can send messages
const status = await rateLimiter.limit(ctx, "sendMessage", { key: userId });

// Automatically throw an error if the rate limit is hit
await rateLimiter.limit(ctx, "failedLogins", { key: userId, throws: true });

// Consume multiple in one request to prevent rate limits on an LLM API.
const status = await rateLimiter.limit(ctx, "llmTokens", { count: tokens });

// Reserve future capacity instead of just failing now
if (!args.skipCheck) {
  const status = await rateLimiter.limit(ctx, "llmRequests", { reserve: true });
  if (status.retryAfter) {
    return ctx.scheduler.runAfter(status.retryAfter, internal.foo.bar, {
      skipCheck: true,
    });
  }
}

// Check a rate limit without consuming it
const status = await rateLimiter.check(ctx, "failedLogins", { key: userId });

// Reset a rate limit on successful login
await rateLimiter.reset(ctx, "failedLogins", { key: userId });

// Use a one-off rate limit config (when not named on initialization)
const config = { kind: "fixed window", rate: 1, period: SECOND };
await rateLimiter.limit(ctx, "oneOffName", { config, throws: true });

See this article for more information.

Pre-requisite: Convex

You'll need an existing Convex project to use the component. Convex is a hosted backend platform, including a database, serverless functions, and a ton more you can learn about here.

Run npm create convex or follow any of the quickstarts to set one up.

Installation

Install the component package:

npm install @convex-dev/ratelimiter

Create a convex.config.ts file in your app's convex/ folder and install the component by calling use:

// convex/convex.config.ts
import { defineApp } from "convex/server";
import ratelimiter from "@convex-dev/ratelimiter/convex.config";

const app = defineApp();
app.use(ratelimiter);

export default app;

Usage

Define your rate limits:

import { RateLimiter } from "@convex-dev/ratelimiter";
import { components } from "./_generated/api";

const rateLimiter = new RateLimiter(components.ratelimiter, {
  // One global / singleton rate limit
  freeTrialSignUp: { kind: "fixed window", rate: 100, period: HOUR },
  sendMessage: { kind: "token bucket", rate: 10, period: MINUTE, capacity: 3 },
});
  • You can safely generate multiple instances if you want to define different rates in separate places.

Use it from a mutation or action:

const { ok, retryAfter } = await rateLimiter.limit(ctx, "freeTrialSignUp");

Or if you want to rate limit based on a key:

await rateLimiter.limit(ctx, "sendMessage", { key: user._id, throws: true });

This call also throws an exception, so you don't have to check the return value.

Check out a full example here.

See this article for more information on usage and advanced patterns.

版本与发布记录

@convex-dev/ratelimiter 共发布 8 个版本,首次发布于 2024/09/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.7latest2024/10/172 年前
0.1.62024/10/092 年前
0.1.52024/09/272 年前
0.1.42024/09/262 年前
0.1.32024/09/262 年前
0.1.22024/09/212 年前

关于 @convex-dev/ratelimiter 的常见问题

@convex-dev/ratelimiter 是什么?

A rate limiter component for Convex. Define and use application-layer rate limits. Type-safe, transactional, fair, safe, and configurable sharding to scale.注意:该包已被作者标记为弃用——Package has been renamed to @convex-dev/rate-limiter

如何安装 @convex-dev/ratelimiter?

在项目目录运行 npm install @convex-dev/ratelimiter,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:convex(~1.16.5 || ~1.17.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@convex-dev/ratelimiter 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@convex-dev/ratelimiter 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@convex-dev/ratelimiter 可以商用吗?

@convex-dev/ratelimiter 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@convex-dev/ratelimiter 的下载量有多少?

根据 npm 官方下载统计,@convex-dev/ratelimiter 在 2026/10/11 – 2026/11/09 这 30 天内被下载 4,805 次,日均约 160 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@convex-dev/ratelimiter 还在维护吗?

最新版本 0.1.7 发布于 2024/10/17(2 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/09/21。作者已将其标记为弃用,新项目建议寻找替代方案。