ServeHTTP exposes the internal http.Handler, which has all [Provider]s' routes registered. It also tracks inflight requests.
(rw http.ResponseWriter, r *http.Request)
| 286 | // ServeHTTP exposes the internal http.Handler, which has all [Provider]s' routes registered. |
| 287 | // It also tracks inflight requests. |
| 288 | func (b *RequestBridge) ServeHTTP(rw http.ResponseWriter, r *http.Request) { |
| 289 | select { |
| 290 | case <-b.closed: |
| 291 | http.Error(rw, "server closed", http.StatusInternalServerError) |
| 292 | return |
| 293 | default: |
| 294 | } |
| 295 | |
| 296 | // We want to abide by the context passed in without losing any of its |
| 297 | // functionality, but we still want to link our shutdown context to each |
| 298 | // request. |
| 299 | ctx := mergeContexts(r.Context(), b.inflightCtx) |
| 300 | |
| 301 | b.inflightReqs.Add(1) |
| 302 | b.inflightWG.Add(1) |
| 303 | defer func() { |
| 304 | b.inflightReqs.Add(-1) |
| 305 | b.inflightWG.Done() |
| 306 | }() |
| 307 | |
| 308 | b.mux.ServeHTTP(rw, r.WithContext(ctx)) |
| 309 | } |
| 310 | |
| 311 | // Shutdown will attempt to gracefully shutdown. This entails waiting for all requests to |
| 312 | // complete, and shutting down the MCP server proxier. |