# HTTP/2: 从服务器收到错误码为 NO_ERROR 的 GOAWAY 帧，但在优雅关闭完成前连接已关闭

- **ID:** `networking/http2-graceful-shutdown-timeout`
- **领域:** networking
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

HTTP/2 优雅关闭超时发生在客户端收到服务器发送的 GOAWAY 帧但未能在允许时间内完成待处理流时，导致连接突然终止。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| nginx 1.25+ | active | — | — |
| Apache HTTP Server 2.4.58+ | active | — | — |
| gRPC 1.60+ | active | — | — |

## 解决方案

1. ```
   Configure the client to gracefully handle GOAWAY by setting a reasonable grace period: In nginx, add 'http2_recv_timeout 30s;' to the server block to allow pending streams to complete.
   ```
2. ```
   Implement retry logic in the client for failed requests after GOAWAY: In gRPC, use WithWaitForHandshake and retry with backoff.
   ```
3. ```
   Reduce the number of concurrent streams per connection to limit pending work during shutdown: In nginx, set 'http2_max_concurrent_streams 64;'.
   ```

## 无效尝试

- **** — 增加服务器上的 HTTP/2 流超时（例如 http2_max_streams）无法解决客户端无法快速处理待处理请求的问题。 (80% 失败率)
- **** — 完全禁用 HTTP/2 并回退到 HTTP/1.1 可以避免问题，但会失去性能优势，且不是长期解决方案。 (90% 失败率)
- **** — 重启负载均衡器或代理可能会清空连接池，但如果应用程序未正确处理 GOAWAY，超时会再次发生。 (85% 失败率)
