Skip to content

Commit d632c8f

Browse files
huangdijiaCopilot
andcommitted
feat: add SmartOrder annotation for intelligent RateLimit prioritization (#1016)
* feat: add SmartOrder annotation for intelligent RateLimit prioritization This feature introduces a new SmartOrder annotation that enables intelligent prioritization of multiple RateLimit annotations on the same method. Key changes: - Added SmartOrder annotation class for marking methods that need smart ordering - Modified RateLimitAspect to support priority-based RateLimit processing - RateLimits are now processed in order of strictness (maxAttempts/decay ratio) - More restrictive rate limits are evaluated first for better efficiency The SmartOrder annotation allows developers to optimize rate limiting behavior by ensuring stricter limits are checked before more lenient ones. * feat: replace SmartOrder annotation with AutoSort for improved rate limit prioritization * Update README.md Co-Authored-By: copilot-swe-agent[bot] <[email protected]> Co-Authored-By: huangdijia <[email protected]> * feat: 添加中文文档以支持限流组件的使用说明 --------- Co-authored-by: Deeka Wong <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]>
1 parent 5f4f2ec commit d632c8f

File tree

4 files changed

+578
-1
lines changed

4 files changed

+578
-1
lines changed

src/rate-limit/README.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ composer require friendsofhyperf/rate-limit
2727
- **Flexible Usage**
2828
- Annotation-based rate limiting via Aspect
2929
- Custom middleware support
30+
- **Smart Order for Multiple Annotations**
31+
- Automatic prioritization of multiple RateLimit annotations
32+
- Intelligent ordering by strictness (maxAttempts/decay ratio)
33+
- More restrictive limits evaluated first for better performance
3034
- **Flexible Key Generation**
3135
- Default method/class-based keys
3236
- Custom key with placeholders support
@@ -39,7 +43,7 @@ composer require friendsofhyperf/rate-limit
3943

4044
## Usage
4145

42-
### Method 1: Using Annotagion
46+
### Method 1: Using Annotation
4347

4448
The easiest way to add rate limiting is using the `#[RateLimit]` attribute:
4549

@@ -142,6 +146,49 @@ class UserController
142146
| `response` | `string` | `'Too Many Attempts.'` | Custom response when rate limit is exceeded |
143147
| `responseCode` | `int` | `429` | HTTP response code when rate limit is exceeded |
144148

149+
#### Multiple RateLimits with AutoSort
150+
151+
When you need multiple rate limits on the same method (e.g., per-minute and per-hour limits), you can use the `AutoSort` annotation to automatically prioritize them:
152+
153+
```php
154+
<?php
155+
156+
use FriendsOfHyperf\RateLimit\Annotation\RateLimit;
157+
use FriendsOfHyperf\RateLimit\Annotation\AutoSort;
158+
159+
class ApiController
160+
{
161+
/**
162+
* Multiple rate limits with smart prioritization
163+
* Stricter limits (smaller maxAttempts/decay ratio) are evaluated first
164+
*/
165+
#[AutoSort]
166+
#[RateLimit(maxAttempts: 10, decay: 60)] // 10 requests/minute - evaluated first
167+
#[RateLimit(maxAttempts: 100, decay: 3600)] // 100 requests/hour - evaluated second
168+
public function expensiveOperation()
169+
{
170+
// Your code here
171+
}
172+
173+
/**
174+
* Without AutoSort, rate limits are evaluated in declaration order
175+
*/
176+
#[RateLimit(maxAttempts: 100, decay: 3600)] // Evaluated first
177+
#[RateLimit(maxAttempts: 10, decay: 60)] // Evaluated second
178+
public function anotherOperation()
179+
{
180+
// Your code here
181+
}
182+
}
183+
```
184+
185+
**Benefits of AutoSort:**
186+
187+
- **Performance**: Stricter limits are checked first, avoiding unnecessary checks of more lenient limits
188+
- **Intelligence**: Automatically calculates priority based on limit strictness (maxAttempts/decay ratio)
189+
- **Opt-in**: Only affects methods where `AutoSort` is explicitly used
190+
- **Backward Compatible**: Existing code continues to work without changes
191+
145192
#### Key Placeholders
146193

147194
The `key` parameter supports dynamic placeholders that will be replaced with method arguments:
@@ -400,6 +447,28 @@ class IpRateLimitMiddleware extends RateLimitMiddleware
400447
}
401448
```
402449

450+
### Example 5: Multiple Rate Limits with Smart Order
451+
452+
Use AutoSort to efficiently handle multiple rate limits on expensive operations:
453+
454+
```php
455+
class ReportController
456+
{
457+
/**
458+
* Expensive report generation with multiple protection levels
459+
* AutoSort ensures stricter limits are checked first for better performance
460+
*/
461+
#[AutoSort]
462+
#[RateLimit(maxAttempts: 5, decay: 60, response: 'Too many requests. Max 5 per minute')] // Emergency brake
463+
#[RateLimit(maxAttempts: 30, decay: 3600, response: 'Hourly limit exceeded. Max 30 per hour')] // Sustained load
464+
#[RateLimit(maxAttempts: 100, decay: 86400, response: 'Daily limit exceeded. Max 100 per day')] // Daily cap
465+
public function generateReport($reportType)
466+
{
467+
// Expensive report generation logic here
468+
}
469+
}
470+
```
471+
403472
## License
404473

405474
[MIT](LICENSE)

0 commit comments

Comments
 (0)