Communitygithub.com

YepAPI/skills

Token bucket、滑動視窗、每位使用者/IP 的限制、Redis 支援、Upstash 模式。

skills 是什麼?

此技能提供一套完整且可實作的速率限制策略,專為使用 Redis 或 Upstash 的現代化應用程式所設計。它涵蓋了多種演算法,例如適用於 API 端點的 token bucket(權杖桶)和適用於驗證路由的 sliding window(滑動視窗),確保能針對不同情境採用最合適的限制機制。該技能強調跨多個執行個體進行分散式狀態管理,避免在無伺服器 (serverless) 或多實例部署中使用記憶體內的解決方案。它提供了將速率限制作為中介軟體實施的清晰指引,可全域應用,並允許針對特定路由或使用者層級(例如免費、專業、企業方案)進行覆寫設定。此處定義的模式展示了如何使用 `@upstash/ratelimit` 搭配 `Redis.fromEnv()` 來進行無伺服器設定,並啟用分析功能以監控使用情況。此外,該技能還強調了正確傳遞 `429` 狀態碼與 `Retry-After` 及 `X-RateLimit-Remaining` 標頭的重要性,有助於建立用戶端與伺服器之間的穩健通訊協定。最後,它提供了應避免的常見陷阱,例如使用過於嚴格的限制阻擋合法使用者,或未在 CDN/邊緣 (edge) 層級實施速率限制。

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

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

速率限制

規則

  • 對 API 端點使用 token bucket(權杖桶),對驗證/登入路由使用 sliding window(滑動視窗)
  • 優先透過 API 金鑰識別呼叫者,其次才是 IP——對於已驗證的路由,絕不能僅依賴 IP
  • 回傳 429 Too Many Requests 狀態碼,並附帶 Retry-After 標頭(秒數)以及 X-RateLimit-Remaining 標頭
  • 將計數器儲存在 Redis(或用於無伺服器的 Upstash)中——在多實例部署中絕對不要使用記憶體內儲存
  • 設定合理的預設值:一般 API 為 100 次請求/分鐘,驗證路由為 5 次請求/分鐘,讀取操作為 1000 次請求/分鐘
  • 對於無伺服器環境,使用 @upstash/ratelimit——它原生處理 Redis 的原子性操作與滑動視窗邏輯
  • 以中介軟體的形式實作速率限制——全域套用,並可針對個別路由覆寫為更嚴格的限制
  • 對於 Next.js:在 middleware.ts 或 API 路由處理程式中進行速率限制,而非 React 元件中
  • 依據層級(免費、專業、企業方案)區分限制——將層級資訊儲存在 JWT 中或透過資料庫查詢取得
  • 記錄隨附呼叫者身份的速率限制觸發事件——以便及早偵測濫用模式

模式

// Upstash 無伺服器模式
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);

應避免事項

  • 在無伺服器或多實例設定中使用記憶體內速率限制——狀態會在呼叫之間遺失
  • 僅在應用程式層進行速率限制——應同時在 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.

相關技能