(w http.ResponseWriter, r *http.Request)
| 50 | } |
| 51 | |
| 52 | func (p *ProxyEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 53 | level.Debug(p.logger).Log("msg", "Received request", "path", r.URL.Path, "query", r.URL.RawQuery) |
| 54 | |
| 55 | // Send the same request to all backends. |
| 56 | resCh := make(chan *backendResponse, len(p.backends)) |
| 57 | go p.executeBackendRequests(r, resCh) |
| 58 | |
| 59 | // Wait for the first response that's feasible to be sent back to the client. |
| 60 | downstreamRes := p.waitBackendResponseForDownstream(resCh) |
| 61 | |
| 62 | if downstreamRes.err != nil { |
| 63 | http.Error(w, downstreamRes.err.Error(), http.StatusInternalServerError) |
| 64 | } else { |
| 65 | w.WriteHeader(downstreamRes.status) |
| 66 | if _, err := w.Write(downstreamRes.body); err != nil { |
| 67 | level.Warn(p.logger).Log("msg", "Unable to write response", "err", err) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | p.metrics.responsesTotal.WithLabelValues(downstreamRes.backend.name, r.Method, p.routeName).Inc() |
| 72 | } |
| 73 | |
| 74 | func (p *ProxyEndpoint) executeBackendRequests(r *http.Request, resCh chan *backendResponse) { |
| 75 | responses := make([]*backendResponse, 0, len(p.backends)) |
no test coverage detected