encode serializes msg and returns a buffer containing the message, or an error if it is too large to be transmitted by grpc. If msg is nil, it generates an empty message.
(c baseCodec, msg any)
| 812 | // error if it is too large to be transmitted by grpc. If msg is nil, it |
| 813 | // generates an empty message. |
| 814 | func encode(c baseCodec, msg any) (mem.BufferSlice, error) { |
| 815 | if msg == nil { // NOTE: typed nils will not be caught by this check |
| 816 | return nil, nil |
| 817 | } |
| 818 | b, err := c.Marshal(msg) |
| 819 | if err != nil { |
| 820 | return nil, status.Errorf(codes.Internal, "grpc: error while marshaling: %v", err.Error()) |
| 821 | } |
| 822 | if bufSize := uint(b.Len()); bufSize > math.MaxUint32 { |
| 823 | b.Free() |
| 824 | return nil, status.Errorf(codes.ResourceExhausted, "grpc: message too large (%d bytes)", bufSize) |
| 825 | } |
| 826 | return b, nil |
| 827 | } |
| 828 | |
| 829 | // compress returns the input bytes compressed by compressor or cp. |
| 830 | // If both compressors are nil, or if the message has zero length, returns nil, |