Go 分布式令牌桶限流 + 兜底策略
上篇文章提到固定时间窗口限流无法处理突然请求洪峰情况,本文讲述的令牌桶线路算法则可以比较好的处理此场景。
- 单位时间按照一定速率匀速的生产 token 放入桶内,直到达到桶容量上限。
- 处理请求,每次尝试获取一个或多个令牌,如果拿到则处理请求,失败则拒绝请求。
优点
可以有效处理瞬间的突发流量,桶内存量 token 即可作为流量缓冲区平滑处理突发流量。
缺点
实现较为复杂。
代码实现core/limit/tokenlimit.go
分布式环境下考虑使用 redis 作为桶和令牌的存储容器,采用 lua 脚本实现整个算法流程。
redis lua 脚本
<span class="c1">-- 每秒生成token数量即token生成速度</span>
<span class="kd">local</span> <span class="n">rate</span> <span cl ... ⌘ [Read more](https://gocn.vip/topics/20950)
🧮 USERS:2 FEEDS:6 TWTS:793 ARCHIVED:389717 CACHE:111838 FOLLOWERS:29 FOLLOWING:688
🧮 USERS:1 FEEDS:2 TWTS:113 ARCHIVED:21446 CACHE:1092 FOLLOWERS:13 FOLLOWING:14
GoCN 每日新闻(2022-01-12)
GoCN 每日新闻(2022-01-12)
- Go 实现的自动保存 Git 项目中未提交代码https://github.com/nikochiko/autosaved
- Go 中快速、简单的全栈 Web 开发入门工具包https://github.com/mikestefanello/pagoda
- Golang Redis RESP3 客户端,自动流水线并支持客户端缓存https://github.com/rueian/rueidis
- Runtime 不变性检查库[https:/ … ⌘ Read more
🧮 USERS:2 FEEDS:6 TWTS:792 ARCHIVED:386043 CACHE:112106 FOLLOWERS:29 FOLLOWING:686
🧮 USERS:1 FEEDS:2 TWTS:112 ARCHIVED:21371 CACHE:1085 FOLLOWERS:13 FOLLOWING:14
GoCN 每日新闻 (2022-01-11)
GoCN 每日新闻 (2022-01-11)
- 解读 Go 语言的 2021:稳定为王https://mp.weixin.qq.com/s/9LKyPfhwldgZY7H4iS7sjg
- Go 泛型的 3 个核心设计https://mp.weixin.qq.com/s/I7qysvddG4NDgtYIMMAE3g
- Golang 的 Ealstic 链接库https://mp.weixin.qq.com/s/iHIxsEZf3w06GbO2sHSuRA
- 动态调整 GOGC 优化 Go 的 GC 标记 CPU 占� … ⌘ Read more
🧮 USERS:2 FEEDS:6 TWTS:789 ARCHIVED:382292 CACHE:108685 FOLLOWERS:29 FOLLOWING:686
🧮 USERS:1 FEEDS:2 TWTS:111 ARCHIVED:21169 CACHE:1064 FOLLOWERS:13 FOLLOWING:14
Whoever named “NFTs” really missed a great opportunity…
Can we all just agree to call them this from now on? The Lunduke Journal subscriber exclusives as of this moment: 7 books, 2 games, LundukeFest pass, + articles and podcast episodes. You can subscribe here on Substack or join The Lunduke Journal Community at ⌘ Read more
Real men don’t comment their code…
Insert manly grunting. The Lunduke Journal subscriber exclusives as of this second: 7 books, 2 games, LundukeFest pass, + articles and podcast episodes. ⌘ Read more
GoCN 每日新闻 (2022-01-10)
GoCN 每日新闻 (2022-01-10)- Minecraft 管理 K8S 集群https://eric-jadi.medium.com/minecraft-as-a-k8s-admin-tool-cf16f890de42
- go ebpf 管理包https://github.com/ehids/ebpfmanager
- Go 错误处理https://earthly.dev/blog/golang-errors/
- Go 中实现用户的每日限额(比如一天只能领三次福利) [https://jueji … ⌘ Read more
Decorative Constants
⌘ Read more
🧮 USERS:2 FEEDS:6 TWTS:788 ARCHIVED:378153 CACHE:109899 FOLLOWERS:29 FOLLOWING:686
Decorative Constants
⌘ Read more
🧮 USERS:1 FEEDS:2 TWTS:110 ARCHIVED:21071 CACHE:1047 FOLLOWERS:13 FOLLOWING:14
Guess I fit the picture 💎
Guess I fit the picture 💎

- 带你探究 Go 语言的 iotahttps://mp.weixin.qq.com/s/V5iJgcLhCYMZd5l8akfbVQ
- 项目实战:使用 Fiber + Gorm 构建 REST APIhttps://mp.weixin.qq.com/s/RqUOLP-OG_wOpCzxVTAl6w
- Effective Error Handling in Golanghttps://earthly.dev/blog/golang-errors/
- 为什么不在 golang 中构建一个 redis 客户端[https://mauricio.github.io/2022/0 … ⌘ Read more
GoCN 每日新闻 (2022-01-08)
- Go 1.17.6/1.16.13 版本发布https://groups.google.com/g/golang-announce/c/95ZD3rKn4DI/m/93cyN8F1BAAJ
- 高效的 Go 错误处理https://earthly.dev/blog/golang-errors/
- 从零实现一个 RedisClient 了解 Redis 协议https://mauricio.github.io/2022/01/07/redis-protocol.html
- Golang 简洁架构实战[https://zh … ⌘ Read more
🧮 USERS:2 FEEDS:6 TWTS:785 ARCHIVED:373450 CACHE:109839 FOLLOWERS:29 FOLLOWING:686
🧮 USERS:1 FEEDS:2 TWTS:108 ARCHIVED:20637 CACHE:974 FOLLOWERS:13 FOLLOWING:14
GoCN 每日新闻 (2022-01-07)
GoCN 每日新闻 (2022-01-07)
- 用 golang 在 tor 网络上安全的私有部署共享文件服务https://github.com/R4yGM/garlicshare
- 一个极速的 golang 全栈 web 开发包https://github.com/mikestefanello/pagoda
- ch 一个 clickhouse 客户端,比 rust 和 c++ 的都快https://github.com/go-faster/ch
- 用 gosec 扫描 go 源码的漏洞[https://jcdan3.medium. … ⌘ Read more
🧮 USERS:2 FEEDS:6 TWTS:784 ARCHIVED:370646 CACHE:110065 FOLLOWERS:29 FOLLOWING:686
🧮 USERS:1 FEEDS:2 TWTS:107 ARCHIVED:20529 CACHE:935 FOLLOWERS:13 FOLLOWING:14
GoCN 每日新闻 (2022-01-06)
- Golang profiler 笔记https://github.com/DataDog/go-profiler-notes/blob/main/README.md
- Bundling templates with embedhttps://osinet.fr/go/en/articles/bundling-templates-with-embed/
- Go 通过 Map/Filter/ForEach 等流式 API 高效处理数据https://gocn.vip/topics/20922
- 滴滴夜莺监控发布 v5 正式版,定位 Promet … ⌘ Read more
🧮 USERS:2 FEEDS:6 TWTS:783 ARCHIVED:367823 CACHE:107570 FOLLOWERS:29 FOLLOWING:686
🧮 USERS:1 FEEDS:2 TWTS:106 ARCHIVED:20451 CACHE:902 FOLLOWERS:13 FOLLOWING:14
Ignite Realtime Blog: Openfire 4.5.6 is released
Openfire 4.5.6 has been released, that addresses an annoying issue that was affecting the earlier 4.5.5 release. We’ve updated the bundled log4j library to version 2.17.1 for good measure.
The changelog denotes the two Jira issues closed by this release. You can find Openfire build artifacts available for download [here](https://github.com/igniterealtime/Openfire/rel … ⌘ Read more
GoCN 每日新闻 (2022-01-05)
GoCN 每日新闻 (2022-01-05)- Rust 与 Go: 为何相得益彰https://gocn.vip/topics/20929
- 聊聊 GOLANG 中形形色色的同步原语http://vearne.cc/archives/39631
- Go 泛型的这 3 个核心设计,你都知道吗?https://segmentfault.com/a/1190000041227815
- Go 语言高效率 Web 开发四:分层领域模型和项目结构分层[https://juejin.cn/post/70492 … ⌘ Read more
🧮 USERS:2 FEEDS:6 TWTS:782 ARCHIVED:350606 CACHE:93073 FOLLOWERS:29 FOLLOWING:686
🧮 USERS:1 FEEDS:2 TWTS:105 ARCHIVED:20383 CACHE:874 FOLLOWERS:13 FOLLOWING:14
GoCN 每日新闻(2022-01-04)
GoCN 每日新闻(2022-01-04)- 「GoCN 酷 Go 推荐」go 语言位操作库 — bitsethttps://mp.weixin.qq.com/s/UcuKgKnt4fwrg3c-UHc3sw
- Go 通过 Map/Filter/ForEach 等流式 API 高效处理数据https://mp.weixin.qq.com/s/7ATm_Zu7ib9MXf8ugy3RcA
- 优化 Go 二进制文件的大小[https://prog.world/optimizing-the-size-of-the-go-binary](https://prog.world/optimizing-the-si … ⌘ Read more
🧮 USERS:2 FEEDS:6 TWTS:781 ARCHIVED:328519 CACHE:74103 FOLLOWERS:29 FOLLOWING:686
🧮 USERS:1 FEEDS:2 TWTS:104 ARCHIVED:20305 CACHE:832 FOLLOWERS:13 FOLLOWING:14
Ignite Realtime Blog: Openfire 4.6.7 released (Log4j 2.17.1 only change)
Openfire 4.6.7 has been released with only a single change to bump the bundled log4j library to version 2.17.1. Whilst we do not believe Openfire to be vulnerable to the CVEs associated with the log4j 2.17.0 and 2.17.1 releases, we realize that many folks are running naive security scanners that are simply checking for bundled jar versions.
The [changelog](https://download.igniterealtime.org/open … ⌘ Read more
🧮 USERS:2 FEEDS:6 TWTS:780 ARCHIVED:325882 CACHE:71787 FOLLOWERS:29 FOLLOWING:686
🧮 USERS:1 FEEDS:2 TWTS:103 ARCHIVED:20216 CACHE:822 FOLLOWERS:13 FOLLOWING:14
Excelize 发布 2.5.0 版本,Go 语言 Excel 文档基础库
Excelize 是 Go 语言编写的用于操作 Office Excel 文档基础库,基于 ECMA-376,ISO/IEC 29500 国际标准。可以使用它来读取、写入由 Microsoft Excel™ 2007 及以上版本创建的电子表格文档。支持 XLSX / XLSM / XLTM 等多种文档格式,高度兼容带有样式、图片 … ⌘ Read more
Excelize 2.5.0 Released – Go API for spreadsheet (Excel) #golang #Excel #excelize href=”https://we.loveprivacy.club/search?q=%23spreadsheet”>#spreadsheet**
1 points posted by xuri ⌘ Read more
go 语言位操作库 bitset
bitset库 实现了 bitsets 数据结构,这是一种正整数和布尔值映射关系的结构,它比 map[uint]bool 更高效
bitsets 基本思想是用一个 bit 位来标记某个元素对应的 Value,每一位表示一个数,1 表示存在,0 表示不存在
比如我要表示 1, 3, 7 这 3 个数
- 构造一个空白 bitsets:00000000
- 每位代表的值如下:76543210
- 想要表示的值标记 1:10001010
�� … ⌘ Read more
🧮 USERS:2 FEEDS:6 TWTS:779 ARCHIVED:323798 CACHE:72698 FOLLOWERS:29 FOLLOWING:686
🧮 USERS:1 FEEDS:2 TWTS:102 ARCHIVED:19935 CACHE:795 FOLLOWERS:13 FOLLOWING:14
MMXXII 💙
MMXXII 💙

- https://eli.thegreenplace.net/2021/go-https-servers-with-tls/ Go HTTPS servers with TLS
- https://eli.thegreenplace.net/2020/embedding-in-go-part-3-interfaces-in-structs/ interfaces in structs
- https://mp.weixin.qq.com/s/Ec1nuR5Q_QgaC3FqeX1gLg 最简单的服务响应时长优化方法
- [https: … ⌘ Read more
🧮 USERS:2 FEEDS:6 TWTS:778 ARCHIVED:321908 CACHE:73580 FOLLOWERS:29 FOLLOWING:686
🧮 USERS:1 FEEDS:2 TWTS:101 ARCHIVED:19852 CACHE:786 FOLLOWERS:13 FOLLOWING:14