In modern web applications, a burst of traffic is inevitable. To prevent the server from being overwhelmed by too many requests,Rate LimitingIt is a crucial technical means.
This article will demonstrate how to use the Gin framework in Go languageLeak bucket algorithmandToken bucket algorithmto implement the current limit of the API.
The significance of current limit
The main purpose of current limiting is to protect system resources and prevent server crashes due to excessive requests. At the same time, it can also prevent malicious users from attacking the system and ensure the stability and availability of services.
Two common current limiting algorithms
1. Leaky Bucket Algorithm
The leaking bucket algorithm treats the request as a drop of water, which first enters the bucket and then flows out of the bucket at a fixed rate. If the request rate exceeds the bucket's outflow rate, the excess request will be discarded.
The advantage of this algorithm is very obvious, which is that it makes the request very stable, but the disadvantages are also obvious. Because the request is very stable, it is not suitable for some flash sales and other scenarios where there may be peak traffic at a certain period of time. It is not easy to control the entry of flow.
2. Token bucket algorithm (Token Bucket)
In the token bucket algorithm, the system will add tokens to the bucket at a fixed rate, and each request needs to obtain a token before it can be executed. If there are not enough tokens in the bucket, the request will be denied.
Code implementation
In this example, we will show how to apply these two algorithms in the Gin framework to implement the current limit of the API.
package main import ( "fmt" "net/http" "time" "/gin-gonic/gin" ratelimit2 "/juju/ratelimit" // Token bucket algorithm ratelimit1 "/ratelimit" // Leak bucket algorithm) func pingHandler(c *) { (200, { "message": "pong", }) } func pingHandler2(c *) { (200, { "message": "pong2", }) } // rateLimit1 uses the leaky bucket algorithm to limit the request ratefunc rateLimit1() func(ctx *) { // Leak barrel algorithm, the first parameter is the time interval between two drops of water. // This means that the time interval between two drops of water is 100 nanoseconds rl := (100) return func(ctx *) { // Try to remove the water droplets if waitTime := ().Sub(()); waitTime > 0 { ("You need to wait %v seconds before the next drop of water will drip\n", waitTime) // Here we can let the program continue to wait, or we can directly refuse to drop it // (waitTime) (, "rate limit, try again later") () return } // Proof can continue to be executed () } } // rateLimit2 uses the token bucket algorithm to limit the request ratefunc rateLimit2() func(ctx *) { // Token bucket algorithm: The first parameter is the rate of filling the token per second // The second parameter is the capacity of the token bucket // Here it means 10 tokens are filled per second rl := (, 10) return func(ctx *) { // Try to get out the token var num int64 = 1 // Here it indicates whether the num tokens need to be equal to the number of tokens that have been retrieved // Not equal means that current limit has been exceeded // For example, if each request consumes 2 tokens, but the number of tokens taken from the bucket is 1, then it is considered that the current limit has exceeded (generally, a request consumes one token, here is an example) if (num) != num { // No token was retrieved this time, which means that the current limit has been exceeded (, "rate limit, try again later") () return } // Proof can continue to be executed () } } func main() { r := () // Leaked bucket algorithm current limit ("/ping", rateLimit1(), pingHandler) // Token bucket algorithm current limit ("/ping2", rateLimit2(), pingHandler2) () }
Code parsing
Implementation of leaky bucket algorithm (rateLimit1 function)
- pass
/ratelimit
In the packageThe method creates a current limiter.
- When the request rate exceeds the processing capability of the current limiter, the request will be denied and returns "rate limit, try again later".
Implementation of token bucket algorithm (rateLimit2 function)
- use
/juju/ratelimit
The package implements the token bucket algorithm. Fill a certain number of tokens into the bucket per second. - If there are not enough tokens in the bucket, the request will be denied.
Gin routing configuration
existmain
In the function,rateLimit1
andrateLimit2
The middleware is/ping
and/ping2
The route sets the leaky bucket and the token bucket flow limit respectively.
Summarize
In this article, we demonstrate how to implement current limiting of the API using leaky bucket algorithms and token bucket algorithms in Go.
These algorithms are very useful in highly concurrent Web services, which can effectively prevent services from being overwhelmed by large amounts of requests and ensure system stability. Hopefully, through this article, you can better understand and apply these current limiting technologies to your project.
The above is the detailed content of Go's use of leaked bucket algorithm and token bucket algorithm to implement API current limit. For more information about Go API current limit, please pay attention to my other related articles!