rpc_util.go 15 KB

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