本文整理匯總了Golang中github.com/GoogleCloudPlatform/kubernetes/pkg/util.RateLimiter類的典型用法代碼示例。如果您正苦於以下問題:Golang RateLimiter類的具體用法?Golang RateLimiter怎麽用?Golang RateLimiter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了RateLimiter類的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: RateLimit
// RateLimit uses rl to rate limit accepting requests to 'handler'.
func RateLimit(rl util.RateLimiter, handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if rl.CanAccept() {
handler.ServeHTTP(w, req)
return
}
tooManyRequests(w)
})
}
示例2: RateLimit
// RateLimit uses rl to rate limit accepting requests to 'handler'.
func RateLimit(rl util.RateLimiter, handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if rl.CanAccept() {
handler.ServeHTTP(w, req)
return
}
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(w, "Rate limit exceeded.")
})
}
示例3: RateLimit
// RateLimit uses rl to rate limit accepting requests to 'handler'.
func RateLimit(rl util.RateLimiter, handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if rl.CanAccept() {
handler.ServeHTTP(w, req)
return
}
// Return a 429 status indicating "Too Many Requests"
w.Header().Set("Retry-After", "1")
w.WriteHeader(errors.StatusTooManyRequests)
fmt.Fprintf(w, "Rate limit is 10 QPS or a burst of 200")
})
}