podman
978 строк · 29.9 Кб
1/*
2*
3* Copyright 2014 gRPC authors.
4*
5* Licensed under the Apache License, Version 2.0 (the "License");
6* you may not use this file except in compliance with the License.
7* You may obtain a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS,
13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14* See the License for the specific language governing permissions and
15* limitations under the License.
16*
17*/
18
19package grpc
20
21import (
22"bytes"
23"compress/gzip"
24"context"
25"encoding/binary"
26"fmt"
27"io"
28"math"
29"strings"
30"sync"
31"time"
32
33"google.golang.org/grpc/codes"
34"google.golang.org/grpc/credentials"
35"google.golang.org/grpc/encoding"
36"google.golang.org/grpc/encoding/proto"
37"google.golang.org/grpc/internal/transport"
38"google.golang.org/grpc/metadata"
39"google.golang.org/grpc/peer"
40"google.golang.org/grpc/stats"
41"google.golang.org/grpc/status"
42)
43
44// Compressor defines the interface gRPC uses to compress a message.
45//
46// Deprecated: use package encoding.
47type Compressor interface {
48// Do compresses p into w.
49Do(w io.Writer, p []byte) error
50// Type returns the compression algorithm the Compressor uses.
51Type() string
52}
53
54type gzipCompressor struct {
55pool sync.Pool
56}
57
58// NewGZIPCompressor creates a Compressor based on GZIP.
59//
60// Deprecated: use package encoding/gzip.
61func NewGZIPCompressor() Compressor {
62c, _ := NewGZIPCompressorWithLevel(gzip.DefaultCompression)
63return c
64}
65
66// NewGZIPCompressorWithLevel is like NewGZIPCompressor but specifies the gzip compression level instead
67// of assuming DefaultCompression.
68//
69// The error returned will be nil if the level is valid.
70//
71// Deprecated: use package encoding/gzip.
72func NewGZIPCompressorWithLevel(level int) (Compressor, error) {
73if level < gzip.DefaultCompression || level > gzip.BestCompression {
74return nil, fmt.Errorf("grpc: invalid compression level: %d", level)
75}
76return &gzipCompressor{
77pool: sync.Pool{
78New: func() any {
79w, err := gzip.NewWriterLevel(io.Discard, level)
80if err != nil {
81panic(err)
82}
83return w
84},
85},
86}, nil
87}
88
89func (c *gzipCompressor) Do(w io.Writer, p []byte) error {
90z := c.pool.Get().(*gzip.Writer)
91defer c.pool.Put(z)
92z.Reset(w)
93if _, err := z.Write(p); err != nil {
94return err
95}
96return z.Close()
97}
98
99func (c *gzipCompressor) Type() string {
100return "gzip"
101}
102
103// Decompressor defines the interface gRPC uses to decompress a message.
104//
105// Deprecated: use package encoding.
106type Decompressor interface {
107// Do reads the data from r and uncompress them.
108Do(r io.Reader) ([]byte, error)
109// Type returns the compression algorithm the Decompressor uses.
110Type() string
111}
112
113type gzipDecompressor struct {
114pool sync.Pool
115}
116
117// NewGZIPDecompressor creates a Decompressor based on GZIP.
118//
119// Deprecated: use package encoding/gzip.
120func NewGZIPDecompressor() Decompressor {
121return &gzipDecompressor{}
122}
123
124func (d *gzipDecompressor) Do(r io.Reader) ([]byte, error) {
125var z *gzip.Reader
126switch maybeZ := d.pool.Get().(type) {
127case nil:
128newZ, err := gzip.NewReader(r)
129if err != nil {
130return nil, err
131}
132z = newZ
133case *gzip.Reader:
134z = maybeZ
135if err := z.Reset(r); err != nil {
136d.pool.Put(z)
137return nil, err
138}
139}
140
141defer func() {
142z.Close()
143d.pool.Put(z)
144}()
145return io.ReadAll(z)
146}
147
148func (d *gzipDecompressor) Type() string {
149return "gzip"
150}
151
152// callInfo contains all related configuration and information about an RPC.
153type callInfo struct {
154compressorType string
155failFast bool
156maxReceiveMessageSize *int
157maxSendMessageSize *int
158creds credentials.PerRPCCredentials
159contentSubtype string
160codec baseCodec
161maxRetryRPCBufferSize int
162onFinish []func(err error)
163}
164
165func defaultCallInfo() *callInfo {
166return &callInfo{
167failFast: true,
168maxRetryRPCBufferSize: 256 * 1024, // 256KB
169}
170}
171
172// CallOption configures a Call before it starts or extracts information from
173// a Call after it completes.
174type CallOption interface {
175// before is called before the call is sent to any server. If before
176// returns a non-nil error, the RPC fails with that error.
177before(*callInfo) error
178
179// after is called after the call has completed. after cannot return an
180// error, so any failures should be reported via output parameters.
181after(*callInfo, *csAttempt)
182}
183
184// EmptyCallOption does not alter the Call configuration.
185// It can be embedded in another structure to carry satellite data for use
186// by interceptors.
187type EmptyCallOption struct{}
188
189func (EmptyCallOption) before(*callInfo) error { return nil }
190func (EmptyCallOption) after(*callInfo, *csAttempt) {}
191
192// StaticMethod returns a CallOption which specifies that a call is being made
193// to a method that is static, which means the method is known at compile time
194// and doesn't change at runtime. This can be used as a signal to stats plugins
195// that this method is safe to include as a key to a measurement.
196func StaticMethod() CallOption {
197return StaticMethodCallOption{}
198}
199
200// StaticMethodCallOption is a CallOption that specifies that a call comes
201// from a static method.
202type StaticMethodCallOption struct {
203EmptyCallOption
204}
205
206// Header returns a CallOptions that retrieves the header metadata
207// for a unary RPC.
208func Header(md *metadata.MD) CallOption {
209return HeaderCallOption{HeaderAddr: md}
210}
211
212// HeaderCallOption is a CallOption for collecting response header metadata.
213// The metadata field will be populated *after* the RPC completes.
214//
215// # Experimental
216//
217// Notice: This type is EXPERIMENTAL and may be changed or removed in a
218// later release.
219type HeaderCallOption struct {
220HeaderAddr *metadata.MD
221}
222
223func (o HeaderCallOption) before(c *callInfo) error { return nil }
224func (o HeaderCallOption) after(c *callInfo, attempt *csAttempt) {
225*o.HeaderAddr, _ = attempt.s.Header()
226}
227
228// Trailer returns a CallOptions that retrieves the trailer metadata
229// for a unary RPC.
230func Trailer(md *metadata.MD) CallOption {
231return TrailerCallOption{TrailerAddr: md}
232}
233
234// TrailerCallOption is a CallOption for collecting response trailer metadata.
235// The metadata field will be populated *after* the RPC completes.
236//
237// # Experimental
238//
239// Notice: This type is EXPERIMENTAL and may be changed or removed in a
240// later release.
241type TrailerCallOption struct {
242TrailerAddr *metadata.MD
243}
244
245func (o TrailerCallOption) before(c *callInfo) error { return nil }
246func (o TrailerCallOption) after(c *callInfo, attempt *csAttempt) {
247*o.TrailerAddr = attempt.s.Trailer()
248}
249
250// Peer returns a CallOption that retrieves peer information for a unary RPC.
251// The peer field will be populated *after* the RPC completes.
252func Peer(p *peer.Peer) CallOption {
253return PeerCallOption{PeerAddr: p}
254}
255
256// PeerCallOption is a CallOption for collecting the identity of the remote
257// peer. The peer field will be populated *after* the RPC completes.
258//
259// # Experimental
260//
261// Notice: This type is EXPERIMENTAL and may be changed or removed in a
262// later release.
263type PeerCallOption struct {
264PeerAddr *peer.Peer
265}
266
267func (o PeerCallOption) before(c *callInfo) error { return nil }
268func (o PeerCallOption) after(c *callInfo, attempt *csAttempt) {
269if x, ok := peer.FromContext(attempt.s.Context()); ok {
270*o.PeerAddr = *x
271}
272}
273
274// WaitForReady configures the action to take when an RPC is attempted on broken
275// connections or unreachable servers. If waitForReady is false and the
276// connection is in the TRANSIENT_FAILURE state, the RPC will fail
277// immediately. Otherwise, the RPC client will block the call until a
278// connection is available (or the call is canceled or times out) and will
279// retry the call if it fails due to a transient error. gRPC will not retry if
280// data was written to the wire unless the server indicates it did not process
281// the data. Please refer to
282// https://github.com/grpc/grpc/blob/master/doc/wait-for-ready.md.
283//
284// By default, RPCs don't "wait for ready".
285func WaitForReady(waitForReady bool) CallOption {
286return FailFastCallOption{FailFast: !waitForReady}
287}
288
289// FailFast is the opposite of WaitForReady.
290//
291// Deprecated: use WaitForReady.
292func FailFast(failFast bool) CallOption {
293return FailFastCallOption{FailFast: failFast}
294}
295
296// FailFastCallOption is a CallOption for indicating whether an RPC should fail
297// fast or not.
298//
299// # Experimental
300//
301// Notice: This type is EXPERIMENTAL and may be changed or removed in a
302// later release.
303type FailFastCallOption struct {
304FailFast bool
305}
306
307func (o FailFastCallOption) before(c *callInfo) error {
308c.failFast = o.FailFast
309return nil
310}
311func (o FailFastCallOption) after(c *callInfo, attempt *csAttempt) {}
312
313// OnFinish returns a CallOption that configures a callback to be called when
314// the call completes. The error passed to the callback is the status of the
315// RPC, and may be nil. The onFinish callback provided will only be called once
316// by gRPC. This is mainly used to be used by streaming interceptors, to be
317// notified when the RPC completes along with information about the status of
318// the RPC.
319//
320// # Experimental
321//
322// Notice: This API is EXPERIMENTAL and may be changed or removed in a
323// later release.
324func OnFinish(onFinish func(err error)) CallOption {
325return OnFinishCallOption{
326OnFinish: onFinish,
327}
328}
329
330// OnFinishCallOption is CallOption that indicates a callback to be called when
331// the call completes.
332//
333// # Experimental
334//
335// Notice: This type is EXPERIMENTAL and may be changed or removed in a
336// later release.
337type OnFinishCallOption struct {
338OnFinish func(error)
339}
340
341func (o OnFinishCallOption) before(c *callInfo) error {
342c.onFinish = append(c.onFinish, o.OnFinish)
343return nil
344}
345
346func (o OnFinishCallOption) after(c *callInfo, attempt *csAttempt) {}
347
348// MaxCallRecvMsgSize returns a CallOption which sets the maximum message size
349// in bytes the client can receive. If this is not set, gRPC uses the default
350// 4MB.
351func MaxCallRecvMsgSize(bytes int) CallOption {
352return MaxRecvMsgSizeCallOption{MaxRecvMsgSize: bytes}
353}
354
355// MaxRecvMsgSizeCallOption is a CallOption that indicates the maximum message
356// size in bytes the client can receive.
357//
358// # Experimental
359//
360// Notice: This type is EXPERIMENTAL and may be changed or removed in a
361// later release.
362type MaxRecvMsgSizeCallOption struct {
363MaxRecvMsgSize int
364}
365
366func (o MaxRecvMsgSizeCallOption) before(c *callInfo) error {
367c.maxReceiveMessageSize = &o.MaxRecvMsgSize
368return nil
369}
370func (o MaxRecvMsgSizeCallOption) after(c *callInfo, attempt *csAttempt) {}
371
372// MaxCallSendMsgSize returns a CallOption which sets the maximum message size
373// in bytes the client can send. If this is not set, gRPC uses the default
374// `math.MaxInt32`.
375func MaxCallSendMsgSize(bytes int) CallOption {
376return MaxSendMsgSizeCallOption{MaxSendMsgSize: bytes}
377}
378
379// MaxSendMsgSizeCallOption is a CallOption that indicates the maximum message
380// size in bytes the client can send.
381//
382// # Experimental
383//
384// Notice: This type is EXPERIMENTAL and may be changed or removed in a
385// later release.
386type MaxSendMsgSizeCallOption struct {
387MaxSendMsgSize int
388}
389
390func (o MaxSendMsgSizeCallOption) before(c *callInfo) error {
391c.maxSendMessageSize = &o.MaxSendMsgSize
392return nil
393}
394func (o MaxSendMsgSizeCallOption) after(c *callInfo, attempt *csAttempt) {}
395
396// PerRPCCredentials returns a CallOption that sets credentials.PerRPCCredentials
397// for a call.
398func PerRPCCredentials(creds credentials.PerRPCCredentials) CallOption {
399return PerRPCCredsCallOption{Creds: creds}
400}
401
402// PerRPCCredsCallOption is a CallOption that indicates the per-RPC
403// credentials to use for the call.
404//
405// # Experimental
406//
407// Notice: This type is EXPERIMENTAL and may be changed or removed in a
408// later release.
409type PerRPCCredsCallOption struct {
410Creds credentials.PerRPCCredentials
411}
412
413func (o PerRPCCredsCallOption) before(c *callInfo) error {
414c.creds = o.Creds
415return nil
416}
417func (o PerRPCCredsCallOption) after(c *callInfo, attempt *csAttempt) {}
418
419// UseCompressor returns a CallOption which sets the compressor used when
420// sending the request. If WithCompressor is also set, UseCompressor has
421// higher priority.
422//
423// # Experimental
424//
425// Notice: This API is EXPERIMENTAL and may be changed or removed in a
426// later release.
427func UseCompressor(name string) CallOption {
428return CompressorCallOption{CompressorType: name}
429}
430
431// CompressorCallOption is a CallOption that indicates the compressor to use.
432//
433// # Experimental
434//
435// Notice: This type is EXPERIMENTAL and may be changed or removed in a
436// later release.
437type CompressorCallOption struct {
438CompressorType string
439}
440
441func (o CompressorCallOption) before(c *callInfo) error {
442c.compressorType = o.CompressorType
443return nil
444}
445func (o CompressorCallOption) after(c *callInfo, attempt *csAttempt) {}
446
447// CallContentSubtype returns a CallOption that will set the content-subtype
448// for a call. For example, if content-subtype is "json", the Content-Type over
449// the wire will be "application/grpc+json". The content-subtype is converted
450// to lowercase before being included in Content-Type. See Content-Type on
451// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests for
452// more details.
453//
454// If ForceCodec is not also used, the content-subtype will be used to look up
455// the Codec to use in the registry controlled by RegisterCodec. See the
456// documentation on RegisterCodec for details on registration. The lookup of
457// content-subtype is case-insensitive. If no such Codec is found, the call
458// will result in an error with code codes.Internal.
459//
460// If ForceCodec is also used, that Codec will be used for all request and
461// response messages, with the content-subtype set to the given contentSubtype
462// here for requests.
463func CallContentSubtype(contentSubtype string) CallOption {
464return ContentSubtypeCallOption{ContentSubtype: strings.ToLower(contentSubtype)}
465}
466
467// ContentSubtypeCallOption is a CallOption that indicates the content-subtype
468// used for marshaling messages.
469//
470// # Experimental
471//
472// Notice: This type is EXPERIMENTAL and may be changed or removed in a
473// later release.
474type ContentSubtypeCallOption struct {
475ContentSubtype string
476}
477
478func (o ContentSubtypeCallOption) before(c *callInfo) error {
479c.contentSubtype = o.ContentSubtype
480return nil
481}
482func (o ContentSubtypeCallOption) after(c *callInfo, attempt *csAttempt) {}
483
484// ForceCodec returns a CallOption that will set codec to be used for all
485// request and response messages for a call. The result of calling Name() will
486// be used as the content-subtype after converting to lowercase, unless
487// CallContentSubtype is also used.
488//
489// See Content-Type on
490// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests for
491// more details. Also see the documentation on RegisterCodec and
492// CallContentSubtype for more details on the interaction between Codec and
493// content-subtype.
494//
495// This function is provided for advanced users; prefer to use only
496// CallContentSubtype to select a registered codec instead.
497//
498// # Experimental
499//
500// Notice: This API is EXPERIMENTAL and may be changed or removed in a
501// later release.
502func ForceCodec(codec encoding.Codec) CallOption {
503return ForceCodecCallOption{Codec: codec}
504}
505
506// ForceCodecCallOption is a CallOption that indicates the codec used for
507// marshaling messages.
508//
509// # Experimental
510//
511// Notice: This type is EXPERIMENTAL and may be changed or removed in a
512// later release.
513type ForceCodecCallOption struct {
514Codec encoding.Codec
515}
516
517func (o ForceCodecCallOption) before(c *callInfo) error {
518c.codec = o.Codec
519return nil
520}
521func (o ForceCodecCallOption) after(c *callInfo, attempt *csAttempt) {}
522
523// CallCustomCodec behaves like ForceCodec, but accepts a grpc.Codec instead of
524// an encoding.Codec.
525//
526// Deprecated: use ForceCodec instead.
527func CallCustomCodec(codec Codec) CallOption {
528return CustomCodecCallOption{Codec: codec}
529}
530
531// CustomCodecCallOption is a CallOption that indicates the codec used for
532// marshaling messages.
533//
534// # Experimental
535//
536// Notice: This type is EXPERIMENTAL and may be changed or removed in a
537// later release.
538type CustomCodecCallOption struct {
539Codec Codec
540}
541
542func (o CustomCodecCallOption) before(c *callInfo) error {
543c.codec = o.Codec
544return nil
545}
546func (o CustomCodecCallOption) after(c *callInfo, attempt *csAttempt) {}
547
548// MaxRetryRPCBufferSize returns a CallOption that limits the amount of memory
549// used for buffering this RPC's requests for retry purposes.
550//
551// # Experimental
552//
553// Notice: This API is EXPERIMENTAL and may be changed or removed in a
554// later release.
555func MaxRetryRPCBufferSize(bytes int) CallOption {
556return MaxRetryRPCBufferSizeCallOption{bytes}
557}
558
559// MaxRetryRPCBufferSizeCallOption is a CallOption indicating the amount of
560// memory to be used for caching this RPC for retry purposes.
561//
562// # Experimental
563//
564// Notice: This type is EXPERIMENTAL and may be changed or removed in a
565// later release.
566type MaxRetryRPCBufferSizeCallOption struct {
567MaxRetryRPCBufferSize int
568}
569
570func (o MaxRetryRPCBufferSizeCallOption) before(c *callInfo) error {
571c.maxRetryRPCBufferSize = o.MaxRetryRPCBufferSize
572return nil
573}
574func (o MaxRetryRPCBufferSizeCallOption) after(c *callInfo, attempt *csAttempt) {}
575
576// The format of the payload: compressed or not?
577type payloadFormat uint8
578
579const (
580compressionNone payloadFormat = 0 // no compression
581compressionMade payloadFormat = 1 // compressed
582)
583
584// parser reads complete gRPC messages from the underlying reader.
585type parser struct {
586// r is the underlying reader.
587// See the comment on recvMsg for the permissible
588// error types.
589r io.Reader
590
591// The header of a gRPC message. Find more detail at
592// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md
593header [5]byte
594
595// recvBufferPool is the pool of shared receive buffers.
596recvBufferPool SharedBufferPool
597}
598
599// recvMsg reads a complete gRPC message from the stream.
600//
601// It returns the message and its payload (compression/encoding)
602// format. The caller owns the returned msg memory.
603//
604// If there is an error, possible values are:
605// - io.EOF, when no messages remain
606// - io.ErrUnexpectedEOF
607// - of type transport.ConnectionError
608// - an error from the status package
609//
610// No other error values or types must be returned, which also means
611// that the underlying io.Reader must not return an incompatible
612// error.
613func (p *parser) recvMsg(maxReceiveMessageSize int) (pf payloadFormat, msg []byte, err error) {
614if _, err := p.r.Read(p.header[:]); err != nil {
615return 0, nil, err
616}
617
618pf = payloadFormat(p.header[0])
619length := binary.BigEndian.Uint32(p.header[1:])
620
621if length == 0 {
622return pf, nil, nil
623}
624if int64(length) > int64(maxInt) {
625return 0, nil, status.Errorf(codes.ResourceExhausted, "grpc: received message larger than max length allowed on current machine (%d vs. %d)", length, maxInt)
626}
627if int(length) > maxReceiveMessageSize {
628return 0, nil, status.Errorf(codes.ResourceExhausted, "grpc: received message larger than max (%d vs. %d)", length, maxReceiveMessageSize)
629}
630msg = p.recvBufferPool.Get(int(length))
631if _, err := p.r.Read(msg); err != nil {
632if err == io.EOF {
633err = io.ErrUnexpectedEOF
634}
635return 0, nil, err
636}
637return pf, msg, nil
638}
639
640// encode serializes msg and returns a buffer containing the message, or an
641// error if it is too large to be transmitted by grpc. If msg is nil, it
642// generates an empty message.
643func encode(c baseCodec, msg any) ([]byte, error) {
644if msg == nil { // NOTE: typed nils will not be caught by this check
645return nil, nil
646}
647b, err := c.Marshal(msg)
648if err != nil {
649return nil, status.Errorf(codes.Internal, "grpc: error while marshaling: %v", err.Error())
650}
651if uint(len(b)) > math.MaxUint32 {
652return nil, status.Errorf(codes.ResourceExhausted, "grpc: message too large (%d bytes)", len(b))
653}
654return b, nil
655}
656
657// compress returns the input bytes compressed by compressor or cp.
658// If both compressors are nil, or if the message has zero length, returns nil,
659// indicating no compression was done.
660//
661// TODO(dfawley): eliminate cp parameter by wrapping Compressor in an encoding.Compressor.
662func compress(in []byte, cp Compressor, compressor encoding.Compressor) ([]byte, error) {
663if compressor == nil && cp == nil {
664return nil, nil
665}
666if len(in) == 0 {
667return nil, nil
668}
669wrapErr := func(err error) error {
670return status.Errorf(codes.Internal, "grpc: error while compressing: %v", err.Error())
671}
672cbuf := &bytes.Buffer{}
673if compressor != nil {
674z, err := compressor.Compress(cbuf)
675if err != nil {
676return nil, wrapErr(err)
677}
678if _, err := z.Write(in); err != nil {
679return nil, wrapErr(err)
680}
681if err := z.Close(); err != nil {
682return nil, wrapErr(err)
683}
684} else {
685if err := cp.Do(cbuf, in); err != nil {
686return nil, wrapErr(err)
687}
688}
689return cbuf.Bytes(), nil
690}
691
692const (
693payloadLen = 1
694sizeLen = 4
695headerLen = payloadLen + sizeLen
696)
697
698// msgHeader returns a 5-byte header for the message being transmitted and the
699// payload, which is compData if non-nil or data otherwise.
700func msgHeader(data, compData []byte) (hdr []byte, payload []byte) {
701hdr = make([]byte, headerLen)
702if compData != nil {
703hdr[0] = byte(compressionMade)
704data = compData
705} else {
706hdr[0] = byte(compressionNone)
707}
708
709// Write length of payload into buf
710binary.BigEndian.PutUint32(hdr[payloadLen:], uint32(len(data)))
711return hdr, data
712}
713
714func outPayload(client bool, msg any, data, payload []byte, t time.Time) *stats.OutPayload {
715return &stats.OutPayload{
716Client: client,
717Payload: msg,
718Data: data,
719Length: len(data),
720WireLength: len(payload) + headerLen,
721CompressedLength: len(payload),
722SentTime: t,
723}
724}
725
726func checkRecvPayload(pf payloadFormat, recvCompress string, haveCompressor bool) *status.Status {
727switch pf {
728case compressionNone:
729case compressionMade:
730if recvCompress == "" || recvCompress == encoding.Identity {
731return status.New(codes.Internal, "grpc: compressed flag set with identity or empty encoding")
732}
733if !haveCompressor {
734return status.Newf(codes.Unimplemented, "grpc: Decompressor is not installed for grpc-encoding %q", recvCompress)
735}
736default:
737return status.Newf(codes.Internal, "grpc: received unexpected payload format %d", pf)
738}
739return nil
740}
741
742type payloadInfo struct {
743compressedLength int // The compressed length got from wire.
744uncompressedBytes []byte
745}
746
747func recvAndDecompress(p *parser, s *transport.Stream, dc Decompressor, maxReceiveMessageSize int, payInfo *payloadInfo, compressor encoding.Compressor) ([]byte, error) {
748pf, buf, err := p.recvMsg(maxReceiveMessageSize)
749if err != nil {
750return nil, err
751}
752if payInfo != nil {
753payInfo.compressedLength = len(buf)
754}
755
756if st := checkRecvPayload(pf, s.RecvCompress(), compressor != nil || dc != nil); st != nil {
757return nil, st.Err()
758}
759
760var size int
761if pf == compressionMade {
762// To match legacy behavior, if the decompressor is set by WithDecompressor or RPCDecompressor,
763// use this decompressor as the default.
764if dc != nil {
765buf, err = dc.Do(bytes.NewReader(buf))
766size = len(buf)
767} else {
768buf, size, err = decompress(compressor, buf, maxReceiveMessageSize)
769}
770if err != nil {
771return nil, status.Errorf(codes.Internal, "grpc: failed to decompress the received message: %v", err)
772}
773if size > maxReceiveMessageSize {
774// TODO: Revisit the error code. Currently keep it consistent with java
775// implementation.
776return nil, status.Errorf(codes.ResourceExhausted, "grpc: received message after decompression larger than max (%d vs. %d)", size, maxReceiveMessageSize)
777}
778}
779return buf, nil
780}
781
782// Using compressor, decompress d, returning data and size.
783// Optionally, if data will be over maxReceiveMessageSize, just return the size.
784func decompress(compressor encoding.Compressor, d []byte, maxReceiveMessageSize int) ([]byte, int, error) {
785dcReader, err := compressor.Decompress(bytes.NewReader(d))
786if err != nil {
787return nil, 0, err
788}
789if sizer, ok := compressor.(interface {
790DecompressedSize(compressedBytes []byte) int
791}); ok {
792if size := sizer.DecompressedSize(d); size >= 0 {
793if size > maxReceiveMessageSize {
794return nil, size, nil
795}
796// size is used as an estimate to size the buffer, but we
797// will read more data if available.
798// +MinRead so ReadFrom will not reallocate if size is correct.
799buf := bytes.NewBuffer(make([]byte, 0, size+bytes.MinRead))
800bytesRead, err := buf.ReadFrom(io.LimitReader(dcReader, int64(maxReceiveMessageSize)+1))
801return buf.Bytes(), int(bytesRead), err
802}
803}
804// Read from LimitReader with limit max+1. So if the underlying
805// reader is over limit, the result will be bigger than max.
806d, err = io.ReadAll(io.LimitReader(dcReader, int64(maxReceiveMessageSize)+1))
807return d, len(d), err
808}
809
810// For the two compressor parameters, both should not be set, but if they are,
811// dc takes precedence over compressor.
812// TODO(dfawley): wrap the old compressor/decompressor using the new API?
813func recv(p *parser, c baseCodec, s *transport.Stream, dc Decompressor, m any, maxReceiveMessageSize int, payInfo *payloadInfo, compressor encoding.Compressor) error {
814buf, err := recvAndDecompress(p, s, dc, maxReceiveMessageSize, payInfo, compressor)
815if err != nil {
816return err
817}
818if err := c.Unmarshal(buf, m); err != nil {
819return status.Errorf(codes.Internal, "grpc: failed to unmarshal the received message: %v", err)
820}
821if payInfo != nil {
822payInfo.uncompressedBytes = buf
823} else {
824p.recvBufferPool.Put(&buf)
825}
826return nil
827}
828
829// Information about RPC
830type rpcInfo struct {
831failfast bool
832preloaderInfo *compressorInfo
833}
834
835// Information about Preloader
836// Responsible for storing codec, and compressors
837// If stream (s) has context s.Context which stores rpcInfo that has non nil
838// pointers to codec, and compressors, then we can use preparedMsg for Async message prep
839// and reuse marshalled bytes
840type compressorInfo struct {
841codec baseCodec
842cp Compressor
843comp encoding.Compressor
844}
845
846type rpcInfoContextKey struct{}
847
848func newContextWithRPCInfo(ctx context.Context, failfast bool, codec baseCodec, cp Compressor, comp encoding.Compressor) context.Context {
849return context.WithValue(ctx, rpcInfoContextKey{}, &rpcInfo{
850failfast: failfast,
851preloaderInfo: &compressorInfo{
852codec: codec,
853cp: cp,
854comp: comp,
855},
856})
857}
858
859func rpcInfoFromContext(ctx context.Context) (s *rpcInfo, ok bool) {
860s, ok = ctx.Value(rpcInfoContextKey{}).(*rpcInfo)
861return
862}
863
864// Code returns the error code for err if it was produced by the rpc system.
865// Otherwise, it returns codes.Unknown.
866//
867// Deprecated: use status.Code instead.
868func Code(err error) codes.Code {
869return status.Code(err)
870}
871
872// ErrorDesc returns the error description of err if it was produced by the rpc system.
873// Otherwise, it returns err.Error() or empty string when err is nil.
874//
875// Deprecated: use status.Convert and Message method instead.
876func ErrorDesc(err error) string {
877return status.Convert(err).Message()
878}
879
880// Errorf returns an error containing an error code and a description;
881// Errorf returns nil if c is OK.
882//
883// Deprecated: use status.Errorf instead.
884func Errorf(c codes.Code, format string, a ...any) error {
885return status.Errorf(c, format, a...)
886}
887
888var errContextCanceled = status.Error(codes.Canceled, context.Canceled.Error())
889var errContextDeadline = status.Error(codes.DeadlineExceeded, context.DeadlineExceeded.Error())
890
891// toRPCErr converts an error into an error from the status package.
892func toRPCErr(err error) error {
893switch err {
894case nil, io.EOF:
895return err
896case context.DeadlineExceeded:
897return errContextDeadline
898case context.Canceled:
899return errContextCanceled
900case io.ErrUnexpectedEOF:
901return status.Error(codes.Internal, err.Error())
902}
903
904switch e := err.(type) {
905case transport.ConnectionError:
906return status.Error(codes.Unavailable, e.Desc)
907case *transport.NewStreamError:
908return toRPCErr(e.Err)
909}
910
911if _, ok := status.FromError(err); ok {
912return err
913}
914
915return status.Error(codes.Unknown, err.Error())
916}
917
918// setCallInfoCodec should only be called after CallOptions have been applied.
919func setCallInfoCodec(c *callInfo) error {
920if c.codec != nil {
921// codec was already set by a CallOption; use it, but set the content
922// subtype if it is not set.
923if c.contentSubtype == "" {
924// c.codec is a baseCodec to hide the difference between grpc.Codec and
925// encoding.Codec (Name vs. String method name). We only support
926// setting content subtype from encoding.Codec to avoid a behavior
927// change with the deprecated version.
928if ec, ok := c.codec.(encoding.Codec); ok {
929c.contentSubtype = strings.ToLower(ec.Name())
930}
931}
932return nil
933}
934
935if c.contentSubtype == "" {
936// No codec specified in CallOptions; use proto by default.
937c.codec = encoding.GetCodec(proto.Name)
938return nil
939}
940
941// c.contentSubtype is already lowercased in CallContentSubtype
942c.codec = encoding.GetCodec(c.contentSubtype)
943if c.codec == nil {
944return status.Errorf(codes.Internal, "no codec registered for content-subtype %s", c.contentSubtype)
945}
946return nil
947}
948
949// channelzData is used to store channelz related data for ClientConn, addrConn and Server.
950// These fields cannot be embedded in the original structs (e.g. ClientConn), since to do atomic
951// operation on int64 variable on 32-bit machine, user is responsible to enforce memory alignment.
952// Here, by grouping those int64 fields inside a struct, we are enforcing the alignment.
953type channelzData struct {
954callsStarted int64
955callsFailed int64
956callsSucceeded int64
957// lastCallStartedTime stores the timestamp that last call starts. It is of int64 type instead of
958// time.Time since it's more costly to atomically update time.Time variable than int64 variable.
959lastCallStartedTime int64
960}
961
962// The SupportPackageIsVersion variables are referenced from generated protocol
963// buffer files to ensure compatibility with the gRPC version used. The latest
964// support package version is 7.
965//
966// Older versions are kept for compatibility.
967//
968// These constants should not be referenced from any other code.
969const (
970SupportPackageIsVersion3 = true
971SupportPackageIsVersion4 = true
972SupportPackageIsVersion5 = true
973SupportPackageIsVersion6 = true
974SupportPackageIsVersion7 = true
975SupportPackageIsVersion8 = true
976)
977
978const grpcUA = "grpc-go/" + Version
979