# http: 请求上下文已取消

- **ID:** `go/http-request-context-canceled`
- **领域:** go
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

客户端取消请求或服务器上下文因超时或客户端断开而取消，导致 HTTP 处理器中止。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.17 | active | — | — |
| 1.20 | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Check for context cancellation and return early: if r.Context().Err() != nil { return }
   ```
2. **** (70% 成功率)
   ```
   Use http.Server with ReadTimeout and WriteTimeout properly: &http.Server{ReadTimeout: 10*time.Second, WriteTimeout: 10*time.Second}
   ```

## 无效尝试

- **** — Processing continues but the client is gone; results are wasted and may cause resource leaks. (80% 失败率)
- **** — Long timeouts can lead to resource exhaustion and don't address client-initiated cancellations. (60% 失败率)
