# rpc 错误：code = ResourceExhausted desc = grpc: 收到的消息大于最大值（5242881 对 4194304）

- **ID:** `go/grpc-resource-exhausted-message-too-large`
- **领域:** go
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

gRPC 默认最大接收消息大小为 4MB。服务端或客户端收到的序列化消息超过了该限制。常见于 protobuf 中嵌入的大型 repeated 字段或二进制数据。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| google.golang.org/grpc 1.x | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   // server
s := grpc.NewServer(grpc.MaxRecvMsgSize(16 * 1024 * 1024))
// client
conn, _ := grpc.Dial(addr, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(16*1024*1024)))
   ```
2. **** (85% 成功率)
   ```
   stream, err := client.Upload(ctx)
for _, chunk := range chunks {
    if err := stream.Send(&pb.Chunk{Data: chunk}); err != nil { return err }
}
resp, err := stream.CloseAndRecv()
   ```

## 无效尝试

- **** — The limit applies to the decompressed message size on receive; compression does not change the post-decompression size. (75% 失败率)
- **** — Without a defined chunking protocol (e.g., streaming with sequence numbers), the receiver cannot reconstruct the message. (60% 失败率)
