← 返回
效率工具 中文

Rate Limit Pro

Advanced rate limiting with tiered controls and quota management
高级限流与分层控制及配额管理
raghulpasupathi
效率工具 clawhub v1.0.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 1,092
下载
💾 15
安装
1
版本
#latest

概述

Rate Limit Pro

Advanced rate limiting with multiple tiers and quota management.

Implementation

class RateLimiter {
  constructor(options = {}) {
    this.tiers = options.tiers || {
      free: { requests: 10, window: 60000 }, // 10 req/min
      basic: { requests: 100, window: 60000 },
      pro: { requests: 1000, window: 60000 }
    };
    this.requests = new Map();
  }

  checkLimit(userId, tier = 'free') {
    const tierConfig = this.tiers[tier];
    if (!tierConfig) {
      return { allowed: false, reason: 'invalid_tier' };
    }

    const now = Date.now();
    const userRequests = this.requests.get(userId) || [];

    // Remove old requests outside window
    const validRequests = userRequests.filter(
      timestamp => now - timestamp < tierConfig.window
    );

    // Check if under limit
    if (validRequests.length >= tierConfig.requests) {
      const oldestRequest = validRequests[0];
      const resetIn = tierConfig.window - (now - oldestRequest);

      return {
        allowed: false,
        reason: 'rate_limit_exceeded',
        limit: tierConfig.requests,
        remaining: 0,
        resetIn: Math.ceil(resetIn / 1000)
      };
    }

    // Add current request
    validRequests.push(now);
    this.requests.set(userId, validRequests);

    return {
      allowed: true,
      limit: tierConfig.requests,
      remaining: tierConfig.requests - validRequests.length,
      resetIn: Math.ceil(tierConfig.window / 1000)
    };
  }

  resetUser(userId) {
    this.requests.delete(userId);
  }

  getStats(userId) {
    const userRequests = this.requests.get(userId) || [];
    return {
      totalRequests: userRequests.length,
      oldestRequest: userRequests[0] || null,
      newestRequest: userRequests[userRequests.length - 1] || null
    };
  }
}

// Export for OpenClaw
module.exports = { RateLimiter };

Usage

const limiter = new skills.rateLimitPro.RateLimiter({
  tiers: {
    free: { requests: 10, window: 60000 },
    pro: { requests: 1000, window: 60000 }
  }
});

const result = limiter.checkLimit('user123', 'free');
if (result.allowed) {
  // Process request
} else {
  console.log(`Rate limit exceeded. Reset in ${result.resetIn}s`);
}

Configuration

{
  "tiers": {
    "free": { "requests": 10, "window": 60000 },
    "basic": { "requests": 100, "window": 60000 },
    "pro": { "requests": 1000, "window": 60000 }
  }
}

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-29 08:02 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

productivity

Word / DOCX

ivangdavila
创建、检查和编辑 Microsoft Word 文档及 DOCX 文件,支持样式、编号、修订记录、表格、分节符及兼容性检查等功能。
★ 440 📥 148,065
productivity

Nano Pdf

steipete
使用nano-pdf CLI通过自然语言指令编辑PDF
★ 275 📥 114,944
productivity

Weather

steipete
获取当前天气和预报(无需API密钥)
★ 446 📥 226,434