rpc_util.go 27 KB

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