go resource_error ai_generated true

rpc 错误:code = ResourceExhausted desc = grpc:接收到的消息大于最大值(5242881 vs. 4194304)

rpc error: code = ResourceExhausted desc = grpc: received message larger than max (5242881 vs. 4194304)

ID: go/grpc-resource-exhausted-message-larger-than-max

其他格式: JSON · Markdown 中文 · English
80%修复率
89%置信度
0证据数
2024-02-14首次发现

版本兼容性

版本状态引入弃用备注
1.x active — — —

根因分析

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

English

The received message exceeds the default 4 MiB max receive size (grpc.MaxRecvMsgSize). gRPC rejects messages above the configured limit to protect memory. The default is 4 MiB for both send and receive.

generic

解决方案

  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()

无效尝试

常见但无效的做法:

  1. 80% 失败

    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.

  2. 60% 失败

    Allowing unbounded messages exposes the service to OOM from a single malicious or buggy client; the protection exists for a reason.