FireflySoft.MultiRateLimit.AspNetCore 3.1.3

dotnet add package FireflySoft.MultiRateLimit.AspNetCore --version 3.1.3                
NuGet\Install-Package FireflySoft.MultiRateLimit.AspNetCore -Version 3.1.3                
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="FireflySoft.MultiRateLimit.AspNetCore" Version="3.1.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FireflySoft.MultiRateLimit.AspNetCore --version 3.1.3                
#r "nuget: FireflySoft.MultiRateLimit.AspNetCore, 3.1.3"                
#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.
// Install FireflySoft.MultiRateLimit.AspNetCore as a Cake Addin
#addin nuget:?package=FireflySoft.MultiRateLimit.AspNetCore&version=3.1.3

// Install FireflySoft.MultiRateLimit.AspNetCore as a Cake Tool
#tool nuget:?package=FireflySoft.MultiRateLimit.AspNetCore&version=3.1.3                

Introduction

Fireflysoft.RateLimit is a rate limiting library based on .Net standard. Its core is simple and lightweight, and can flexibly meet the rate limiting needs of many scenarios.

Features

  • Multiple rate limiting algorithms: built-in fixed window, sliding window, leaky bucket, token bucket, and can be extended.
  • Multiple counting storage: memory and Redis (including cluster).
  • Distributed friendly: supports unified counting of distributed programs with Redis storage.
  • Flexible rate limiting targets: each data can be extracted from the request to set rate limiting targets.
  • Support rate limit penalty: the client can be locked for a period of time after the rate limit is triggered.
  • Time window enhancement: support to the millisecond level; support starting from the starting point of time periods such as seconds, minutes, hours, dates, etc.
  • Real-time tracking: the number of requests processed and the remaining allowed requests in the current counting cycle, as well as the reset time of the counting cycle.
  • Dynamically change the rules: support the dynamic change of the rate limiting rules when the program is running.
  • Custom error: you can customize the error code and error message after the current limit is triggered.
  • Universality: in principle, it can meet any scenario that requires rate limiting.

Usage

The following code calls the rate-limit middleware from Startup.Configure:

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddRateLimit(new IAlgorithm[2] {
            new InProcessFixedWindowAlgorithm(
            new[] {
                new FixedWindowRule()
                {
                    ExtractTarget = context =>
                    {
                        var httpContext= context as HttpContext;

                        // Through CDN
                        var ip = httpContext!.Request.Headers["Cdn-Src-Ip"].FirstOrDefault();
                        if (!string.IsNullOrEmpty(ip))
                            return ip;

                        // Through SLB
                        ip = httpContext!.Request.Headers["X-Forwarded-For"].FirstOrDefault();
                        if (!string.IsNullOrEmpty(ip))
                            return ip;

                        ip = httpContext!.Connection.RemoteIpAddress?.ToString();
                        return ip??"Anonymous-IP";
                    },
                    CheckRuleMatching = context =>
                    {
                        return true;
                    },
                    Name = "ClientIPRule",
                    LimitNumber = 30,
                    StatWindow = TimeSpan.FromSeconds(5)
                }
            }),
            new InProcessTokenBucketAlgorithm(
            new[] {
                new TokenBucketRule(4,4,TimeSpan.FromSeconds(5))
                {
                    ExtractTarget = context =>
                    {
                            var httpContext= context as HttpContext;
                        return httpContext!.Request.Path.Value;
                    },
                    CheckRuleMatching = context =>
                    {
                        return true;
                    },
                    Name="default limit rule",
                }
            })
        });

    ...
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...

    app.UseMultiRateLimit();

    ...
}
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.3 451 10/9/2022
3.1.2 398 9/26/2022
3.1.1 429 9/26/2022
3.1.0 435 9/26/2022

Return X-RateLimit-XXX in HTTP response.