rpc_util.go 15 KB

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