SyZero.RateLimiting 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SyZero.RateLimiting --version 1.2.0
                    
NuGet\Install-Package SyZero.RateLimiting -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SyZero.RateLimiting" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SyZero.RateLimiting" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="SyZero.RateLimiting" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SyZero.RateLimiting --version 1.2.0
                    
#r "nuget: SyZero.RateLimiting, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SyZero.RateLimiting@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SyZero.RateLimiting&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=SyZero.RateLimiting&version=1.2.0
                    
Install as a Cake Tool

SyZero.RateLimiting

限流模块:包装 .NET 内置 System.Threading.RateLimiting 与 ASP.NET Core 限流中间件,提供按 IP 的全局固定窗口策略;同时给出 IDistributedRateLimiter 抽象,可由 SyZero.Redis 提供分布式实现。

📦 安装

dotnet add package SyZero.RateLimiting

✨ 特性

  • 🚦 开箱即用全局限流:默认按客户端 IP 分区、固定窗口
  • 🧩 IDistributedRateLimiter 抽象:进程内默认实现 LocalDistributedRateLimiter,分布式实现见 SyZero.Redis
  • ⚙️ 可配置:窗口长度、令牌数、拒绝状态码均可在 appsettings.json 调整

🚀 快速开始

1. 注册

builder.Services.AddSyZeroRateLimiting(builder.Configuration);

2. 中间件

app.UseRouting();
app.UseSyZeroRateLimiting();
app.MapControllers().RequireRateLimiting("syzero-global");

3. 业务侧使用抽象

public class LoginService
{
    private readonly IDistributedRateLimiter _limiter;
    public LoginService(IDistributedRateLimiter limiter) => _limiter = limiter;

    public async Task<bool> TryLoginAsync(string userName)
    {
        if (!await _limiter.TryAcquireAsync($"login:{userName}", permitLimit: 5, window: 60))
        {
            throw new BusinessException("登录过于频繁,请稍后再试");
        }
        // ... 业务 ...
        return true;
    }
}

⚙️ 配置项

appsettings.json 中的 SyZeroRateLimiting 节:

字段 默认值 说明
EnableGlobal true 是否启用全局策略
GlobalPolicyName syzero-global 策略名称
GlobalPermitLimit 100 窗口允许请求数
GlobalWindowSeconds 60 窗口长度(秒)
RejectionStatusCode 429 拒绝时的 HTTP 状态码

📜 许可证

Apache 2.0

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SyZero.RateLimiting:

Package Downloads
SyZero.Redis

SyZero-Redis

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.0.0 103 10/23/2026
9.1.0 81 10/22/2026
1.2.0 84 10/22/2026
1.2.0-dev.1 49 10/22/2026