令牌桶算法实现限流

前言

高并发场景下,为了保障服务的高可用(过载保护),除了使用消息中间件做流量削峰之外,还可以在网关侧做限流;

而令牌桶则是常见的限流算法,本文简单记录下。

Token bucket

令牌桶算法维基百科

 

主要思想

1. There is a bucket of a token that can hold up the number of burst at most.

2. We will add tokens into this bucket at a rate of N per second. If the bucket is full, no more tokens are added to this bucket. We call this rate as rate.

3. When there are requests, we will take the corresponding number of tokens from the bucket. If the bucket is empty, block/discard it until enough tokens are put in.

功能实现

1. In the case of running for a long time, the average value of the limited request rate is equal to the rate of adding tokens, the rate.

2. Allow a certain level of peak flow:

  • If the request rate is M and is greater than rate, the rate of token decreasing is M - rate.
  • Therefore, the time for us to take all the tokens from a full bucket is burst / (M - rate), and the number of the requests be accepted is burst / (M - rate) * M during this period.

All in all, it can be simply understood as rate is the average request rate and burst is is the instantaneous maximum request rate.

 

代码实现

 

 

Golang实现

 

补充

- 漏桶:桶里装的是请求(限制的是最大请求数,超过则丢掉),有缓存,适合秒杀场景(请求量级远超服务能力)

- 令牌桶:桶里装的是令牌/服务能力,无缓存,适合请求数在一定范围内浮动。

 

posted @ 2022-06-10 22:32  lixin[at]hitwh  阅读(263)  评论(0编辑  收藏  举报