7 type limiterHandler struct {
12 func NewRequestLimiter(maxRequests int, handler http.Handler) http.Handler {
13 return &limiterHandler{
14 requests: make(chan struct{}, maxRequests),
19 func (h *limiterHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
21 case h.requests <- struct{}{}:
23 // reached max requests
24 resp.WriteHeader(http.StatusServiceUnavailable)
27 h.handler.ServeHTTP(resp, req)