当前位置: > > > > 使用“didip/tollbooth”限制每小时最大请求数
使用“didip/tollbooth”限制每小时最大请求数
来源:stackoverflow
2024-04-25 17:45:34
0浏览
收藏
最近发现不少小伙伴都对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”限制每小时最大请求数》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注米云公众号!
