Rabbit.Common.RateLimit 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Rabbit.Common.RateLimit --version 1.1.0
                    
NuGet\Install-Package Rabbit.Common.RateLimit -Version 1.1.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="Rabbit.Common.RateLimit" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Rabbit.Common.RateLimit" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Rabbit.Common.RateLimit" />
                    
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 Rabbit.Common.RateLimit --version 1.1.0
                    
#r "nuget: Rabbit.Common.RateLimit, 1.1.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 Rabbit.Common.RateLimit@1.1.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=Rabbit.Common.RateLimit&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Rabbit.Common.RateLimit&version=1.1.0
                    
Install as a Cake Tool

Rabbit.Common.RateLimit

限流组件,支持令牌桶和滑动窗口算法。

安装

dotnet add package Rabbit.Common.RateLimit

依赖

  • Rabbit.Common.Lite
  • Rabbit.Common.Redis(可选,用于分布式限流)
  • FreeRedis(可选,用于 Redis 存储)

功能

  • 令牌桶算法: 平滑限流,支持突发流量
  • 滑动窗口算法: 精确计数
  • Redis 存储: 分布式限流支持

快速开始

1. 配置限流

using Rabbit.Common.RateLimit;

// 内存存储(单机)
services.AddRateLimitConfigure();

// Redis 存储(单实例,使用默认 IRedisClient)
services.AddRateLimitConfigure(RateLimitStorageType.Redis);

// Redis 存储(多实例,指定 Key)
services.AddFreeRedisMapConfigure("RateLimitRedis", "127.0.0.1:6379");
services.AddRateLimitConfigure(RateLimitStorageType.Redis, "RateLimitRedis");

2. 使用中间件

app.Use(async (context, next) =>
{
    var store = context.RequestServices.GetRequiredService<IRateLimitStore>();

    // 按 IP 限流
    var key = context.Connection.RemoteIpAddress?.ToString() ?? "unknown";

    var options = new RateLimitOptions
    {
        Capacity = 100,           // 最大请求数
        RefillPerSecond = 10,     // 每秒填充速率
        Window = TimeSpan.FromSeconds(1),
        Prefix = "rl:api:"
    };

    if (!await store.IsAllowedAsync(key, options, AlgorithmEnum.TokenBucket))
    {
        context.Response.StatusCode = 429; // Too Many Requests
        await context.Response.WriteAsync("请求过于频繁,请稍后重试");
        return;
    }

    await next();
});

3. 不同场景限流

// API 限流 - 令牌桶(允许突发)
var apiOptions = new RateLimitOptions
{
    Capacity = 100,
    RefillPerSecond = 10,
    Prefix = "rl:api:"
};

// 登录限流 - 滑动窗口(严格计数)
var loginOptions = new RateLimitOptions
{
    Capacity = 5,              // 5次
    Window = TimeSpan.FromMinutes(1), // 每分钟
    Prefix = "rl:login:"
};

if (!await store.IsAllowedAsync(userId, loginOptions, AlgorithmEnum.SlidingWindow))
{
    return "登录次数过多,请稍后再试";
}

配置选项

public class RateLimitOptions
{
    /// <summary>
    /// 令牌桶容量 / 窗口最大请求数
    /// </summary>
    public int Capacity { get; set; }

    /// <summary>
    /// 每秒填充速率(仅令牌桶用)
    /// </summary>
    public int RefillPerSecond { get; set; }

    /// <summary>
    /// 时间窗口
    /// </summary>
    public TimeSpan Window { get; set; } = TimeSpan.FromSeconds(1);

    /// <summary>
    /// 键前缀
    /// </summary>
    public string Prefix { get; set; } = "rl:";
}

算法对比

算法 特点 适用场景
TokenBucket 允许突发,平滑限流 普通 API
SlidingWindow 精确计数,严格限流 登录、支付

许可证

MIT

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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 Rabbit.Common.RateLimit:

Package Downloads
Rabbit.Common.Full

Meta-package referencing all Rabbit.Common utility packages. / Rabbit.Common 全家桶,引用所有子包

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.1 129 8/11/2026
1.4.0 120 7/16/2026
1.3.1 143 5/28/2026
1.3.0 130 5/19/2026
1.2.1 132 4/7/2026
1.2.0 155 3/27/2026
1.1.0 165 3/12/2026
1.0.0 167 3/12/2026