# rpc 错误：代码 = DeadlineExceeded 描述 = 上下文超过截止时间

- **ID:** `go/grpc-deadline-exceeded-context`
- **领域:** go
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在 RPC 完成前，客户端或服务端的上下文截止时间已到。常见于服务端处理缓慢、网络拥塞，或相对于处理逻辑而言截止时间设置过短。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| google.golang.org/grpc v1.40+ | active | — | — |

## 解决方案

1. **** (85% 成功率)
   ```
   ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
resp, err := client.Do(ctx, req)
if status.Code(err) == codes.DeadlineExceeded {
    // retry with backoff
}
   ```
2. **** (80% 成功率)
   ```
   // measure handler latency, then set client deadline > p99
ctx, cancel := context.WithTimeout(ctx, p99Latency*2)
defer cancel()
   ```

## 无效尝试

- **** — If the server is genuinely overloaded, tight retries amplify load and worsen latency, causing more timeouts. No jitter means thundering herd. (80% 失败率)
- **** — Unbounded calls leak goroutines and hold connections indefinitely when the server hangs, eventually exhausting resources. (75% 失败率)
