| 46 | } |
| 47 | |
| 48 | func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 49 | if hasCortexProbeHeader(r) { |
| 50 | w.WriteHeader(http.StatusOK) |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | apiName := r.Header.Get(consts.CortexAPINameHeader) |
| 55 | if apiName == "" { |
| 56 | http.Error(w, fmt.Sprintf("missing %s", consts.CortexAPINameHeader), http.StatusInternalServerError) |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | ctx := r.Context() |
| 61 | ctx = context.WithValue(ctx, APINameCtxKey, apiName) |
| 62 | |
| 63 | if err := h.activator.Try(ctx, func() error { |
| 64 | return h.proxyRequest(w, r) |
| 65 | }); err != nil { |
| 66 | h.logger.Errorw("activator try error", zap.Error(err)) |
| 67 | |
| 68 | if stderrors.Is(err, context.DeadlineExceeded) || stderrors.Is(err, proxy.ErrRequestQueueFull) { |
| 69 | http.Error(w, err.Error(), http.StatusServiceUnavailable) |
| 70 | } else { |
| 71 | w.WriteHeader(http.StatusInternalServerError) |
| 72 | telemetry.Error(err) |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func (h *Handler) proxyRequest(w http.ResponseWriter, r *http.Request) error { |
| 78 | target := r.Header.Get(consts.CortexTargetServiceHeader) |