MCPcopy
hub / github.com/cortexlabs/cortex / ServeHTTP

Method ServeHTTP

pkg/activator/handler.go:48–75  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

46}
47
48func (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
77func (h *Handler) proxyRequest(w http.ResponseWriter, r *http.Request) error {
78 target := r.Header.Get(consts.CortexTargetServiceHeader)

Callers 6

proxyRequestMethod · 0.45
PanicMiddlewareFunction · 0.45
ClientIDMiddlewareFunction · 0.45
AWSAuthMiddlewareFunction · 0.45

Calls 5

proxyRequestMethod · 0.95
ErrorFunction · 0.92
hasCortexProbeHeaderFunction · 0.85
ErrorMethod · 0.80
TryMethod · 0.65

Tested by 1