How To Measure The Size of gRPC Response
Asked Answered
A

2

5

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

Arquit answered 9/12, 2019 at 9:27 Comment(0)
A
3

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
}
Arquit answered 11/12, 2019 at 8:36 Comment(2)
This is not a precise answer. You are just predicting what would be the compression, by doing a gob encoding offline. There is no guarantee that this would be the size of the network payload.Barbarossa
Is there any Java equivalent?Valetudinary
N
6

Computing message size

As per the proto docs,

func Size(m Message) int

Size returns the size in bytes of the wire-format encoding of m.

gRPC message size limits

Also, it might be useful in your case to consider setting grpc.MaxCallRecvMsgSize as a grpc.CallOption to manage limits.

Neighborhood answered 5/6, 2020 at 19:57 Comment(0)
A
3

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
}
Arquit answered 11/12, 2019 at 8:36 Comment(2)
This is not a precise answer. You are just predicting what would be the compression, by doing a gob encoding offline. There is no guarantee that this would be the size of the network payload.Barbarossa
Is there any Java equivalent?Valetudinary

© 2022 - 2024 — McMap. All rights reserved.