-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPollyRateLimiterAttribute.cs
More file actions
40 lines (36 loc) · 1.43 KB
/
PollyRateLimiterAttribute.cs
File metadata and controls
40 lines (36 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
namespace ZibStack.NET.Aop;
/// <summary>
/// Polly-based rate limiter aspect. Uses a fixed window rate limiter to restrict
/// how many times a method can be called within a time window.
/// Excess calls throw <see cref="Polly.RateLimiting.RateLimiterRejectedException"/>.
/// </summary>
/// <example>
/// <code>
/// // Max 100 calls per 60 seconds:
/// [PollyRateLimiter(PermitLimit = 100, WindowSeconds = 60)]
/// public async Task<SearchResult> SearchAsync(string query) { ... }
///
/// // Max 5 calls per second (burst protection):
/// [PollyRateLimiter(PermitLimit = 5, WindowSeconds = 1)]
/// public async Task SendNotificationAsync(int userId) { ... }
/// </code>
/// </example>
[AspectHandler(typeof(PollyRateLimiterHandler))]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public sealed class PollyRateLimiterAttribute : AspectAttribute
{
/// <summary>
/// Maximum number of calls allowed in the window. Default: 100.
/// </summary>
public int PermitLimit { get; set; } = 100;
/// <summary>
/// Window duration in seconds. Default: 60.
/// </summary>
public int WindowSeconds { get; set; } = 60;
/// <summary>
/// Maximum number of calls that can be queued when the limit is reached.
/// Queued calls wait for the next window. Default: 0 (reject immediately).
/// </summary>
public int QueueLimit { get; set; }
}