# rpc 错误：代码 = ResourceExhausted，描述 = grpc：接收到的消息超过最大限制（5242881 > 4194304）

- **ID:** `go/grpc-message-size-exceeded`
- **领域:** go
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

protobuf 消息超过 gRPC 默认 4 MiB 接收上限。常见于大文件上传、批量数组或内嵌二进制块。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.40+ | active | — | — |
| 1.60+ | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Raise the limit on both client and server:
// client
conn, _ := grpc.NewClient(addr,
  grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(16*1024*1024)))
// server
srv := grpc.NewServer(grpc.MaxRecvMsgSize(16*1024*1024))
   ```
2. **** (95% 成功率)
   ```
   Switch large payloads to streaming RPCs:
stream, _ := client.Upload(ctx)
for _, chunk := range chunks { stream.Send(chunk) }
stream.CloseAndRecv()
   ```

## 无效尝试

- **** — gRPC's limit is checked on the uncompressed logical message size in many configs; compression may not help and adds CPU cost. (70% 失败率)
- **** — Deterministic size violation; retries return the same error. (95% 失败率)
