rate-limiting-implementation by aj-geddes/useful-ai-prompts
npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill rate-limiting-implementation实现速率限制和节流机制,以保护您的服务免受滥用,确保资源的公平分配,并在负载下维持系统稳定性。
最小工作示例:
interface TokenBucketConfig {
capacity: number;
refillRate: number; // tokens per second
refillInterval: number; // milliseconds
}
class TokenBucket {
private tokens: number;
private lastRefill: number;
private readonly capacity: number;
private readonly refillRate: number;
private readonly refillInterval: number;
private refillTimer?: NodeJS.Timeout;
constructor(config: TokenBucketConfig) {
this.capacity = config.capacity;
this.tokens = config.capacity;
this.refillRate = config.refillRate;
this.refillInterval = config.refillInterval;
this.lastRefill = Date.now();
this.startRefill();
}
private startRefill(): void {
// ... (完整实现请参阅参考指南)
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
references/ 目录下的详细实现:
| 指南 | 内容 |
|---|---|
| 令牌桶算法 (TypeScript) | 令牌桶算法 (TypeScript) |
| 基于 Redis 的分布式速率限制器 | 基于 Redis 的分布式速率限制器 |
| Express 中间件 | Express 中间件 |
| 滑动窗口算法 (Python) | 滑动窗口算法 (Python) |
| 分层速率限制 | 分层速率限制 |
| 自适应速率限制 | 自适应速率限制 |
每周安装次数
129
仓库
GitHub 星标数
126
首次出现
2026年1月21日
安全审计
安装于
opencode109
gemini-cli106
codex105
cursor96
claude-code92
github-copilot91
Implement rate limiting and throttling mechanisms to protect your services from abuse, ensure fair resource allocation, and maintain system stability under load.
Minimal working example:
interface TokenBucketConfig {
capacity: number;
refillRate: number; // tokens per second
refillInterval: number; // milliseconds
}
class TokenBucket {
private tokens: number;
private lastRefill: number;
private readonly capacity: number;
private readonly refillRate: number;
private readonly refillInterval: number;
private refillTimer?: NodeJS.Timeout;
constructor(config: TokenBucketConfig) {
this.capacity = config.capacity;
this.tokens = config.capacity;
this.refillRate = config.refillRate;
this.refillInterval = config.refillInterval;
this.lastRefill = Date.now();
this.startRefill();
}
private startRefill(): void {
// ... (see reference guides for full implementation)
Detailed implementations in the references/ directory:
| Guide | Contents |
|---|---|
| Token Bucket Algorithm (TypeScript) | Token Bucket Algorithm (TypeScript) |
| Redis-Based Distributed Rate Limiter | Redis-Based Distributed Rate Limiter |
| Express Middleware | Express Middleware |
| Sliding Window Algorithm (Python) | Sliding Window Algorithm (Python) |
| Tiered Rate Limiting | Tiered Rate Limiting |
| Adaptive Rate Limiting | Adaptive Rate Limiting |
Weekly Installs
129
Repository
GitHub Stars
126
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode109
gemini-cli106
codex105
cursor96
claude-code92
github-copilot91
Linux云主机安全托管指南:从SSH加固到HTTPS部署
33,600 周安装