TokenBucket

Search for a command to run...

No comments yet. Be the first to comment.
At the beginning of this open-source column, I wrote this article to describe the birth and development of open-source projects, express my views on the open-source community and ecology, and share it with you. Some opinions are out of personal pers...
Want to learn how to code? This article will guide you through the jungle of technology and resources to help you go from no knowledge to fast, interactive and modern coding skills, following the forest path I took. I spent a month learning how to st...

this article describes how to analyze and design State persistence management through React Hooks. Analysis Normal frontend, components are class files, maintain their own status, and are not easy to reuse. First, separate the UI and status of th...

We believe that the experience of the experience is linked to the actual things and actual behaviors, while the technology of the technician symbolizes more general knowledge. First of all, in practical operation, it can be seen that skilled people ...


the token bucket algorithm is the most commonly used algorithm in network Traffic Shaping (Traffic Shaping) and Rate Limiting (Rate Limiting).
Typically, the token bucket algorithm is used to control the number of data sent to the network and allow the sending of burst data.
this package is based on the Token Bucket algorithm (Token Bucket) to implement throttling, which is very easy to use. RateLimiter is often used to limit the access rate to some physical or logical resources. It supports three methods,
assume that one is working RateLimiter
For a Ratelimiter that generates a token per second, every second without a token, we will add a token 1.
If the Ratelimiter does not use it in 10 seconds, then tokens become 10.0. At this time, a request arrives and requests three tokens, we will serve it from the token in Ratelimiter, tokens to 7.0. After this request, another request comes and requests 10 tokens.
We will from the remaining 7 token cards from RatelimiterFor this request, there are three tokens left, we will get them from the new token produced by Ratelimiter.
We already know that the Ratelimiter produces 1 new token per second, which means that the above request still requires the three commands required for above request. The card requires it to wait for 3 seconds.
Imagine a Ratelimiter generated a token per second, and now it is not used (in the initial state). If an expensive request requires 100 token cards. If we choose to let this request wait for 100 seconds before allowing it to execute, this is obviously ridiculous.
Why do we do nothing but just wait for 100 seconds? A better approach is to allow this request to execute immediately (no different from all), and then postpone the subsequent request to the right time point.
We allow this expensive task to perform immediately and delay the subsequent request for 100 seconds. This strategy is to let the task execute and wait at the same time.
An important conclusion: Ratelimit does not remember the last request, but the next request allows the time to execute. This can also tell us very straightforwardly that the time interval of reaching the next scheduling time point.
The Ratelimiter is also very simple: the next scheduling time has passed. The difference between this time and the current time is how long the Ratelimiter has not been used. We will translate this time into tokens.Limit == 1), and just one request per second, then tokens will not grow.
Ratelimiter has a barrel capacity that is directly discarded when the request is greater than the capacity of this barrel.
https://github.com/golang/time/blob/master/rate/rate.go