登录
首页 >  Golang >  Go问答

速率限制特定端点

来源:stackoverflow

时间:2024-04-11 09:18:35 380浏览 收藏

今天golang学习网给大家带来了《速率限制特定端点》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

问题内容

我是 golang 新手,正在开发我的第一个 api。我有两个端点,我只想对其中之一进行速率限制。我找到了一个有用的教程来帮助我入门,并且我的方法基于该教程,认识到这种方法将限制我的两个端点的速率:

var limiter = rate.newlimiter(rate.every((1*time.hour)/3), 1)

func limit(next http.handler) http.handler {
    return http.handlerfunc(func(res http.responsewriter, req *http.request) {
        if limiter.allow() == false {
            http.error(res, http.statustext(429), http.statustoomanyrequests)
            return
        }
        next.servehttp(res, req)
    })
}

func main() {
    mux := http.newservemux()
    mux.handlefunc("/", createnewtoken)
    mux.handlefunc("/notify", sendpushnotificationtoalltokens)

    log.fatal(http.listenandservetls(":5050", "localhost.crt", "localhost.key", limit(mux)))
}

我研究了http.handle和http.handlefunc之间的区别,并天真地相信我可以用http.handlefunc代替http.handle。这种方法是完全有缺陷的,因为 handlerfunc 中包含的逻辑永远不会执行:

var limiter = rate.NewLimiter(rate.Every(1*time.Hour/3), 1)

func limit(next http.HandlerFunc) http.HandlerFunc {
    return func(res http.ResponseWriter, req *http.Request) {
        if limiter.Allow() == false {
            http.Error(res, http.StatusText(429), http.StatusTooManyRequests)
            return
        }
        next.ServeHTTP(res, req)
    }
}

func main() {
    //mux := http.NewServeMux()
    http.HandleFunc("/", createNewToken)
    http.HandleFunc("/notify", sendPushNotificationToAllTokens)

    // attempt to only rate limit the /notify endpoint 
    log.Fatal(http.ListenAndServeTLS(":5050", "localhost.crt", "localhost.key", limit(sendPushNotificationToAllTokens)))

任何人都可以解释为什么这不起作用,以及我如何解决这个问题以仅对特定端点进行速率限制?


正确答案


使用普通 http.handlerhttp.hanlderfunc 之间的区别在这里并不重要。 http.handlefunc 只是将常规函数转换为 http.handler 的一种方法 - 它本质上与 limit 的原始版本执行相同的操作。

您的 limit 实现看起来都很好;可能第二个更好,因为它更简单。相反,问题出在 main 中。当您调用 http.listenandservetls 并为最终参数提供值时,它请求仅将您作为最终参数传入的处理程序用作根请求处理程序。除非您传入 nil 作为最后一个参数,否则对 http.handle()http.handlefunc() 的任何调用都将被忽略。

您想要做的是将 limit 应用于您想要限制的特定处理程序。为此,您有两种选择。首先,您可以像第一个代码片段中一样使用 servemux

func main() {
    mux := http.newservemux()
    mux.handlefunc("/", createnewtoken)
    // limit only the handler for "/notify".
    mux.handlefunc("/notify", limit(sendpushnotificationtoalltokens))

    // don't limit the whole mux.
    log.fatal(http.listenandservetls(":5050", "localhost.crt", "localhost.key", mux))
}

或者,您可以执行类似于第二个代码片段的操作,但将 nil 作为最后一个参数传递给 http.listenandservetls,以便使用默认的 http.servemux,这意味着对 http.handlefunc() 的调用将受到尊重:

func main() {
    http.HandleFunc("/", createNewToken)
    // Limit only the handler for "/notify".
    http.HandleFunc("/notify", limit(sendPushNotificationToAllTokens))

    // Pass in nil here so that http.DefaultServeMux is used.
    log.Fatal(http.ListenAndServeTLS(":5050", "localhost.crt", "localhost.key", nil))
}

对于简单的应用程序,第一种方法可能就很好。对于更复杂的事情,我建议使用后面的方法,因为如果您打开多个服务器或执行其他更复杂的事情,它会起作用。

今天关于《速率限制特定端点》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>