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

Method SendMsg

stream.go:1711–1792  ·  view source on GitHub ↗
(m any)

Source from the content-addressed store, hash-verified

1709}
1710
1711func (ss *serverStream) SendMsg(m any) (err error) {
1712 defer func() {
1713 if ss.trInfo != nil {
1714 ss.mu.Lock()
1715 if ss.trInfo.tr != nil {
1716 if err == nil {
1717 ss.trInfo.tr.LazyLog(&payload{sent: true, msg: m}, true)
1718 } else {
1719 ss.trInfo.tr.LazyLog(&fmtStringer{"%v", []any{err}}, true)
1720 ss.trInfo.tr.SetError()
1721 }
1722 }
1723 ss.mu.Unlock()
1724 }
1725 if err != nil && err != io.EOF {
1726 st, _ := status.FromError(toRPCErr(err))
1727 ss.s.WriteStatus(st)
1728 // Non-user specified status was sent out. This should be an error
1729 // case (as a server side Cancel maybe).
1730 //
1731 // This is not handled specifically now. User will return a final
1732 // status from the service handler, we will log that error instead.
1733 // This behavior is similar to an interceptor.
1734 }
1735 }()
1736
1737 // Server handler could have set new compressor by calling SetSendCompressor.
1738 // In case it is set, we need to use it for compressing outbound message.
1739 if sendCompressorsName := ss.s.SendCompress(); sendCompressorsName != ss.sendCompressorName {
1740 ss.compressorV1 = encoding.GetCompressor(sendCompressorsName)
1741 ss.sendCompressorName = sendCompressorsName
1742 }
1743
1744 // load hdr, payload, data
1745 hdr, data, payload, pf, err := prepareMsg(m, ss.codec, ss.compressorV0, ss.compressorV1, ss.p.bufferPool)
1746 if err != nil {
1747 return err
1748 }
1749
1750 defer func() {
1751 data.Free()
1752 // only free payload if compression was made, and therefore it is a different set
1753 // of buffers from data.
1754 if pf.isCompressed() {
1755 payload.Free()
1756 }
1757 }()
1758
1759 dataLen := data.Len()
1760 payloadLen := payload.Len()
1761
1762 // TODO(dfawley): should we be checking len(data) instead?
1763 if payloadLen > ss.maxSendMessageSize {
1764 return status.Errorf(codes.ResourceExhausted, "trying to send message larger than max (%d vs. %d)", payloadLen, ss.maxSendMessageSize)
1765 }
1766 if err := ss.s.Write(hdr, payload, &transport.WriteOptions{Last: false}); err != nil {
1767 return toRPCErr(err)
1768 }

Callers

nothing calls this directly

Calls 15

FromErrorFunction · 0.92
GetCompressorFunction · 0.92
ErrorfFunction · 0.92
toRPCErrFunction · 0.85
prepareMsgFunction · 0.85
outPayloadFunction · 0.85
WriteStatusMethod · 0.80
SendCompressMethod · 0.80
isCompressedMethod · 0.80
MaterializeMethod · 0.80
NowMethod · 0.80
LazyLogMethod · 0.65

Tested by

no test coverage detected