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