Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of limiting current by using fixed window in ASP.NET Core

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "ASP.NET Core uses fixed window current limiting method is what", the explanation content in the article is simple and clear, easy to learn and understand, please follow the idea of small series slowly in-depth, together to study and learn "ASP.NET Core uses fixed window current limiting method is what"!

algorithm principle

Fixed window algorithm, also known as counter algorithm, is a simple current-limiting algorithm. A threshold value and a counting value are set in unit time, the counting value is increased by one every time a request is received, current limiting is triggered if the counting value exceeds the threshold value, normal processing is requested if the counting value does not reach the threshold value, and the counting value is cleared and accumulated again after entering the next unit time.

As shown above, the time unit is 1 second and the threshold is 3.

3 requests in the first second, no current limit will be triggered;

1 request in the second second, no current limit is triggered;

4 requests in the third second, the first 3 requests in this second are processed normally, and the fourth request triggers current limiting and will be rejected for processing.

Current limiting will not be triggered in the following 4 seconds and 5 seconds, and all requests will be processed normally.

algorithm implementation

There are two implementation methods: in-process memory fixed window algorithm and Redis-based fixed window algorithm.

In-process fixed window algorithm

Using a dictionary, Key is the current limiting target and Value includes the count value and expiration time. When processing the request, first extract the current limiting target from the request, and then search in the dictionary according to the current limiting target. If it does not exist, add a dictionary entry, and the count value is 1, and the expiration time is the current time + the current limiting unit time; if it exists, check whether it expires. If it expires, the count value returns to 1, and the expiration time is the current time + the current limiting unit time. If it does not expire, only the count value increases by 1. Here you need to pay attention to multi-threading issues, read and write data when you need to lock.

MemoryCache can be used in C#language, its cache items have an expiration time, do not need to recycle expired items themselves.

In-process counting method is most suitable for single-instance processing program current limiting, in the case of multi-instance processing, the number of requests received by each instance may be uneven, and the current limiting effect cannot be guaranteed.

Fixed Window Algorithm Based on Redis

Redis is stored as a KV, similar to a dictionary, and also comes with an expiration time. When processing the request, first extract the current limiting target from the request, and then search in Redis according to the current limiting target. If it does not exist, add a KV item. The Value value is 1, and the expiration time is the current time + current limiting unit time. If it exists, the Value value is increased by 1.

This logic can be encapsulated in a Lua script, because Lua script is also atomic when executed in Redis, so Redis's current limit count is naturally accurate when distributed processing.

algorithm is applied

Here, we take the current-limiting component FireflySoft.RateLimit as an example to implement the fixed window current-limiting in ASP.NET Core.

1. Install Nuget package

There are many ways to install, choose your favorite on the line.

Package Manager commands:

Install-Package FireflySoft.RateLimit.AspNetCore

or. NET command:

dotnet add package FireflySoft.RateLimit.AspNetCore

Or add directly to the project file:

2, the use of middleware

Using middleware in Startup, the demo code is as follows (explained in detail below):

public void ConfigureServices(IServiceCollection services) { ... app.AddRateLimit(new InProcessFixedWindowAlgorithm( new[] { new FixedWindowRule() { ExtractTarget = context => { //extract current limiting target return (context as HttpContext).Request.Path.Value; }, CheckRuleMatching = context => { //Determine whether current limiting is required for the current request return true; }, Name="fixed window limit rule", LimitNumber=30, //Limiting threshold StatWindow=TimeSpan.FromSeconds(1) //Current Limit Unit Time } }) ); ... }public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { ... app.UseRateLimit(); ... }

As above, you need to register the service first, and then use the middleware.

When registering for the service, you need to provide a current limiting algorithm and corresponding rules:

Here we use InProcessFixedWindowAlgorithm, or RedisFixedWindowAlgorithm, we need to pass in a Redis connection.

The current limiting threshold is 30 and the current limiting unit time is 1 second.

ExtractTarget is used to extract the current limiting target, here each different request path. If there is an IO request, the corresponding asynchronous method ExtractTargetAsync is also supported here.

CheckRuleMatching is used to verify whether the current request is current-throttling. If there is an IO request, the corresponding asynchronous method CheckRuleMatchingAsync is also supported here.

By default, HttpStatusCode 429 is returned when the current is limited. You can customize this value with the optional parameter error when AddRateLimit, as well as the contents of Http Header and Body.

The basic uses are those in the example above.

If it is still based on the traditional. NET Framework, you need to register a message processor RateLimitHandler in Application_Start. The algorithm and rules are common. For details, please see the instructions on Github: github.com/bosima/FireflySoft.RateLimit#aspnet

FireflySoft.RateLimit is a current-limiting library based on. NET Standard. Its kernel is simple and lightweight, and it can flexibly cope with various current-limiting scenarios.

Its main features include:

Multiple current-limiting algorithms: built-in fixed window, sliding window, leaky bucket, token bucket four algorithms, but also user-defined expansion.

Multiple count storage: currently supports memory, Redis two storage methods.

Distributed Friendly: Supports uniform counting of distributed programs through Redis storage.

Flexible throttling targets: Various data can be extracted from the request for setting throttling targets.

Current limiting penalty: You can lock the client for a period of time after triggering current limiting.

Dynamically changing rules: supports dynamically changing current limiting rules while the program is running.

Custom error: You can customize the error code and error message after triggering current limit.

Universality: In principle, it can satisfy any scenario that requires current limiting.

Thank you for reading, the above is "ASP.NET Core using fixed window current limiting method is what" content, after this article's study, I believe that we use fixed window current limiting method ASP.NET Core is what this problem has a deeper understanding, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report