rpc_util.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. *
  3. * Copyright 2014, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. package grpc
  34. import (
  35. "bytes"
  36. "compress/gzip"
  37. "encoding/binary"
  38. "io"
  39. "io/ioutil"
  40. "math"
  41. "sync"
  42. "time"
  43. "golang.org/x/net/context"
  44. "google.golang.org/grpc/codes"
  45. "google.golang.org/grpc/credentials"
  46. "google.golang.org/grpc/metadata"
  47. "google.golang.org/grpc/peer"
  48. "google.golang.org/grpc/stats"
  49. "google.golang.org/grpc/status"
  50. "google.golang.org/grpc/transport"
  51. )
  52. // Compressor defines the interface gRPC uses to compress a message.
  53. type Compressor interface {
  54. // Do compresses p into w.
  55. Do(w io.Writer, p []byte) error
  56. // Type returns the compression algorithm the Compressor uses.
  57. Type() string
  58. }
  59. type gzipCompressor struct {
  60. pool sync.Pool
  61. }
  62. // NewGZIPCompressor creates a Compressor based on GZIP.
  63. func NewGZIPCompressor() Compressor {
  64. return &gzipCompressor{
  65. pool: sync.Pool{
  66. New: func() interface{} {
  67. return gzip.NewWriter(ioutil.Discard)
  68. },
  69. },
  70. }
  71. }
  72. func (c *gzipCompressor) Do(w io.Writer, p []byte) error {
  73. z := c.pool.Get().(*gzip.Writer)
  74. z.Reset(w)
  75. if _, err := z.Write(p); err != nil {
  76. return err
  77. }
  78. return z.Close()
  79. }
  80. func (c *gzipCompressor) Type() string {
  81. return "gzip"
  82. }
  83. // Decompressor defines the interface gRPC uses to decompress a message.
  84. type Decompressor interface {
  85. // Do reads the data from r and uncompress them.
  86. Do(r io.Reader) ([]byte, error)
  87. // Type returns the compression algorithm the Decompressor uses.
  88. Type() string
  89. }
  90. type gzipDecompressor struct {
  91. pool sync.Pool
  92. }
  93. // NewGZIPDecompressor creates a Decompressor based on GZIP.
  94. func NewGZIPDecompressor() Decompressor {
  95. return &gzipDecompressor{}
  96. }
  97. func (d *gzipDecompressor) Do(r io.Reader) ([]byte, error) {
  98. var z *gzip.Reader
  99. switch maybeZ := d.pool.Get().(type) {
  100. case nil:
  101. newZ, err := gzip.NewReader(r)
  102. if err != nil {
  103. return nil, err
  104. }
  105. z = newZ
  106. case *gzip.Reader:
  107. z = maybeZ
  108. if err := z.Reset(r); err != nil {
  109. d.pool.Put(z)
  110. return nil, err
  111. }
  112. }
  113. defer func() {
  114. z.Close()
  115. d.pool.Put(z)
  116. }()
  117. return ioutil.ReadAll(z)
  118. }
  119. func (d *gzipDecompressor) Type() string {
  120. return "gzip"
  121. }
  122. // callInfo contains all related configuration and information about an RPC.
  123. type callInfo struct {
  124. failFast bool
  125. headerMD metadata.MD
  126. trailerMD metadata.MD
  127. peer *peer.Peer
  128. traceInfo traceInfo // in trace.go
  129. maxReceiveMessageSize *int
  130. maxSendMessageSize *int
  131. creds credentials.PerRPCCredentials
  132. }
  133. var defaultCallInfo = callInfo{failFast: true}
  134. // CallOption configures a Call before it starts or extracts information from
  135. // a Call after it completes.
  136. type CallOption interface {
  137. // before is called before the call is sent to any server. If before
  138. // returns a non-nil error, the RPC fails with that error.
  139. before(*callInfo) error
  140. // after is called after the call has completed. after cannot return an
  141. // error, so any failures should be reported via output parameters.
  142. after(*callInfo)
  143. }
  144. // EmptyCallOption does not alter the Call configuration.
  145. // It can be embedded in another structure to carry satellite data for use
  146. // by interceptors.
  147. type EmptyCallOption struct{}
  148. func (EmptyCallOption) before(*callInfo) error { return nil }
  149. func (EmptyCallOption) after(*callInfo) {}
  150. type beforeCall func(c *callInfo) error
  151. func (o beforeCall) before(c *callInfo) error { return o(c) }
  152. func (o beforeCall) after(c *callInfo) {}
  153. type afterCall func(c *callInfo)
  154. func (o afterCall) before(c *callInfo) error { return nil }
  155. func (o afterCall) after(c *callInfo) { o(c) }
  156. // Header returns a CallOptions that retrieves the header metadata
  157. // for a unary RPC.
  158. func Header(md *metadata.MD) CallOption {
  159. return afterCall(func(c *callInfo) {
  160. *md = c.headerMD
  161. })
  162. }
  163. // Trailer returns a CallOptions that retrieves the trailer metadata
  164. // for a unary RPC.
  165. func Trailer(md *metadata.MD) CallOption {
  166. return afterCall(func(c *callInfo) {
  167. *md = c.trailerMD
  168. })
  169. }
  170. // Peer returns a CallOption that retrieves peer information for a
  171. // unary RPC.
  172. func Peer(peer *peer.Peer) CallOption {
  173. return afterCall(func(c *callInfo) {
  174. if c.peer != nil {
  175. *peer = *c.peer
  176. }
  177. })
  178. }
  179. // FailFast configures the action to take when an RPC is attempted on broken
  180. // connections or unreachable servers. If failfast is true, the RPC will fail
  181. // immediately. Otherwise, the RPC client will block the call until a
  182. // connection is available (or the call is canceled or times out) and will retry
  183. // the call if it fails due to a transient error. Please refer to
  184. // https://github.com/grpc/grpc/blob/master/doc/wait-for-ready.md.
  185. // Note: failFast is default to true.
  186. func FailFast(failFast bool) CallOption {
  187. return beforeCall(func(c *callInfo) error {
  188. c.failFast = failFast
  189. return nil
  190. })
  191. }
  192. // MaxCallRecvMsgSize returns a CallOption which sets the maximum message size the client can receive.
  193. func MaxCallRecvMsgSize(s int) CallOption {
  194. return beforeCall(func(o *callInfo) error {
  195. o.maxReceiveMessageSize = &s
  196. return nil
  197. })
  198. }
  199. // MaxCallSendMsgSize returns a CallOption which sets the maximum message size the client can send.
  200. func MaxCallSendMsgSize(s int) CallOption {
  201. return beforeCall(func(o *callInfo) error {
  202. o.maxSendMessageSize = &s
  203. return nil
  204. })
  205. }
  206. // PerRPCCredentials returns a CallOption that sets credentials.PerRPCCredentials
  207. // for a call.
  208. func PerRPCCredentials(creds credentials.PerRPCCredentials) CallOption {
  209. return beforeCall(func(c *callInfo) error {
  210. c.creds = creds
  211. return nil
  212. })
  213. }
  214. // The format of the payload: compressed or not?
  215. type payloadFormat uint8
  216. const (
  217. compressionNone payloadFormat = iota // no compression
  218. compressionMade
  219. )
  220. // parser reads complete gRPC messages from the underlying reader.
  221. type parser struct {
  222. // r is the underlying reader.
  223. // See the comment on recvMsg for the permissible
  224. // error types.
  225. r io.Reader
  226. // The header of a gRPC message. Find more detail
  227. // at http://www.grpc.io/docs/guides/wire.html.
  228. header [5]byte
  229. }
  230. // recvMsg reads a complete gRPC message from the stream.
  231. //
  232. // It returns the message and its payload (compression/encoding)
  233. // format. The caller owns the returned msg memory.
  234. //
  235. // If there is an error, possible values are:
  236. // * io.EOF, when no messages remain
  237. // * io.ErrUnexpectedEOF
  238. // * of type transport.ConnectionError
  239. // * of type transport.StreamError
  240. // No other error values or types must be returned, which also means
  241. // that the underlying io.Reader must not return an incompatible
  242. // error.
  243. func (p *parser) recvMsg(maxReceiveMessageSize int) (pf payloadFormat, msg []byte, err error) {
  244. if _, err := p.r.Read(p.header[:]); err != nil {
  245. return 0, nil, err
  246. }
  247. pf = payloadFormat(p.header[0])
  248. length := binary.BigEndian.Uint32(p.header[1:])
  249. if length == 0 {
  250. return pf, nil, nil
  251. }
  252. if length > uint32(maxReceiveMessageSize) {
  253. return 0, nil, Errorf(codes.ResourceExhausted, "grpc: received message larger than max (%d vs. %d)", length, maxReceiveMessageSize)
  254. }
  255. // TODO(bradfitz,zhaoq): garbage. reuse buffer after proto decoding instead
  256. // of making it for each message:
  257. msg = make([]byte, int(length))
  258. if _, err := p.r.Read(msg); err != nil {
  259. if err == io.EOF {
  260. err = io.ErrUnexpectedEOF
  261. }
  262. return 0, nil, err
  263. }
  264. return pf, msg, nil
  265. }
  266. // encode serializes msg and prepends the message header. If msg is nil, it
  267. // generates the message header of 0 message length.
  268. func encode(c Codec, msg interface{}, cp Compressor, cbuf *bytes.Buffer, outPayload *stats.OutPayload) ([]byte, error) {
  269. var (
  270. b []byte
  271. length uint
  272. )
  273. if msg != nil {
  274. var err error
  275. // TODO(zhaoq): optimize to reduce memory alloc and copying.
  276. b, err = c.Marshal(msg)
  277. if err != nil {
  278. return nil, Errorf(codes.Internal, "grpc: error while marshaling: %v", err.Error())
  279. }
  280. if outPayload != nil {
  281. outPayload.Payload = msg
  282. // TODO truncate large payload.
  283. outPayload.Data = b
  284. outPayload.Length = len(b)
  285. }
  286. if cp != nil {
  287. if err := cp.Do(cbuf, b); err != nil {
  288. return nil, Errorf(codes.Internal, "grpc: error while compressing: %v", err.Error())
  289. }
  290. b = cbuf.Bytes()
  291. }
  292. length = uint(len(b))
  293. }
  294. if length > math.MaxUint32 {
  295. return nil, Errorf(codes.ResourceExhausted, "grpc: message too large (%d bytes)", length)
  296. }
  297. const (
  298. payloadLen = 1
  299. sizeLen = 4
  300. )
  301. var buf = make([]byte, payloadLen+sizeLen+len(b))
  302. // Write payload format
  303. if cp == nil {
  304. buf[0] = byte(compressionNone)
  305. } else {
  306. buf[0] = byte(compressionMade)
  307. }
  308. // Write length of b into buf
  309. binary.BigEndian.PutUint32(buf[1:], uint32(length))
  310. // Copy encoded msg to buf
  311. copy(buf[5:], b)
  312. if outPayload != nil {
  313. outPayload.WireLength = len(buf)
  314. }
  315. return buf, nil
  316. }
  317. func checkRecvPayload(pf payloadFormat, recvCompress string, dc Decompressor) error {
  318. switch pf {
  319. case compressionNone:
  320. case compressionMade:
  321. if dc == nil || recvCompress != dc.Type() {
  322. return Errorf(codes.Unimplemented, "grpc: Decompressor is not installed for grpc-encoding %q", recvCompress)
  323. }
  324. default:
  325. return Errorf(codes.Internal, "grpc: received unexpected payload format %d", pf)
  326. }
  327. return nil
  328. }
  329. func recv(p *parser, c Codec, s *transport.Stream, dc Decompressor, m interface{}, maxReceiveMessageSize int, inPayload *stats.InPayload) error {
  330. pf, d, err := p.recvMsg(maxReceiveMessageSize)
  331. if err != nil {
  332. return err
  333. }
  334. if inPayload != nil {
  335. inPayload.WireLength = len(d)
  336. }
  337. if err := checkRecvPayload(pf, s.RecvCompress(), dc); err != nil {
  338. return err
  339. }
  340. if pf == compressionMade {
  341. d, err = dc.Do(bytes.NewReader(d))
  342. if err != nil {
  343. return Errorf(codes.Internal, "grpc: failed to decompress the received message %v", err)
  344. }
  345. }
  346. if len(d) > maxReceiveMessageSize {
  347. // TODO: Revisit the error code. Currently keep it consistent with java
  348. // implementation.
  349. return Errorf(codes.ResourceExhausted, "grpc: received message larger than max (%d vs. %d)", len(d), maxReceiveMessageSize)
  350. }
  351. if err := c.Unmarshal(d, m); err != nil {
  352. return Errorf(codes.Internal, "grpc: failed to unmarshal the received message %v", err)
  353. }
  354. if inPayload != nil {
  355. inPayload.RecvTime = time.Now()
  356. inPayload.Payload = m
  357. // TODO truncate large payload.
  358. inPayload.Data = d
  359. inPayload.Length = len(d)
  360. }
  361. return nil
  362. }
  363. type rpcInfo struct {
  364. bytesSent bool
  365. bytesReceived bool
  366. }
  367. type rpcInfoContextKey struct{}
  368. func newContextWithRPCInfo(ctx context.Context) context.Context {
  369. return context.WithValue(ctx, rpcInfoContextKey{}, &rpcInfo{})
  370. }
  371. func rpcInfoFromContext(ctx context.Context) (s *rpcInfo, ok bool) {
  372. s, ok = ctx.Value(rpcInfoContextKey{}).(*rpcInfo)
  373. return
  374. }
  375. func updateRPCInfoInContext(ctx context.Context, s rpcInfo) {
  376. if ss, ok := rpcInfoFromContext(ctx); ok {
  377. *ss = s
  378. }
  379. return
  380. }
  381. // Code returns the error code for err if it was produced by the rpc system.
  382. // Otherwise, it returns codes.Unknown.
  383. //
  384. // Deprecated; use status.FromError and Code method instead.
  385. func Code(err error) codes.Code {
  386. if s, ok := status.FromError(err); ok {
  387. return s.Code()
  388. }
  389. return codes.Unknown
  390. }
  391. // ErrorDesc returns the error description of err if it was produced by the rpc system.
  392. // Otherwise, it returns err.Error() or empty string when err is nil.
  393. //
  394. // Deprecated; use status.FromError and Message method instead.
  395. func ErrorDesc(err error) string {
  396. if s, ok := status.FromError(err); ok {
  397. return s.Message()
  398. }
  399. return err.Error()
  400. }
  401. // Errorf returns an error containing an error code and a description;
  402. // Errorf returns nil if c is OK.
  403. //
  404. // Deprecated; use status.Errorf instead.
  405. func Errorf(c codes.Code, format string, a ...interface{}) error {
  406. return status.Errorf(c, format, a...)
  407. }
  408. // MethodConfig defines the configuration recommended by the service providers for a
  409. // particular method.
  410. // This is EXPERIMENTAL and subject to change.
  411. type MethodConfig struct {
  412. // WaitForReady indicates whether RPCs sent to this method should wait until
  413. // the connection is ready by default (!failfast). The value specified via the
  414. // gRPC client API will override the value set here.
  415. WaitForReady *bool
  416. // Timeout is the default timeout for RPCs sent to this method. The actual
  417. // deadline used will be the minimum of the value specified here and the value
  418. // set by the application via the gRPC client API. If either one is not set,
  419. // then the other will be used. If neither is set, then the RPC has no deadline.
  420. Timeout *time.Duration
  421. // MaxReqSize is the maximum allowed payload size for an individual request in a
  422. // stream (client->server) in bytes. The size which is measured is the serialized
  423. // payload after per-message compression (but before stream compression) in bytes.
  424. // The actual value used is the minumum of the value specified here and the value set
  425. // by the application via the gRPC client API. If either one is not set, then the other
  426. // will be used. If neither is set, then the built-in default is used.
  427. MaxReqSize *int
  428. // MaxRespSize is the maximum allowed payload size for an individual response in a
  429. // stream (server->client) in bytes.
  430. MaxRespSize *int
  431. }
  432. // ServiceConfig is provided by the service provider and contains parameters for how
  433. // clients that connect to the service should behave.
  434. // This is EXPERIMENTAL and subject to change.
  435. type ServiceConfig struct {
  436. // LB is the load balancer the service providers recommends. The balancer specified
  437. // via grpc.WithBalancer will override this.
  438. LB Balancer
  439. // Methods contains a map for the methods in this service.
  440. // If there is an exact match for a method (i.e. /service/method) in the map, use the corresponding MethodConfig.
  441. // If there's no exact match, look for the default config for the service (/service/) and use the corresponding MethodConfig if it exists.
  442. // Otherwise, the method has no MethodConfig to use.
  443. Methods map[string]MethodConfig
  444. }
  445. func min(a, b *int) *int {
  446. if *a < *b {
  447. return a
  448. }
  449. return b
  450. }
  451. func getMaxSize(mcMax, doptMax *int, defaultVal int) *int {
  452. if mcMax == nil && doptMax == nil {
  453. return &defaultVal
  454. }
  455. if mcMax != nil && doptMax != nil {
  456. return min(mcMax, doptMax)
  457. }
  458. if mcMax != nil {
  459. return mcMax
  460. }
  461. return doptMax
  462. }
  463. // SupportPackageIsVersion4 is referenced from generated protocol buffer files
  464. // to assert that that code is compatible with this version of the grpc package.
  465. //
  466. // This constant may be renamed in the future if a change in the generated code
  467. // requires a synchronised update of grpc-go and protoc-gen-go. This constant
  468. // should not be referenced from any other code.
  469. const SupportPackageIsVersion4 = true
  470. // Version is the current grpc version.
  471. const Version = "1.4.1"
  472. const grpcUA = "grpc-go/" + Version