Browse Source

feat: Refactor operation rate limiting methods for simplified parameter handling and add extension methods

pull/25024/head
maliming 3 weeks ago
parent
commit
5b1f3e8304
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 8
      docs/en/docs-nav.json
  2. 4
      docs/en/framework/infrastructure/operation-rate-limiting.md
  3. 38
      framework/src/Volo.Abp.OperationRateLimiting/Volo/Abp/OperationRateLimiting/Checker/OperationRateLimitingCheckerExtensions.cs

8
docs/en/docs-nav.json

@ -721,10 +721,6 @@
"text": "Distributed Locking",
"path": "framework/infrastructure/distributed-locking.md"
},
{
"text": "Operation Rate Limiting",
"path": "framework/infrastructure/operation-rate-limiting.md"
},
{
"text": "Email Sending",
"items": [
@ -811,6 +807,10 @@
"text": "Object to Object Mapping",
"path": "framework/infrastructure/object-to-object-mapping.md"
},
{
"text": "Operation Rate Limiting",
"path": "framework/infrastructure/operation-rate-limiting.md"
},
{
"text": "Settings",
"path": "framework/infrastructure/settings.md"

4
docs/en/framework/infrastructure/operation-rate-limiting.md

@ -64,8 +64,7 @@ public class SmsAppService : ApplicationService
public async Task SendCodeAsync(string phoneNumber)
{
await _rateLimitChecker.CheckAsync("SendSmsCode",
new OperationRateLimitingContext { Parameter = phoneNumber });
await _rateLimitChecker.CheckAsync("SendSmsCode", phoneNumber);
// If we reach here, the limit was not exceeded.
// Send the SMS code...
@ -75,6 +74,7 @@ public class SmsAppService : ApplicationService
* `CheckAsync` increments the counter and throws `AbpOperationRateLimitingException` (HTTP 429) if the limit is exceeded.
* Each phone number has its own counter because we used `PartitionByParameter()`.
* Passing `phoneNumber` directly is a shortcut for `new OperationRateLimitingContext { Parameter = phoneNumber }`. Extension methods are provided for all four methods (`CheckAsync`, `IsAllowedAsync`, `GetStatusAsync`, `ResetAsync`) when you only need to pass a `parameter` string.
That's the basic usage. The following sections explain each concept in detail.

38
framework/src/Volo.Abp.OperationRateLimiting/Volo/Abp/OperationRateLimiting/Checker/OperationRateLimitingCheckerExtensions.cs

@ -0,0 +1,38 @@
using System.Threading.Tasks;
namespace Volo.Abp.OperationRateLimiting;
public static class OperationRateLimitingCheckerExtensions
{
public static Task CheckAsync(
this IOperationRateLimitingChecker checker,
string policyName,
string parameter)
{
return checker.CheckAsync(policyName, new OperationRateLimitingContext { Parameter = parameter });
}
public static Task<bool> IsAllowedAsync(
this IOperationRateLimitingChecker checker,
string policyName,
string parameter)
{
return checker.IsAllowedAsync(policyName, new OperationRateLimitingContext { Parameter = parameter });
}
public static Task<OperationRateLimitingResult> GetStatusAsync(
this IOperationRateLimitingChecker checker,
string policyName,
string parameter)
{
return checker.GetStatusAsync(policyName, new OperationRateLimitingContext { Parameter = parameter });
}
public static Task ResetAsync(
this IOperationRateLimitingChecker checker,
string policyName,
string parameter)
{
return checker.ResetAsync(policyName, new OperationRateLimitingContext { Parameter = parameter });
}
}
Loading…
Cancel
Save