| 34 | } |
| 35 | |
| 36 | func (l *limitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 37 | sem, endpt := l.Check(r) |
| 38 | if sem != nil { |
| 39 | if !sem.TryAcquire(1) { |
| 40 | concurrentLimitedCounter.WithLabelValues(endpt, r.Method).Add(1) |
| 41 | ctx := r.Context() |
| 42 | zlog.Info(ctx). |
| 43 | Str("remote_addr", r.RemoteAddr). |
| 44 | Str("method", r.Method). |
| 45 | Str("request_uri", r.RequestURI). |
| 46 | Int("status", http.StatusTooManyRequests). |
| 47 | Msg("rate limited HTTP request") |
| 48 | |
| 49 | apiError(ctx, w, http.StatusTooManyRequests, "server handling too many requests") |
| 50 | } |
| 51 | defer sem.Release(1) |
| 52 | } |
| 53 | l.Next.ServeHTTP(w, r) |
| 54 | } |