Shutdown will attempt to gracefully shutdown. This entails waiting for all requests to complete, and shutting down the MCP server proxier. TODO: add tests.
(ctx context.Context)
| 312 | // complete, and shutting down the MCP server proxier. |
| 313 | // TODO: add tests. |
| 314 | func (b *RequestBridge) Shutdown(ctx context.Context) error { |
| 315 | var err error |
| 316 | b.shutdownOnce.Do(func() { |
| 317 | // Prevent any new requests from being accepted. |
| 318 | close(b.closed) |
| 319 | |
| 320 | // Wait for inflight requests to complete or context cancellation. |
| 321 | done := make(chan struct{}) |
| 322 | go func() { |
| 323 | b.inflightWG.Wait() |
| 324 | close(done) |
| 325 | }() |
| 326 | |
| 327 | select { |
| 328 | case <-ctx.Done(): |
| 329 | // Cancel all inflight requests, if any are still running. |
| 330 | b.logger.Debug(ctx, "shutdown context canceled; canceling inflight requests", slog.Error(ctx.Err())) |
| 331 | b.inflightCancel() |
| 332 | <-done |
| 333 | err = ctx.Err() |
| 334 | case <-done: |
| 335 | } |
| 336 | |
| 337 | if b.mcpProxy != nil { |
| 338 | // It's ok that we reuse the ctx here even if it's done, since the |
| 339 | // Shutdown method will just immediately use the more aggressive close |
| 340 | // since the ctx is already expired. |
| 341 | err = multierror.Append(err, b.mcpProxy.Shutdown(ctx)) |
| 342 | } |
| 343 | }) |
| 344 | |
| 345 | return err |
| 346 | } |
| 347 | |
| 348 | func (b *RequestBridge) InflightRequests() int32 { |
| 349 | return b.inflightReqs.Load() |