| 28 | ) |
| 29 | |
| 30 | func Handler(breaker *Breaker, next http.Handler) http.HandlerFunc { |
| 31 | return func(w http.ResponseWriter, r *http.Request) { |
| 32 | if probe.IsRequestKubeletProbe(r) || breaker == nil { |
| 33 | next.ServeHTTP(w, r) |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | if err := breaker.Maybe(r.Context(), func() { |
| 38 | next.ServeHTTP(w, r) |
| 39 | }); err != nil { |
| 40 | if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, ErrRequestQueueFull) { |
| 41 | http.Error(w, err.Error(), http.StatusServiceUnavailable) |
| 42 | } else { |
| 43 | w.WriteHeader(http.StatusInternalServerError) |
| 44 | telemetry.Error(err) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |