ahur-system/ai-rate-limiter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ahur-system/ai-rate-limiter

Composer 安装命令:

composer require ahur-system/ai-rate-limiter

包简介

Intelligent API Rate Limiting with AI-powered adaptive throttling

README 文档

README

PHP Version License Redis

An innovative PHP rate limiting library that uses artificial intelligence to provide adaptive throttling based on usage patterns. This library goes beyond traditional rate limiting by learning from user behavior and optimizing limits in real-time.

⚠️ Important Information

🤖 AI Implementation

This library uses AI concepts (pattern learning, adaptive algorithms, intelligent decision making) but does NOT require:

  • ❌ No machine learning libraries
  • ❌ No neural networks or deep learning
  • ❌ No external AI services
  • ❌ No complex ML algorithms

The "AI" is implemented through:

  • Pattern Learning: Analyzes historical usage data
  • Adaptive Algorithms: Dynamically adjusts limits based on behavior
  • Intelligent Decision Making: Context-aware rate limiting
  • Statistical Analysis: Trend calculation and burst detection

🔑 Licensing

  • MIT License: Completely free to use
  • No License Key Required: No proprietary licensing system
  • Open Source: Full source code available
  • No Usage Limits: Use in any number of projects
  • Commercial Use: Allowed for commercial applications

🚀 Ready to Use

Simply install via Composer and start using immediately - no additional setup, licensing, or AI services required!

🚀 Features

🤖 AI-Powered Intelligence

  • Adaptive Rate Limiting: Automatically adjusts limits based on usage patterns
  • Burst Detection: Intelligently detects and handles traffic bursts
  • Pattern Learning: Learns from historical usage data to optimize performance
  • Predictive Throttling: Anticipates usage patterns to prevent rate limit violations

🎯 Smart Features

  • Multi-Tenant Support: Isolated rate limiting for different users/APIs
  • Endpoint-Specific Limits: Different limits for different API endpoints
  • Intelligent Retry Strategies: Multiple strategies (exponential, linear, fixed, jitter, adaptive)
  • Real-time Analytics: Detailed usage statistics and trends
  • HTTP Header Integration: Standard rate limiting headers for APIs

🔧 Technical Excellence

  • High Performance: Redis-based storage for lightning-fast operations
  • Memory Efficient: Automatic cleanup of old patterns
  • Thread Safe: Designed for concurrent access
  • Extensible: Easy to customize and extend

📦 Installation

Requirements

  • PHP 8.1 or higher
  • Redis server
  • PHP Redis extension

Composer Installation

composer require ahur-system/ai-rate-limiter

Manual Installation

git clone https://github.com/ahur-system/ai-rate-limiter.git
cd ai-rate-limiter
composer install

🚀 Quick Start

<?php

require_once 'vendor/autoload.php';

use AhurSystem\AIRateLimiter\AIRateLimiter;

// Initialize Redis
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// Create rate limiter
$limiter = new AIRateLimiter($redis, [
    'default_limit' => 100,
    'default_window' => 3600, // 1 hour
    'learning_enabled' => true,
    'adaptive_throttling' => true
]);

// Check rate limit
$result = $limiter->check('user_123', 'api/v1/users', [
    'user_agent' => 'MyApp/1.0',
    'ip' => '192.168.1.100'
]);

if ($result->isAllowed()) {
    echo "Request allowed! Remaining: " . $result->getRemainingRequests();
} else {
    echo "Rate limited! Retry after: " . $result->getRetryDelay() . " seconds";
}

📚 Usage Examples

Basic Rate Limiting

$result = $limiter->check('user_123', 'api/v1/users');

echo "Allowed: " . ($result->isAllowed() ? 'Yes' : 'No') . "\n";
echo "Remaining: " . $result->getRemainingRequests() . "\n";
echo "Reset time: " . $result->getResetDateTime()->format('Y-m-d H:i:s') . "\n";

API Integration with Headers

$result = $limiter->check('api_key_456', 'api/v1/data');

// Set HTTP headers
foreach ($result->getHeaders() as $name => $value) {
    header("$name: $value");
}

// Check if rate limited
if (!$result->isAllowed()) {
    http_response_code(429);
    echo json_encode([
        'error' => 'Rate limit exceeded',
        'retry_after' => $result->getRetryDelay()
    ]);
    exit;
}

Usage Statistics

$result = $limiter->check('user_123', 'api/v1/users');
$stats = $result->getStats();

echo "Current usage: " . $stats['current_usage'] . "\n";
echo "Pattern count: " . $stats['pattern_count'] . "\n";
echo "Usage trend: " . round($stats['trend'] * 100, 2) . "%\n";
echo "Burst factor: " . round($stats['burst_factor'], 2) . "\n";

Retry Strategies

The library supports multiple retry strategies for handling rate-limited requests:

// Available strategies
$strategies = $limiter->getStrategyDescriptions();
foreach ($strategies as $name => $description) {
    echo "$name: $description\n";
}

// Use different strategies
$limiter = new AIRateLimiter($redis, [
    'retry_strategy' => 'exponential' // Default: exponential backoff
]);

$limiter = new AIRateLimiter($redis, [
    'retry_strategy' => 'linear' // Linear increase
]);

$limiter = new AIRateLimiter($redis, [
    'retry_strategy' => 'jitter' // Exponential with random jitter
]);

$limiter = new AIRateLimiter($redis, [
    'retry_strategy' => 'fixed' // Fixed delay
]);

$limiter = new AIRateLimiter($redis, [
    'retry_strategy' => 'adaptive' // Pattern-based adaptive delay
]);

Strategy Details:

  • exponential: 60s, 120s, 240s, 480s... (recommended for APIs)
  • linear: 60s, 120s, 180s, 240s... (predictable increases)
  • fixed: Always 60s (simple and consistent)
  • jitter: Exponential with ±10% randomness (prevents thundering herd)
  • adaptive: Based on usage patterns and historical data

Reset Rate Limits

// Reset limits for a specific user and endpoint
$limiter->reset('user_123', 'api/v1/users');

🏗️ Architecture

Core Components

  1. AIRateLimiter: Main class handling rate limiting logic
  2. RateLimitResult: Encapsulates rate limiting results
  3. Pattern Analysis: AI algorithms for usage pattern detection
  4. Redis Storage: High-performance data storage

Data Flow

Request → Pattern Analysis → AI Decision → Rate Limit Check → Result
    ↓           ↓              ↓              ↓              ↓
  Context   Historical    Adaptive      Redis Store    Response
            Patterns      Limits

🧪 Testing

Run the test suite:

composer test

Run examples:

php examples/basic_usage.php

📈 Performance

Benchmarks

  • Check Operation: ~0.1ms average response time
  • Pattern Analysis: ~0.5ms for complex patterns
  • Memory Usage: ~1KB per user pattern
  • Redis Operations: 2-3 operations per check

Scalability

  • Supports millions of concurrent users
  • Horizontal scaling with Redis cluster
  • Automatic cleanup of old patterns
  • Efficient memory usage

🔒 Security Features

  • Isolation: Multi-tenant support with key isolation
  • Validation: Input validation and sanitization
  • Rate Limiting: Prevents abuse and DoS attacks
  • Audit Trail: Detailed logging of rate limit decisions

🤝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup

git clone https://github.com/ahur-system/ai-rate-limiter.git
cd ai-rate-limiter
composer install
composer test

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Inspired by modern AI/ML approaches to rate limiting
  • Built for the PHP community
  • Designed for high-performance applications

📞 Support

Made with ❤️ for the PHP community

This innovative rate limiting solution combines the power of artificial intelligence with the simplicity of PHP to provide intelligent, adaptive API protection.

ahur-system/ai-rate-limiter 适用场景与选型建议

ahur-system/ai-rate-limiter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 07 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「redis」 「api」 「throttling」 「adaptive」 「ai」 「rate-limiting」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ahur-system/ai-rate-limiter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ahur-system/ai-rate-limiter 我们能提供哪些服务?
定制开发 / 二次开发

基于 ahur-system/ai-rate-limiter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 39
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-30