# http: 响应写入超时：请求在 30 秒后超时

- **ID:** `go/http-response-writer-timeout`
- **领域:** go
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

服务器写入超时在写入响应体期间到期，导致连接突然关闭。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.18 | active | — | — |
| 1.21 | active | — | — |

## 解决方案

1. **** (80% 成功率)
   ```
   Set a reasonable WriteTimeout and handle errors from Write: if _, err := w.Write(data); err != nil { return }
   ```
2. **** (75% 成功率)
   ```
   Use http.TimeoutHandler to wrap handlers and return a timeout response: http.TimeoutHandler(handler, 30*time.Second, "timeout")
   ```

## 无效尝试

- **** — May cause slow clients to hold connections indefinitely, leading to resource exhaustion. (70% 失败率)
- **** — Writes will fail silently; data is corrupted and the connection is already closed. (90% 失败率)
