MCPcopy
hub / github.com/grpc/grpc-go / Encode

Method Encode

preloader.go:42–82  ·  preloader.go::PreparedMsg.Encode

Encode marshalls and compresses the message using the codec and compressor for the stream.

(s Stream, msg any)

Source from the content-addressed store, hash-verified

40
41// Encode marshalls and compresses the message using the codec and compressor for the stream.
42func (p *PreparedMsg) Encode(s Stream, msg any) error {
43 ctx := s.Context()
44 rpcInfo, ok := rpcInfoFromContext(ctx)
45 if !ok {
46 return status.Errorf(codes.Internal, "grpc: unable to get rpcInfo")
47 }
48
49 // check if the context has the relevant information to prepareMsg
50 if rpcInfo.preloaderInfo.codec == nil {
51 return status.Errorf(codes.Internal, "grpc: rpcInfo.preloaderInfo.codec is nil")
52 }
53
54 // prepare the msg
55 data, err := encode(rpcInfo.preloaderInfo.codec, msg)
56 if err != nil {
57 return err
58 }
59
60 materializedData := data.Materialize()
61 data.Free()
62 p.encodedData = mem.BufferSlice{mem.SliceBuffer(materializedData)}
63
64 // TODO: it should be possible to grab the bufferPool from the underlying
65 // stream implementation with a type cast to its actual type (such as
66 // addrConnStream) and accessing the buffer pool directly.
67 var compData mem.BufferSlice
68 compData, p.pf, err = compress(p.encodedData, rpcInfo.preloaderInfo.cp, rpcInfo.preloaderInfo.comp, mem.DefaultBufferPool())
69 if err != nil {
70 return err
71 }
72
73 if p.pf.isCompressed() {
74 materializedCompData := compData.Materialize()
75 compData.Free()
76 compData = mem.BufferSlice{mem.SliceBuffer(materializedCompData)}
77 }
78
79 p.hdr, p.payload = msgHeader(p.encodedData, compData, p.pf)
80
81 return nil
82}

Callers 9

StreamingCallMethod · 0.95
testPreloaderClientSendFunction · 0.95
prepareMessagesFunction · 0.45
afterFunction · 0.45
retrieveSnapshotFunction · 0.45
statsTagRPCMethod · 0.45

Calls 12

MaterializeMethod · 0.95
FreeMethod · 0.95
ErrorfFunction · 0.92
SliceBufferTypeAlias · 0.92
DefaultBufferPoolFunction · 0.92
rpcInfoFromContextFunction · 0.85
encodeFunction · 0.85
compressFunction · 0.85
msgHeaderFunction · 0.85
isCompressedMethod · 0.80
ContextMethod · 0.65
FreeMethod · 0.65

Tested by 3

testPreloaderClientSendFunction · 0.76