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

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

## 根因

接收到的消息超过了默认的 4 MiB 最大接收大小（grpc.MaxRecvMsgSize）。gRPC 会拒绝超过配置限制的消息以保护内存。发送和接收的默认值均为 4 MiB。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.x | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Raise the limit deliberately on both ends to a value you can justify:

srv := grpc.NewServer(grpc.MaxRecvMsgSize(16<<20), grpc.MaxSendMsgSize(16<<20))
conn, _ := grpc.Dial(addr, grpc.WithDefaultCallOptions(
    grpc.MaxCallRecvMsgSize(16<<20),
    grpc.MaxCallSendMsgSize(16<<20)))
   ```
2. **** (88% 成功率)
   ```
   For genuinely large payloads, switch to client/server streaming instead of a unary message:

stream, _ := client.Upload(ctx)
for _, chunk := range chunks { stream.Send(&pb.Chunk{Data: chunk}) }
_, err := stream.CloseAndRecv()
   ```

## 无效尝试

- **** — The max-size check is applied to the uncompressed message on the receiving side, so compression does not raise the limit and the error persists. (80% 失败率)
- **** — Allowing unbounded messages exposes the service to OOM from a single malicious or buggy client; the protection exists for a reason. (60% 失败率)
