// 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) }) }
// 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.") }) }
// 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") }) }