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)
| 387 | // complete, and shutting down the MCP server proxier. |
| 388 | // TODO: add tests. |
| 389 | func (b *RequestBridge) Shutdown(ctx context.Context) error { |
| 390 | var err error |
| 391 | b.shutdownOnce.Do(func() { |
| 392 | // Prevent any new requests from being accepted. |
| 393 | close(b.closed) |
| 394 | |
| 395 | // Wait for inflight requests to complete or context cancellation. |
| 396 | done := make(chan struct{}) |
| 397 | go func() { |
| 398 | b.inflightWG.Wait() |
| 399 | close(done) |
| 400 | }() |
| 401 | |
| 402 | select { |
| 403 | case <-ctx.Done(): |
| 404 | // Cancel all inflight requests, if any are still running. |
| 405 | b.logger.Debug(ctx, "shutdown context canceled; canceling inflight requests", slog.Error(ctx.Err())) |
| 406 | b.inflightCancel() |
| 407 | <-done |
| 408 | err = ctx.Err() |
| 409 | case <-done: |
| 410 | } |
| 411 | |
| 412 | if b.mcpProxy != nil { |
| 413 | // It's ok that we reuse the ctx here even if it's done, since the |
| 414 | // Shutdown method will just immediately use the more aggressive close |
| 415 | // since the ctx is already expired. |
| 416 | err = multierror.Append(err, b.mcpProxy.Shutdown(ctx)) |
| 417 | } |
| 418 | }) |
| 419 | |
| 420 | return err |
| 421 | } |
| 422 | |
| 423 | func (b *RequestBridge) InflightRequests() int32 { |
| 424 | return b.inflightReqs.Load() |