Search code examples
gogrpcrpcgrpc-pythongrpc-go

How To Measure The Size of gRPC Response


I am trying come up with better ways to deal with the 4mb message size limit in grpc. I need a way to measure the size of the grpc response received on client side. When the response exceeds 4mb limit grpc shows a error message like:

could not greet: rpc error: code = ResourceExhausted desc = grpc: received message larger than max (74000087 vs. 4194304)

with "74000087" being the actual size of the response. How is this calculated? Is there a way to get this value?

I have gone through multiple articles on this topic but could find nothing? Can someone please help? Thanks.

My implementation is using golang


Solution

  • I figured out a way to do this. The solution is to encode the gRPC response into the byte stream, and then calculate the size using binary.Size()

    func GetGRPCResponseSize(val interface{}, desc string) (int, error) {
    
        var buff bytes.Buffer
        enc := gob.NewEncoder(&buff)
        err := enc.Encode(val)
        if err != nil {
            log.Error("encode error:", err)
            return 0, err
        }
        return binary.Size(buff.Bytes()), nil
    }