# gRPC 错误：消息过大（超过 4 MB 限制）

- **ID:** `communication/grpc-message-size-exceeded`
- **领域:** communication
- **类别:** protocol_error
- **错误码:** `RESOURCE_EXHAUSTED`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

gRPC 默认的最大消息大小为 4 MB（发送和接收均适用），任何超过此限制的消息都会导致 RpcError。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| gRPC 1.48.0 | active | — | — |
| gRPC 1.50.0 | active | — | — |
| gRPC 1.54.0 | active | — | — |

## 解决方案

1. ```
   Increase max message size on both client and server: server: server = grpc.server(futures.ThreadPoolExecutor(max_workers=10), options=[('grpc.max_send_message_length', 10 * 1024 * 1024), ('grpc.max_receive_message_length', 10 * 1024 * 1024)]) client: channel = grpc.insecure_channel('localhost:50051', options=[('grpc.max_send_message_length', 10 * 1024 * 1024), ('grpc.max_receive_message_length', 10 * 1024 * 1024)])
   ```
2. ```
   Split the large payload into multiple smaller messages using client-side chunking, then reassemble on server
   ```
3. ```
   Use gRPC streaming (server-side or bidirectional) to send data in chunks without changing limits
   ```

## 无效尝试

- **** — Server still enforces default limit, so server-side deserialization fails (95% 失败率)
- **** — gRPC compression reduces wire size but server still checks uncompressed size (70% 失败率)
- **** — Streaming works but requires protocol change and breaks existing clients; not a direct fix (60% 失败率)
