GithubHelp home page GithubHelp logo

bosima / fireflysoft.ratelimit Goto Github PK

View Code? Open in Web Editor NEW
148.0 8.0 35.0 462 KB

It is a rate limiting library based on .Net standard.

License: Apache License 2.0

C# 100.00%
rate-limit leaky-bucket token-bucket fixed-window sliding-window redis dotnet-framework middleware aspnetcore rate-limiting memory

fireflysoft.ratelimit's Introduction

FireflySoft.RateLimit           中文

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.

Projects

Project Descriptioin
FireflySoft.RateLmit.Core algorithm, rules, persistence and other core codes.
FireflySoft.RateLimit.AspNet ASP.NET rate-limit middleware based on .NET Framework.
FireflySoft.RateLimit.AspNetCore ASP.NET Core rate-limit middleware.
FireflySoft.RateLimit.Core.UnitTest Unit test for FireflySoft.RateLimit.Core.
FireflySoft.RateLimit.Core.BenchmarkTest Benchmark test for FireflySoft.RateLimit.Core.
Samples/Console FireflySoft.RateLmit.Core sample program.
Samples/AspNet FireflySoft.RateLimit.AspNet sample program.
Samples/AspNetCore FireflySoft.RateLimit.AspNetCore sample program.
Samples/RuleAutoUpdate A sample that can automatic update rate limiting rules.

Usage

ASP.NET Core

1、Install Nuget Package

Package Manager:

Install-Package FireflySoft.RateLimit.AspNetCore

Or .NET CLI:

dotnet add package FireflySoft.RateLimit.AspNetCore

Or Project file:

<ItemGroup>
<PackageReference Include="FireflySoft.RateLimit.AspNetCore" Version="2.*" />
</ItemGroup>

2、Use Middleware

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

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddRateLimit(new InProcessFixedWindowAlgorithm(
        new[] {
            new FixedWindowRule()
            {
                ExtractTarget = context =>
                {
                    return (context as HttpContext).Request.Path.Value;
                },
                CheckRuleMatching = context =>
                {
                    return true;
                },
                Name="default limit rule",
                LimitNumber=30,
                StatWindow=TimeSpan.FromSeconds(1)
            }
        })
    );

    ...
}

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

    app.UseRateLimit();

    ...
}

ASP.NET

1、Install Nuget Package:

Package Manager:

Install-Package FireflySoft.RateLimit.AspNet

2、Register MessageHandler

Open Global.asax.cs, the following code adds the rate limit message handle:

protected void Application_Start()
{
    ...

    GlobalConfiguration.Configuration.MessageHandlers.Add(
        new RateLimitHandler(
            new Core.InProcessAlgorithm.InProcessFixedWindowAlgorithm(
                new[] {
                    new FixedWindowRule()
                    {
                        ExtractTarget = context =>
                        {
                            return (context as HttpRequestMessage).RequestUri.AbsolutePath;
                        },
                        CheckRuleMatching = context =>
                        {
                            return true;
                        },
                        Name="default limit rule",
                        LimitNumber=30,
                        StatWindow=TimeSpan.FromSeconds(1)
                    }
                })
        ));

    ...
}

Others

1、Install Nuget Package

Package Manager:

Install-Package FireflySoft.RateLimit.Core

Or .NET CLI:

dotnet add package FireflySoft.RateLimit.Core

2、Use IAlgorithm

Use IAlgorithm to filter every request, process the return value of Check method.

// Rule
var fixedWindowRules = new FixedWindowRule[]
    {
        new FixedWindowRule()
        {
            Id = "3",
            StatWindow=TimeSpan.FromSeconds(1),
            LimitNumber=30,
            ExtractTarget = (request) =>
            {
                return (request as SimulationRequest).RequestResource;
            },
            CheckRuleMatching = (request) =>
            {
                return true;
            },
        }
    };

// Algorithm
IAlgorithm algorithm = new InProcessFixedWindowAlgorithm(fixedWindowRules);

// Check
var result = algorithm.Check(new SimulationRequest()
    {
        RequestId = Guid.NewGuid().ToString(),
        RequestResource = "home",
        Parameters = new Dictionary<string, string>() {
                    { "from","sample" },
            }
    });

SimulationRequest is a custom request that you can modify to any type.

fireflysoft.ratelimit's People

Contributors

bosima avatar dependabot[bot] avatar frooxius avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fireflysoft.ratelimit's Issues

同一个程序是否支持多个限流算法

期望使用固定窗口算法来控制ip某个时间端不要超过特定数量
期望使用令牌桶算法来控制访问的限流
有什么方法可以同时注册使用这两种算法吗?

LeakyBucket bug when the first period has over outflow_quantity_per_unit quantity let in bucket

asume that the bucket
capacity : 20
outflow_unit: 500 ms
outflow_quantity_per_unit: 5
amount: 1

in the first period ( the first outflow unit: the first 500ms), has 25 requests, end of the period, 5 requests pass, and 20 requests in the buecket.
in the second period, in the theory, also has 5 requests pass (wait is 0, ret = [0, x, 0]), but actually, in the second period all request all return wait > 0

that is: when capacity >= outflow_quantity_per_unit * n (n >= 2), and the first period let in the bucket quantity greater than outflow_quantity_per_unit , then cause the problem.
because the below sentence:
if(current_value>outflow_quantity_per_unit)

Redis storage have a defect

image

这是令牌桶算法方式在redis的存储,这个key的剩余令牌桶数量不会自动更新,只有当每次程序访问的时候才能去更新key剩余的容量还有多少,this one of tow defect,

每次新的请求都会重新创建一个Key,而且这个key不会自动过期,也就是说随着程序运行的越来越久,访问量越来越高,那么redis的key也会越来越多,只能去手动维护?

CheckRuleMatching return false Will it take effect

还有个问题,就是实例化限流规则类的 自定义CheckRuleMatching 委托规则 如果返回false,是不是应该就是不对改规则进行限流,可是实际上貌似没什么作用,甚至还抛出一个异常
image

Defects of redis storage

image

这是令牌桶算法方式在redis的存储,这个key的剩余令牌桶数量不会自动更新,只有当每次程序访问的时候才能去更新key剩余的容量还有多少,this one of tow defect,

每次新的请求都会重新创建一个Key,而且这个key不会自动过期,也就是说随着程序运行的越来越久,访问量越来越高,那么redis的key也会越来越多,只能去手动维护?

FixedWindowRule可以支持动态往容器内增加可用数量么?

您好,

我有这样的一个场景,控制10分钟内只能有1000个用户进入系统,在这期间如果进入的用户有退出的话,我想递增可进入人数

假设:
StatWindow=TimeSpan.FromSeconds(10*60),
LimitNumber=1000,

一分钟之后,剩余可以登录的用户数为800,此时有10个用户退出

我想把当前剩余可以登录的用户数置为810

请问这种在程序运行过程中修改剩余数量的场景支持吗?或者说未来打算支持么?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.