Skip to content

Search EricksonLopez.* Ecosystem

Type a library name, NuGet package ID, .NET type, or architectural concept to search across the portal.

Getting Started with EricksonLopez.RateLimiting

Install EricksonLopez.RateLimiting (1.0.0 on NuGet) along with .AspNetCore or .Redis as needed.

dotnet add package EricksonLopez.RateLimiting --version 1.0.0
Terminal window
dotnet add package EricksonLopez.RateLimiting.AspNetCore --version 1.0.0
dotnet add package EricksonLopez.RateLimiting.Redis --version 1.0.0

AcquireAsync returns Task<Result<RateLimitLease>>, and acquired leases should always be wrapped in using:

using System;
using System.Threading;
using System.Threading.Tasks;
using EricksonLopez.RateLimiting;
using EricksonLopez.Result;
namespace Example.RateLimitingQuickStart;
public static class RateLimiterUsage
{
public static async Task<bool> TryConsumeAsync(string tenantKey, CancellationToken ct)
{
var options = new RateLimiterOptions
{
PermitLimit = 100,
Window = TimeSpan.FromMinutes(1),
SegmentsPerWindow = 6
};
var limiter = new SlidingWindowRateLimiter(options);
Result<RateLimitLease> result = await limiter.AcquireAsync(tenantKey, permits: 1, ct);
if (result.IsFailure || !result.Value.IsAcquired)
{
return false;
}
using RateLimitLease lease = result.Value;
return lease.RemainingPermits >= 0;
}
}