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.
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
- 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
Simply install via Composer and start using immediately - no additional setup, licensing, or AI services required!
- 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
- 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
- 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
- PHP 8.1 or higher
- Redis server
- PHP Redis extension
composer require ahur-system/ai-rate-limitergit clone https://github.com/ahur-system/ai-rate-limiter.git
cd ai-rate-limiter
composer install<?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";
}$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";$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;
}$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";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 limits for a specific user and endpoint
$limiter->reset('user_123', 'api/v1/users');- AIRateLimiter: Main class handling rate limiting logic
- RateLimitResult: Encapsulates rate limiting results
- Pattern Analysis: AI algorithms for usage pattern detection
- Redis Storage: High-performance data storage
Request β Pattern Analysis β AI Decision β Rate Limit Check β Result
β β β β β
Context Historical Adaptive Redis Store Response
Patterns Limits
Run the test suite:
composer testRun examples:
php examples/basic_usage.php- 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
- Supports millions of concurrent users
- Horizontal scaling with Redis cluster
- Automatic cleanup of old patterns
- Efficient memory usage
- 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
We welcome contributions! Please see our Contributing Guide for details.
git clone https://github.com/ahur-system/ai-rate-limiter.git
cd ai-rate-limiter
composer install
composer testThis project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by modern AI/ML approaches to rate limiting
- Built for the PHP community
- Designed for high-performance applications
- Issues: GitHub Issues
- Documentation: Wiki
- Discussions: GitHub Discussions
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.