# gRPC 客户端流失败，资源耗尽：待处理请求过多

- **ID:** `communication/grpc-client-stream-failed-with-resource-exhausted`
- **领域:** communication
- **类别:** runtime_error
- **错误码:** `RESOURCE_EXHAUSTED`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

gRPC 客户端侧流控制或并发限制超限，导致通道拒绝新流。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| gRPC 1.54.0 | active | — | — |
| gRPC 1.60.0 | active | — | — |
| gRPC 1.62.0 | active | — | — |

## 解决方案

1. ```
   Configure gRPC client channel with increased max_concurrent_streams and flow control window: In C++, set `ChannelArguments` with `GRPC_ARG_MAX_CONCURRENT_STREAMS` to 1000 and `GRPC_ARG_HTTP2_WRITE_BUFFER_SIZE` to 64KB. In Python, use `grpc.insecure_channel(target, options=[('grpc.max_concurrent_streams', 1000)])`.
   ```
2. ```
   Implement client-side rate limiting with a semaphore to limit in-flight requests: Use `asyncio.Semaphore(100)` in Python or `std::counting_semaphore` in C++ to throttle before sending.
   ```
3. ```
   Enable gRPC client-side keepalive with aggressive timeout to detect dead streams: Set `grpc.keepalive_time_ms` to 10000 and `grpc.keepalive_timeout_ms` to 5000.
   ```

## 无效尝试

- **Increase the gRPC max concurrent streams on the server side only** — The issue is client-side concurrency limits; server-side changes don't help. (80% 失败率)
- **Restart the client process to reset channel state** — Restarting only temporarily clears the queue; the same condition recurs under load. (90% 失败率)
- **Ignore the error and retry indefinitely with backoff** — Without addressing the concurrency limit, retries will keep hitting the same error. (70% 失败率)
