# rpc错误：代码=资源耗尽 描述=grpc：接收的消息大小超过最大值（4294967295 vs. 4194304）

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

## 根因

gRPC消息大小超过配置的最大值，通常是由于有效载荷过大或流式数据。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.0 | active | — | — |
| 1.1 | active | — | — |

## 解决方案

1. **Increase the maximum message size on both client and server.** (90% 成功率)
   ```
   // Server side
s := grpc.NewServer(grpc.MaxRecvMsgSize(10*1024*1024), grpc.MaxSendMsgSize(10*1024*1024))
// Client side
conn, err := grpc.Dial("localhost:8080", grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(10*1024*1024), grpc.MaxCallSendMsgSize(10*1024*1024)))
   ```
2. **Implement streaming to handle large data in chunks.** (85% 成功率)
   ```
   Use server streaming RPC to send data in multiple messages, e.g., service MyService { rpc Upload(stream Chunk) returns (UploadStatus); }
   ```

## 无效尝试

- **Ignore the limit and send the same large message again.** — The server will reject it again because the limit is enforced. (100% 失败率)
- **Reduce the message size by splitting manually without adjusting server config.** — Manual splitting may break protocol semantics and increase complexity. (70% 失败率)
