亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

速率限制特定端點

速率限制特定端點

Go
FFIVE 2023-03-15 14:24:03
我是 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)))誰能解釋為什么這不起作用,以及我如何解決這個問題以僅對特定端點進行速率限制?
查看完整描述

1 回答

?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

使用 plainhttp.Handler和 a之間的區別http.HanlderFunc在這里并不重要。http.HandleFunc只是一種將常規函數轉換為 a 的方法http.Handler- 它本質上與原始版本的limit.


您對limit兩者的實現看起來都不錯;可能第二個更好,因為它更簡單。相反,問題出在main. 當您調用http.ListenAndServeTLS并為最終參數提供值時,它會請求僅將您作為最終參數傳入的處理程序用作根請求處理程序。除非您作為最后一個參數傳入,否則對http.Handle()或的任何調用都會被忽略。http.HandleFunc()nil


您要做的是應用于limit您要限制的特定處理程序。為此,您有兩種選擇。ServeMux首先,您可以在第一個代碼片段中使用like:


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))

}

對于一個簡單的應用程序,第一種方法可能沒問題。對于更復雜的事情,我推薦后一種方法,因為如果您打開多個服務器或做其他更復雜的事情,它就會起作用。


查看完整回答
反對 回復 2023-03-15
  • 1 回答
  • 0 關注
  • 95 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號