stream.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. "errors"
  37. "io"
  38. "sync"
  39. "time"
  40. "golang.org/x/net/context"
  41. "golang.org/x/net/trace"
  42. "google.golang.org/grpc/codes"
  43. "google.golang.org/grpc/metadata"
  44. "google.golang.org/grpc/transport"
  45. )
  46. // StreamHandler defines the handler called by gRPC server to complete the
  47. // execution of a streaming RPC.
  48. type StreamHandler func(srv interface{}, stream ServerStream) error
  49. // StreamDesc represents a streaming RPC service's method specification.
  50. type StreamDesc struct {
  51. StreamName string
  52. Handler StreamHandler
  53. // At least one of these is true.
  54. ServerStreams bool
  55. ClientStreams bool
  56. }
  57. // Stream defines the common interface a client or server stream has to satisfy.
  58. type Stream interface {
  59. // Context returns the context for this stream.
  60. Context() context.Context
  61. // SendMsg blocks until it sends m, the stream is done or the stream
  62. // breaks.
  63. // On error, it aborts the stream and returns an RPC status on client
  64. // side. On server side, it simply returns the error to the caller.
  65. // SendMsg is called by generated code. Also Users can call SendMsg
  66. // directly when it is really needed in their use cases.
  67. SendMsg(m interface{}) error
  68. // RecvMsg blocks until it receives a message or the stream is
  69. // done. On client side, it returns io.EOF when the stream is done. On
  70. // any other error, it aborts the stream and returns an RPC status. On
  71. // server side, it simply returns the error to the caller.
  72. RecvMsg(m interface{}) error
  73. }
  74. // ClientStream defines the interface a client stream has to satisfy.
  75. type ClientStream interface {
  76. // Header returns the header metadata received from the server if there
  77. // is any. It blocks if the metadata is not ready to read.
  78. Header() (metadata.MD, error)
  79. // Trailer returns the trailer metadata from the server. It must be called
  80. // after stream.Recv() returns non-nil error (including io.EOF) for
  81. // bi-directional streaming and server streaming or stream.CloseAndRecv()
  82. // returns for client streaming in order to receive trailer metadata if
  83. // present. Otherwise, it could returns an empty MD even though trailer
  84. // is present.
  85. Trailer() metadata.MD
  86. // CloseSend closes the send direction of the stream. It closes the stream
  87. // when non-nil error is met.
  88. CloseSend() error
  89. Stream
  90. }
  91. // NewClientStream creates a new Stream for the client side. This is called
  92. // by generated code.
  93. func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (ClientStream, error) {
  94. var (
  95. t transport.ClientTransport
  96. s *transport.Stream
  97. err error
  98. put func()
  99. )
  100. c := defaultCallInfo
  101. for _, o := range opts {
  102. if err := o.before(&c); err != nil {
  103. return nil, toRPCErr(err)
  104. }
  105. }
  106. callHdr := &transport.CallHdr{
  107. Host: cc.authority,
  108. Method: method,
  109. Flush: desc.ServerStreams && desc.ClientStreams,
  110. }
  111. if cc.dopts.cp != nil {
  112. callHdr.SendCompress = cc.dopts.cp.Type()
  113. }
  114. cs := &clientStream{
  115. opts: opts,
  116. c: c,
  117. desc: desc,
  118. codec: cc.dopts.codec,
  119. cp: cc.dopts.cp,
  120. dc: cc.dopts.dc,
  121. tracing: EnableTracing,
  122. }
  123. if cc.dopts.cp != nil {
  124. callHdr.SendCompress = cc.dopts.cp.Type()
  125. cs.cbuf = new(bytes.Buffer)
  126. }
  127. if cs.tracing {
  128. cs.trInfo.tr = trace.New("grpc.Sent."+methodFamily(method), method)
  129. cs.trInfo.firstLine.client = true
  130. if deadline, ok := ctx.Deadline(); ok {
  131. cs.trInfo.firstLine.deadline = deadline.Sub(time.Now())
  132. }
  133. cs.trInfo.tr.LazyLog(&cs.trInfo.firstLine, false)
  134. ctx = trace.NewContext(ctx, cs.trInfo.tr)
  135. }
  136. gopts := BalancerGetOptions{
  137. BlockingWait: !c.failFast,
  138. }
  139. for {
  140. t, put, err = cc.getTransport(ctx, gopts)
  141. if err != nil {
  142. // TODO(zhaoq): Probably revisit the error handling.
  143. if _, ok := err.(*rpcError); ok {
  144. return nil, err
  145. }
  146. if err == errConnClosing {
  147. if c.failFast {
  148. return nil, Errorf(codes.Unavailable, "%v", errConnClosing)
  149. }
  150. continue
  151. }
  152. // All the other errors are treated as Internal errors.
  153. return nil, Errorf(codes.Internal, "%v", err)
  154. }
  155. s, err = t.NewStream(ctx, callHdr)
  156. if err != nil {
  157. if put != nil {
  158. put()
  159. put = nil
  160. }
  161. if _, ok := err.(transport.ConnectionError); ok {
  162. if c.failFast {
  163. cs.finish(err)
  164. return nil, toRPCErr(err)
  165. }
  166. continue
  167. }
  168. return nil, toRPCErr(err)
  169. }
  170. break
  171. }
  172. cs.put = put
  173. cs.t = t
  174. cs.s = s
  175. cs.p = &parser{r: s}
  176. // Listen on ctx.Done() to detect cancellation when there is no pending
  177. // I/O operations on this stream.
  178. go func() {
  179. select {
  180. case <-t.Error():
  181. // Incur transport error, simply exit.
  182. case <-s.Context().Done():
  183. err := s.Context().Err()
  184. cs.finish(err)
  185. cs.closeTransportStream(transport.ContextErr(err))
  186. }
  187. }()
  188. return cs, nil
  189. }
  190. // clientStream implements a client side Stream.
  191. type clientStream struct {
  192. opts []CallOption
  193. c callInfo
  194. t transport.ClientTransport
  195. s *transport.Stream
  196. p *parser
  197. desc *StreamDesc
  198. codec Codec
  199. cp Compressor
  200. cbuf *bytes.Buffer
  201. dc Decompressor
  202. tracing bool // set to EnableTracing when the clientStream is created.
  203. mu sync.Mutex
  204. put func()
  205. closed bool
  206. // trInfo.tr is set when the clientStream is created (if EnableTracing is true),
  207. // and is set to nil when the clientStream's finish method is called.
  208. trInfo traceInfo
  209. }
  210. func (cs *clientStream) Context() context.Context {
  211. return cs.s.Context()
  212. }
  213. func (cs *clientStream) Header() (metadata.MD, error) {
  214. m, err := cs.s.Header()
  215. if err != nil {
  216. if _, ok := err.(transport.ConnectionError); !ok {
  217. cs.closeTransportStream(err)
  218. }
  219. }
  220. return m, err
  221. }
  222. func (cs *clientStream) Trailer() metadata.MD {
  223. return cs.s.Trailer()
  224. }
  225. func (cs *clientStream) SendMsg(m interface{}) (err error) {
  226. if cs.tracing {
  227. cs.mu.Lock()
  228. if cs.trInfo.tr != nil {
  229. cs.trInfo.tr.LazyLog(&payload{sent: true, msg: m}, true)
  230. }
  231. cs.mu.Unlock()
  232. }
  233. defer func() {
  234. if err != nil {
  235. cs.finish(err)
  236. }
  237. if err == nil || err == io.EOF {
  238. return
  239. }
  240. if _, ok := err.(transport.ConnectionError); !ok {
  241. cs.closeTransportStream(err)
  242. }
  243. err = toRPCErr(err)
  244. }()
  245. out, err := encode(cs.codec, m, cs.cp, cs.cbuf)
  246. defer func() {
  247. if cs.cbuf != nil {
  248. cs.cbuf.Reset()
  249. }
  250. }()
  251. if err != nil {
  252. return transport.StreamErrorf(codes.Internal, "grpc: %v", err)
  253. }
  254. return cs.t.Write(cs.s, out, &transport.Options{Last: false})
  255. }
  256. func (cs *clientStream) RecvMsg(m interface{}) (err error) {
  257. err = recv(cs.p, cs.codec, cs.s, cs.dc, m)
  258. defer func() {
  259. // err != nil indicates the termination of the stream.
  260. if err != nil {
  261. cs.finish(err)
  262. }
  263. }()
  264. if err == nil {
  265. if cs.tracing {
  266. cs.mu.Lock()
  267. if cs.trInfo.tr != nil {
  268. cs.trInfo.tr.LazyLog(&payload{sent: false, msg: m}, true)
  269. }
  270. cs.mu.Unlock()
  271. }
  272. if !cs.desc.ClientStreams || cs.desc.ServerStreams {
  273. return
  274. }
  275. // Special handling for client streaming rpc.
  276. err = recv(cs.p, cs.codec, cs.s, cs.dc, m)
  277. cs.closeTransportStream(err)
  278. if err == nil {
  279. return toRPCErr(errors.New("grpc: client streaming protocol violation: get <nil>, want <EOF>"))
  280. }
  281. if err == io.EOF {
  282. if cs.s.StatusCode() == codes.OK {
  283. cs.finish(err)
  284. return nil
  285. }
  286. return Errorf(cs.s.StatusCode(), "%s", cs.s.StatusDesc())
  287. }
  288. return toRPCErr(err)
  289. }
  290. if _, ok := err.(transport.ConnectionError); !ok {
  291. cs.closeTransportStream(err)
  292. }
  293. if err == io.EOF {
  294. if cs.s.StatusCode() == codes.OK {
  295. // Returns io.EOF to indicate the end of the stream.
  296. return
  297. }
  298. return Errorf(cs.s.StatusCode(), "%s", cs.s.StatusDesc())
  299. }
  300. return toRPCErr(err)
  301. }
  302. func (cs *clientStream) CloseSend() (err error) {
  303. err = cs.t.Write(cs.s, nil, &transport.Options{Last: true})
  304. defer func() {
  305. if err != nil {
  306. cs.finish(err)
  307. }
  308. }()
  309. if err == nil || err == io.EOF {
  310. return
  311. }
  312. if _, ok := err.(transport.ConnectionError); !ok {
  313. cs.closeTransportStream(err)
  314. }
  315. err = toRPCErr(err)
  316. return
  317. }
  318. func (cs *clientStream) closeTransportStream(err error) {
  319. cs.mu.Lock()
  320. if cs.closed {
  321. cs.mu.Unlock()
  322. return
  323. }
  324. cs.closed = true
  325. cs.mu.Unlock()
  326. cs.t.CloseStream(cs.s, err)
  327. }
  328. func (cs *clientStream) finish(err error) {
  329. cs.mu.Lock()
  330. defer cs.mu.Unlock()
  331. for _, o := range cs.opts {
  332. o.after(&cs.c)
  333. }
  334. if cs.put != nil {
  335. cs.put()
  336. cs.put = nil
  337. }
  338. if !cs.tracing {
  339. return
  340. }
  341. if cs.trInfo.tr != nil {
  342. if err == nil || err == io.EOF {
  343. cs.trInfo.tr.LazyPrintf("RPC: [OK]")
  344. } else {
  345. cs.trInfo.tr.LazyPrintf("RPC: [%v]", err)
  346. cs.trInfo.tr.SetError()
  347. }
  348. cs.trInfo.tr.Finish()
  349. cs.trInfo.tr = nil
  350. }
  351. }
  352. // ServerStream defines the interface a server stream has to satisfy.
  353. type ServerStream interface {
  354. // SendHeader sends the header metadata. It should not be called
  355. // after SendProto. It fails if called multiple times or if
  356. // called after SendProto.
  357. SendHeader(metadata.MD) error
  358. // SetTrailer sets the trailer metadata which will be sent with the
  359. // RPC status.
  360. SetTrailer(metadata.MD)
  361. Stream
  362. }
  363. // serverStream implements a server side Stream.
  364. type serverStream struct {
  365. t transport.ServerTransport
  366. s *transport.Stream
  367. p *parser
  368. codec Codec
  369. cp Compressor
  370. dc Decompressor
  371. cbuf *bytes.Buffer
  372. statusCode codes.Code
  373. statusDesc string
  374. trInfo *traceInfo
  375. mu sync.Mutex // protects trInfo.tr after the service handler runs.
  376. }
  377. func (ss *serverStream) Context() context.Context {
  378. return ss.s.Context()
  379. }
  380. func (ss *serverStream) SendHeader(md metadata.MD) error {
  381. return ss.t.WriteHeader(ss.s, md)
  382. }
  383. func (ss *serverStream) SetTrailer(md metadata.MD) {
  384. if md.Len() == 0 {
  385. return
  386. }
  387. ss.s.SetTrailer(md)
  388. return
  389. }
  390. func (ss *serverStream) SendMsg(m interface{}) (err error) {
  391. defer func() {
  392. if ss.trInfo != nil {
  393. ss.mu.Lock()
  394. if ss.trInfo.tr != nil {
  395. if err == nil {
  396. ss.trInfo.tr.LazyLog(&payload{sent: true, msg: m}, true)
  397. } else {
  398. ss.trInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
  399. ss.trInfo.tr.SetError()
  400. }
  401. }
  402. ss.mu.Unlock()
  403. }
  404. }()
  405. out, err := encode(ss.codec, m, ss.cp, ss.cbuf)
  406. defer func() {
  407. if ss.cbuf != nil {
  408. ss.cbuf.Reset()
  409. }
  410. }()
  411. if err != nil {
  412. err = transport.StreamErrorf(codes.Internal, "grpc: %v", err)
  413. return err
  414. }
  415. return ss.t.Write(ss.s, out, &transport.Options{Last: false})
  416. }
  417. func (ss *serverStream) RecvMsg(m interface{}) (err error) {
  418. defer func() {
  419. if ss.trInfo != nil {
  420. ss.mu.Lock()
  421. if ss.trInfo.tr != nil {
  422. if err == nil {
  423. ss.trInfo.tr.LazyLog(&payload{sent: false, msg: m}, true)
  424. } else if err != io.EOF {
  425. ss.trInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
  426. ss.trInfo.tr.SetError()
  427. }
  428. }
  429. ss.mu.Unlock()
  430. }
  431. }()
  432. return recv(ss.p, ss.codec, ss.s, ss.dc, m)
  433. }