Communitygithub.com

YepAPI/skills

令牌桶、滑动窗口、基于用户/IP限制、Redis 后端、Upstash 模式。

skills 是什么?

该技能提供了全面的速率限制指导,涵盖令牌桶、滑动窗口等经典算法,并针对现代云原生和 Serverless 架构进行了优化。它详细说明了如何基于 API 密钥或 IP 地址识别调用者、设置合理的默认限制(如普通API 100 req/min、认证 5 req/min)、以及统一使用 Redis(或 Upstash)作为原子性计数存储,避免多实例部署下状态丢失的问题。技能强烈建议将速率限制作为全局中间件实现,并支持按路由或用户层级(免费、专业、企业)覆盖限制;对于 Next.js,限制逻辑应置于 middleware.ts 或 API 路由处理器中。此外,它强调记录限制命中日志以检测滥用模式,并提醒开发者不要忽略 Webhook 和公开表单提交的防护。支持的平台包括 Node.js、Next.js、Cloudflare Workers、Vercel Edge Functions 等 Serverless 环境。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/rate-limiting

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

速率限制

规则

  • 对 API 端点使用令牌桶,对认证/登录路由使用滑动窗口
  • 优先使用 API 密钥识别调用者,回退到 IP —— 对于已认证路由绝不只依赖 IP
  • 返回 429 Too Many Requests,附带 Retry-After 头(秒)和 X-RateLimit-Remaining 头
  • 在 Redis(或 Serverless 下的 Upstash)中存储计数器 —— 多实例部署中切勿使用内存存储
  • 设置合理的默认值:通用 API 100 req/min,认证路由 5 req/min,只读路由 1000 req/min
  • Serverless 环境使用 @upstash/ratelimit —— 它原生处理 Redis 原子性和滑动窗口
  • 将速率限制实现为中间件 —— 全局应用,按路由使用更严格的限制覆盖
  • 对于 Next.js:在 middleware.ts 或 API 路由处理程序中进行速率限制,不要在 React 组件中执行
  • 按层级(免费、专业、企业)区分限制 —— 将层级存储在 JWT 或数据库查找中
  • 记录包含调用者身份的速率限制命中日志 —— 及早检测滥用模式

模式

// Upstash Serverless 模式
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";

const ratelimit = new Ratelimit({
  redis: Redis.fromEnv(),
  limiter: Ratelimit.slidingWindow(10, "10 s"),
  analytics: true,
});

const { success, limit, remaining, reset } = await ratelimit.limit(identifier);

避免

  • 在 Serverless 或多实例设置中使用内存速率限制 —— 状态在调用之间会丢失
  • 仅限应用层速率限制 —— 同时添加 CDN/边缘限制(Cloudflare、Vercel)
  • 使用过于激进的限制阻止合法用户 —— 从宽松开始,根据数据收紧
  • 忘记对 Webhook 和公开表单提交进行速率限制

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相关技能