使用“didip/tollbooth”限制每小时最大请求数
来源:stackoverflow
时间:2024-04-25 17:45:34 311浏览 收藏
最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《使用“didip/tollbooth”限制每小时最大请求数》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~
我对速率限制不熟悉,想要使用收费站来限制 http 请求。
我还阅读了维基百科上的令牌桶算法页面。
对于一个简单的测试应用程序,我希望将最大并发请求数限制为 10
,无论请求 ip 为何,并根据请求 ip 将最大突发大小设置为 3
。
注意:10
和 3
只是为了使速率限制更容易观察。
下面是我的代码,基于 tollbooth
的 github 页面上的示例:
package main import ( "net/http" "time" "github.com/didip/tollbooth/v7" "github.com/didip/tollbooth/v7/limiter" ) func main() { lmt := tollbooth.newlimiter(3, &limiter.expirableoptions{defaultexpirationttl: time.hour}) http.handle("/", tollbooth.limitfunchandler(lmt, hellohandler)) http.listenandserve(":8080", nil) } func hellohandler(w http.responsewriter, req *http.request) { w.write([]byte("hello, world!")) }
我通过快速连续多次运行 curl -i localhost:8080
来测试代码,每当超出我设置的速率限制时,我都会收到 http/1.1 429 too many requests
错误。
以下是我的问题:
如何使用
tollbooth
将最大并发请求数限制为10
之类的内容?这样做有意义吗?我认为确实如此,因为仅基于 ip 的速率限制听起来像是当太多 ip 同时访问服务器时,服务器仍然可能会出现内存不足的情况。我是否正确地接近了速率限制,或者我错过了什么?也许无论负载均衡器与云中的应用程序一起使用,都可以更好地处理这个问题?
更新:这是我基于 woody1193 的答案的工作代码:
package main import ( "net/http" "sync" "time" "github.com/didip/tollbooth/v7" "github.com/didip/tollbooth/v7/limiter" ) func main() { ipLimiter := tollbooth.NewLimiter(3, &limiter.ExpirableOptions{DefaultExpirationTTL: time.Hour}) globalLimiter := NewConcurrentLimiter(10) http.Handle("/", globalLimiter.LimitConcurrentRequests(ipLimiter, HelloHandler)) http.ListenAndServe(":8080", nil) } func HelloHandler(w http.ResponseWriter, req *http.Request) { w.Write([]byte("Hello, World!")) } type ConcurrentLimiter struct { max int current int mut sync.Mutex } func NewConcurrentLimiter(limit int) *ConcurrentLimiter { return &ConcurrentLimiter{ max: limit, } } func (limiter *ConcurrentLimiter) LimitConcurrentRequests(lmt *limiter.Limiter, handler func(http.ResponseWriter, *http.Request)) http.Handler { middle := func(w http.ResponseWriter, r *http.Request) { limiter.mut.Lock() maxHit := limiter.current == limiter.max if maxHit { limiter.mut.Unlock() http.Error(w, http.StatusText(429), http.StatusTooManyRequests) return } limiter.current += 1 limiter.mut.Unlock() defer func() { limiter.mut.Lock() limiter.current -= 1 limiter.mut.Unlock() }() // There's no rate-limit error, serve the next handler. handler(w, r) } return tollbooth.LimitHandler(lmt, http.HandlerFunc(middle)) }
正确答案
收费站似乎不提供您正在寻找的功能。但是,您可以自己推出:
type concurrentlimiter struct { max int current int mut sync.mutex } func newconcurrentlimiter(limit int) *concurrentlimiter { return &concurrentlimiter { max: limit, mut: new(sync.mutex), } } func (limiter *concurrentlimiter) limitconcurrentrequests(lmt *limiter.limiter, next http.handler) http.handler { middle := func(w http.responsewriter, r *http.request) { limiter.mut.lock() maxhit := limiter.current == limiter.max if maxhit { limiter.mut.unlock() httperror := // insert your http error here return } limiter.current += 1 limiter.mut.unlock() defer func() { limiter.mut.lock() limiter.current -= 1 limiter.mut.unlock() }() // there's no rate-limit error, serve the next handler. next.servehttp(w, r) } return tollbooth.limithandler(lmt, http.handlerfunc(middle)) }
然后,在您的设置中您可以执行以下操作:
http.Handle("/", NewConcurrentLimiter(10).LimitConcurrentRequests(HelloHandler))
此代码的工作原理是维护一个值,该值描述 api 当前正在处理的请求数量,并在达到最大值时返回错误。 mutex
用于确保无论并发请求如何,该值都会更新。
由于 tollbooth 处理此类函数的方式(即它不作为中间件运行),我必须将 tollbooth.limiter
注入到我编写的限制器中。
今天关于《使用“didip/tollbooth”限制每小时最大请求数》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习