令牌桶算法实现限流

 


前言

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

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

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 @   RainingInMacondo  阅读(269)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
历史上的今天:
2017-06-10 Python基础(正则、序列化、常用模块和面向对象)
点击右上角即可分享
微信分享提示