rpc_util.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. "encoding/binary"
  23. "io"
  24. "io/ioutil"
  25. "math"
  26. "sync"
  27. "time"
  28. "golang.org/x/net/context"
  29. "google.golang.org/grpc/codes"
  30. "google.golang.org/grpc/credentials"
  31. "google.golang.org/grpc/encoding"
  32. "google.golang.org/grpc/metadata"
  33. "google.golang.org/grpc/peer"
  34. "google.golang.org/grpc/stats"
  35. "google.golang.org/grpc/status"
  36. "google.golang.org/grpc/transport"
  37. )
  38. // Compressor defines the interface gRPC uses to compress a message.
  39. type Compressor interface {
  40. // Do compresses p into w.
  41. Do(w io.Writer, p []byte) error
  42. // Type returns the compression algorithm the Compressor uses.
  43. Type() string
  44. }
  45. type gzipCompressor struct {
  46. pool sync.Pool
  47. }
  48. // NewGZIPCompressor creates a Compressor based on GZIP.
  49. func NewGZIPCompressor() Compressor {
  50. return &gzipCompressor{
  51. pool: sync.Pool{
  52. New: func() interface{} {
  53. return gzip.NewWriter(ioutil.Discard)
  54. },
  55. },
  56. }
  57. }
  58. func (c *gzipCompressor) Do(w io.Writer, p []byte) error {
  59. z := c.pool.Get().(*gzip.Writer)
  60. defer c.pool.Put(z)
  61. z.Reset(w)
  62. if _, err := z.Write(p); err != nil {
  63. return err
  64. }
  65. return z.Close()
  66. }
  67. func (c *gzipCompressor) Type() string {
  68. return "gzip"
  69. }
  70. // Decompressor defines the interface gRPC uses to decompress a message.
  71. type Decompressor interface {
  72. // Do reads the data from r and uncompress them.
  73. Do(r io.Reader) ([]byte, error)
  74. // Type returns the compression algorithm the Decompressor uses.
  75. Type() string
  76. }
  77. type gzipDecompressor struct {
  78. pool sync.Pool
  79. }
  80. // NewGZIPDecompressor creates a Decompressor based on GZIP.
  81. func NewGZIPDecompressor() Decompressor {
  82. return &gzipDecompressor{}
  83. }
  84. func (d *gzipDecompressor) Do(r io.Reader) ([]byte, error) {
  85. var z *gzip.Reader
  86. switch maybeZ := d.pool.Get().(type) {
  87. case nil:
  88. newZ, err := gzip.NewReader(r)
  89. if err != nil {
  90. return nil, err
  91. }
  92. z = newZ
  93. case *gzip.Reader:
  94. z = maybeZ
  95. if err := z.Reset(r); err != nil {
  96. d.pool.Put(z)
  97. return nil, err
  98. }
  99. }
  100. defer func() {
  101. z.Close()
  102. d.pool.Put(z)
  103. }()
  104. return ioutil.ReadAll(z)
  105. }
  106. func (d *gzipDecompressor) Type() string {
  107. return "gzip"
  108. }
  109. // callInfo contains all related configuration and information about an RPC.
  110. type callInfo struct {
  111. compressorType string
  112. failFast bool
  113. headerMD metadata.MD
  114. trailerMD metadata.MD
  115. peer *peer.Peer
  116. traceInfo traceInfo // in trace.go
  117. maxReceiveMessageSize *int
  118. maxSendMessageSize *int
  119. creds credentials.PerRPCCredentials
  120. }
  121. func defaultCallInfo() *callInfo {
  122. return &callInfo{failFast: true}
  123. }
  124. // CallOption configures a Call before it starts or extracts information from
  125. // a Call after it completes.
  126. type CallOption interface {
  127. // before is called before the call is sent to any server. If before
  128. // returns a non-nil error, the RPC fails with that error.
  129. before(*callInfo) error
  130. // after is called after the call has completed. after cannot return an
  131. // error, so any failures should be reported via output parameters.
  132. after(*callInfo)
  133. }
  134. // EmptyCallOption does not alter the Call configuration.
  135. // It can be embedded in another structure to carry satellite data for use
  136. // by interceptors.
  137. type EmptyCallOption struct{}
  138. func (EmptyCallOption) before(*callInfo) error { return nil }
  139. func (EmptyCallOption) after(*callInfo) {}
  140. type beforeCall func(c *callInfo) error
  141. func (o beforeCall) before(c *callInfo) error { return o(c) }
  142. func (o beforeCall) after(c *callInfo) {}
  143. type afterCall func(c *callInfo)
  144. func (o afterCall) before(c *callInfo) error { return nil }
  145. func (o afterCall) after(c *callInfo) { o(c) }
  146. // Header returns a CallOptions that retrieves the header metadata
  147. // for a unary RPC.
  148. func Header(md *metadata.MD) CallOption {
  149. return afterCall(func(c *callInfo) {
  150. *md = c.headerMD
  151. })
  152. }
  153. // Trailer returns a CallOptions that retrieves the trailer metadata
  154. // for a unary RPC.
  155. func Trailer(md *metadata.MD) CallOption {
  156. return afterCall(func(c *callInfo) {
  157. *md = c.trailerMD
  158. })
  159. }
  160. // Peer returns a CallOption that retrieves peer information for a
  161. // unary RPC.
  162. func Peer(peer *peer.Peer) CallOption {
  163. return afterCall(func(c *callInfo) {
  164. if c.peer != nil {
  165. *peer = *c.peer
  166. }
  167. })
  168. }
  169. // FailFast configures the action to take when an RPC is attempted on broken
  170. // connections or unreachable servers. If failFast is true, the RPC will fail
  171. // immediately. Otherwise, the RPC client will block the call until a
  172. // connection is available (or the call is canceled or times out) and will
  173. // retry the call if it fails due to a transient error. gRPC will not retry if
  174. // data was written to the wire unless the server indicates it did not process
  175. // the data. Please refer to
  176. // https://github.com/grpc/grpc/blob/master/doc/wait-for-ready.md.
  177. //
  178. // By default, RPCs are "Fail Fast".
  179. func FailFast(failFast bool) CallOption {
  180. return beforeCall(func(c *callInfo) error {
  181. c.failFast = failFast
  182. return nil
  183. })
  184. }
  185. // MaxCallRecvMsgSize returns a CallOption which sets the maximum message size the client can receive.
  186. func MaxCallRecvMsgSize(s int) CallOption {
  187. return beforeCall(func(o *callInfo) error {
  188. o.maxReceiveMessageSize = &s
  189. return nil
  190. })
  191. }
  192. // MaxCallSendMsgSize returns a CallOption which sets the maximum message size the client can send.
  193. func MaxCallSendMsgSize(s int) CallOption {
  194. return beforeCall(func(o *callInfo) error {
  195. o.maxSendMessageSize = &s
  196. return nil
  197. })
  198. }
  199. // PerRPCCredentials returns a CallOption that sets credentials.PerRPCCredentials
  200. // for a call.
  201. func PerRPCCredentials(creds credentials.PerRPCCredentials) CallOption {
  202. return beforeCall(func(c *callInfo) error {
  203. c.creds = creds
  204. return nil
  205. })
  206. }
  207. // UseCompressor returns a CallOption which sets the compressor used when
  208. // sending the request. If WithCompressor is also set, UseCompressor has
  209. // higher priority.
  210. //
  211. // This API is EXPERIMENTAL.
  212. func UseCompressor(name string) CallOption {
  213. return beforeCall(func(c *callInfo) error {
  214. c.compressorType = name
  215. return nil
  216. })
  217. }
  218. // The format of the payload: compressed or not?
  219. type payloadFormat uint8
  220. const (
  221. compressionNone payloadFormat = iota // no compression
  222. compressionMade
  223. )
  224. // parser reads complete gRPC messages from the underlying reader.
  225. type parser struct {
  226. // r is the underlying reader.
  227. // See the comment on recvMsg for the permissible
  228. // error types.
  229. r io.Reader
  230. // The header of a gRPC message. Find more detail
  231. // at https://grpc.io/docs/guides/wire.html.
  232. header [5]byte
  233. }
  234. // recvMsg reads a complete gRPC message from the stream.
  235. //
  236. // It returns the message and its payload (compression/encoding)
  237. // format. The caller owns the returned msg memory.
  238. //
  239. // If there is an error, possible values are:
  240. // * io.EOF, when no messages remain
  241. // * io.ErrUnexpectedEOF
  242. // * of type transport.ConnectionError
  243. // * of type transport.StreamError
  244. // No other error values or types must be returned, which also means
  245. // that the underlying io.Reader must not return an incompatible
  246. // error.
  247. func (p *parser) recvMsg(maxReceiveMessageSize int) (pf payloadFormat, msg []byte, err error) {
  248. if _, err := p.r.Read(p.header[:]); err != nil {
  249. return 0, nil, err
  250. }
  251. pf = payloadFormat(p.header[0])
  252. length := binary.BigEndian.Uint32(p.header[1:])
  253. if length == 0 {
  254. return pf, nil, nil
  255. }
  256. if int64(length) > int64(maxInt) {
  257. return 0, nil, status.Errorf(codes.ResourceExhausted, "grpc: received message larger than max length allowed on current machine (%d vs. %d)", length, maxInt)
  258. }
  259. if int(length) > maxReceiveMessageSize {
  260. return 0, nil, status.Errorf(codes.ResourceExhausted, "grpc: received message larger than max (%d vs. %d)", length, maxReceiveMessageSize)
  261. }
  262. // TODO(bradfitz,zhaoq): garbage. reuse buffer after proto decoding instead
  263. // of making it for each message:
  264. msg = make([]byte, int(length))
  265. if _, err := p.r.Read(msg); err != nil {
  266. if err == io.EOF {
  267. err = io.ErrUnexpectedEOF
  268. }
  269. return 0, nil, err
  270. }
  271. return pf, msg, nil
  272. }
  273. // encode serializes msg and returns a buffer of message header and a buffer of msg.
  274. // If msg is nil, it generates the message header and an empty msg buffer.
  275. // TODO(ddyihai): eliminate extra Compressor parameter.
  276. func encode(c Codec, msg interface{}, cp Compressor, outPayload *stats.OutPayload, compressor encoding.Compressor) ([]byte, []byte, error) {
  277. var (
  278. b []byte
  279. cbuf *bytes.Buffer
  280. )
  281. const (
  282. payloadLen = 1
  283. sizeLen = 4
  284. )
  285. if msg != nil {
  286. var err error
  287. b, err = c.Marshal(msg)
  288. if err != nil {
  289. return nil, nil, status.Errorf(codes.Internal, "grpc: error while marshaling: %v", err.Error())
  290. }
  291. if outPayload != nil {
  292. outPayload.Payload = msg
  293. // TODO truncate large payload.
  294. outPayload.Data = b
  295. outPayload.Length = len(b)
  296. }
  297. if compressor != nil || cp != nil {
  298. cbuf = new(bytes.Buffer)
  299. // Has compressor, check Compressor is set by UseCompressor first.
  300. if compressor != nil {
  301. z, _ := compressor.Compress(cbuf)
  302. if _, err := z.Write(b); err != nil {
  303. return nil, nil, status.Errorf(codes.Internal, "grpc: error while compressing: %v", err.Error())
  304. }
  305. z.Close()
  306. } else {
  307. // If Compressor is not set by UseCompressor, use default Compressor
  308. if err := cp.Do(cbuf, b); err != nil {
  309. return nil, nil, status.Errorf(codes.Internal, "grpc: error while compressing: %v", err.Error())
  310. }
  311. }
  312. b = cbuf.Bytes()
  313. }
  314. }
  315. if uint(len(b)) > math.MaxUint32 {
  316. return nil, nil, status.Errorf(codes.ResourceExhausted, "grpc: message too large (%d bytes)", len(b))
  317. }
  318. bufHeader := make([]byte, payloadLen+sizeLen)
  319. if compressor != nil || cp != nil {
  320. bufHeader[0] = byte(compressionMade)
  321. } else {
  322. bufHeader[0] = byte(compressionNone)
  323. }
  324. // Write length of b into buf
  325. binary.BigEndian.PutUint32(bufHeader[payloadLen:], uint32(len(b)))
  326. if outPayload != nil {
  327. outPayload.WireLength = payloadLen + sizeLen + len(b)
  328. }
  329. return bufHeader, b, nil
  330. }
  331. func checkRecvPayload(pf payloadFormat, recvCompress string, haveCompressor bool) *status.Status {
  332. switch pf {
  333. case compressionNone:
  334. case compressionMade:
  335. if recvCompress == "" || recvCompress == encoding.Identity {
  336. return status.New(codes.Internal, "grpc: compressed flag set with identity or empty encoding")
  337. }
  338. if !haveCompressor {
  339. return status.Newf(codes.Unimplemented, "grpc: Decompressor is not installed for grpc-encoding %q", recvCompress)
  340. }
  341. default:
  342. return status.Newf(codes.Internal, "grpc: received unexpected payload format %d", pf)
  343. }
  344. return nil
  345. }
  346. // For the two compressor parameters, both should not be set, but if they are,
  347. // dc takes precedence over compressor.
  348. // TODO(dfawley): wrap the old compressor/decompressor using the new API?
  349. func recv(p *parser, c Codec, s *transport.Stream, dc Decompressor, m interface{}, maxReceiveMessageSize int, inPayload *stats.InPayload, compressor encoding.Compressor) error {
  350. pf, d, err := p.recvMsg(maxReceiveMessageSize)
  351. if err != nil {
  352. return err
  353. }
  354. if inPayload != nil {
  355. inPayload.WireLength = len(d)
  356. }
  357. if st := checkRecvPayload(pf, s.RecvCompress(), compressor != nil || dc != nil); st != nil {
  358. return st.Err()
  359. }
  360. if pf == compressionMade {
  361. // To match legacy behavior, if the decompressor is set by WithDecompressor or RPCDecompressor,
  362. // use this decompressor as the default.
  363. if dc != nil {
  364. d, err = dc.Do(bytes.NewReader(d))
  365. if err != nil {
  366. return status.Errorf(codes.Internal, "grpc: failed to decompress the received message %v", err)
  367. }
  368. } else {
  369. dcReader, err := compressor.Decompress(bytes.NewReader(d))
  370. if err != nil {
  371. return status.Errorf(codes.Internal, "grpc: failed to decompress the received message %v", err)
  372. }
  373. d, err = ioutil.ReadAll(dcReader)
  374. if err != nil {
  375. return status.Errorf(codes.Internal, "grpc: failed to decompress the received message %v", err)
  376. }
  377. }
  378. }
  379. if len(d) > maxReceiveMessageSize {
  380. // TODO: Revisit the error code. Currently keep it consistent with java
  381. // implementation.
  382. return status.Errorf(codes.ResourceExhausted, "grpc: received message larger than max (%d vs. %d)", len(d), maxReceiveMessageSize)
  383. }
  384. if err := c.Unmarshal(d, m); err != nil {
  385. return status.Errorf(codes.Internal, "grpc: failed to unmarshal the received message %v", err)
  386. }
  387. if inPayload != nil {
  388. inPayload.RecvTime = time.Now()
  389. inPayload.Payload = m
  390. // TODO truncate large payload.
  391. inPayload.Data = d
  392. inPayload.Length = len(d)
  393. }
  394. return nil
  395. }
  396. type rpcInfo struct {
  397. failfast bool
  398. }
  399. type rpcInfoContextKey struct{}
  400. func newContextWithRPCInfo(ctx context.Context, failfast bool) context.Context {
  401. return context.WithValue(ctx, rpcInfoContextKey{}, &rpcInfo{failfast: failfast})
  402. }
  403. func rpcInfoFromContext(ctx context.Context) (s *rpcInfo, ok bool) {
  404. s, ok = ctx.Value(rpcInfoContextKey{}).(*rpcInfo)
  405. return
  406. }
  407. // Code returns the error code for err if it was produced by the rpc system.
  408. // Otherwise, it returns codes.Unknown.
  409. //
  410. // Deprecated: use status.FromError and Code method instead.
  411. func Code(err error) codes.Code {
  412. if s, ok := status.FromError(err); ok {
  413. return s.Code()
  414. }
  415. return codes.Unknown
  416. }
  417. // ErrorDesc returns the error description of err if it was produced by the rpc system.
  418. // Otherwise, it returns err.Error() or empty string when err is nil.
  419. //
  420. // Deprecated: use status.FromError and Message method instead.
  421. func ErrorDesc(err error) string {
  422. if s, ok := status.FromError(err); ok {
  423. return s.Message()
  424. }
  425. return err.Error()
  426. }
  427. // Errorf returns an error containing an error code and a description;
  428. // Errorf returns nil if c is OK.
  429. //
  430. // Deprecated: use status.Errorf instead.
  431. func Errorf(c codes.Code, format string, a ...interface{}) error {
  432. return status.Errorf(c, format, a...)
  433. }
  434. // The SupportPackageIsVersion variables are referenced from generated protocol
  435. // buffer files to ensure compatibility with the gRPC version used. The latest
  436. // support package version is 5.
  437. //
  438. // Older versions are kept for compatibility. They may be removed if
  439. // compatibility cannot be maintained.
  440. //
  441. // These constants should not be referenced from any other code.
  442. const (
  443. SupportPackageIsVersion3 = true
  444. SupportPackageIsVersion4 = true
  445. SupportPackageIsVersion5 = true
  446. )
  447. // Version is the current grpc version.
  448. const Version = "1.9.2"
  449. const grpcUA = "grpc-go/" + Version