stream.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. err error
  97. put func()
  98. )
  99. // TODO(zhaoq): CallOption is omitted. Add support when it is needed.
  100. gopts := BalancerGetOptions{
  101. BlockingWait: false,
  102. }
  103. t, put, err = cc.getTransport(ctx, gopts)
  104. if err != nil {
  105. return nil, toRPCErr(err)
  106. }
  107. callHdr := &transport.CallHdr{
  108. Host: cc.authority,
  109. Method: method,
  110. Flush: desc.ServerStreams && desc.ClientStreams,
  111. }
  112. if cc.dopts.cp != nil {
  113. callHdr.SendCompress = cc.dopts.cp.Type()
  114. }
  115. cs := &clientStream{
  116. desc: desc,
  117. put: put,
  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. s, err := t.NewStream(ctx, callHdr)
  137. if err != nil {
  138. cs.finish(err)
  139. return nil, toRPCErr(err)
  140. }
  141. cs.t = t
  142. cs.s = s
  143. cs.p = &parser{r: s}
  144. // Listen on ctx.Done() to detect cancellation when there is no pending
  145. // I/O operations on this stream.
  146. go func() {
  147. select {
  148. case <-t.Error():
  149. // Incur transport error, simply exit.
  150. case <-s.Context().Done():
  151. err := s.Context().Err()
  152. cs.finish(err)
  153. cs.closeTransportStream(transport.ContextErr(err))
  154. }
  155. }()
  156. return cs, nil
  157. }
  158. // clientStream implements a client side Stream.
  159. type clientStream struct {
  160. t transport.ClientTransport
  161. s *transport.Stream
  162. p *parser
  163. desc *StreamDesc
  164. codec Codec
  165. cp Compressor
  166. cbuf *bytes.Buffer
  167. dc Decompressor
  168. tracing bool // set to EnableTracing when the clientStream is created.
  169. mu sync.Mutex
  170. put func()
  171. closed bool
  172. // trInfo.tr is set when the clientStream is created (if EnableTracing is true),
  173. // and is set to nil when the clientStream's finish method is called.
  174. trInfo traceInfo
  175. }
  176. func (cs *clientStream) Context() context.Context {
  177. return cs.s.Context()
  178. }
  179. func (cs *clientStream) Header() (metadata.MD, error) {
  180. m, err := cs.s.Header()
  181. if err != nil {
  182. if _, ok := err.(transport.ConnectionError); !ok {
  183. cs.closeTransportStream(err)
  184. }
  185. }
  186. return m, err
  187. }
  188. func (cs *clientStream) Trailer() metadata.MD {
  189. return cs.s.Trailer()
  190. }
  191. func (cs *clientStream) SendMsg(m interface{}) (err error) {
  192. if cs.tracing {
  193. cs.mu.Lock()
  194. if cs.trInfo.tr != nil {
  195. cs.trInfo.tr.LazyLog(&payload{sent: true, msg: m}, true)
  196. }
  197. cs.mu.Unlock()
  198. }
  199. defer func() {
  200. if err != nil {
  201. cs.finish(err)
  202. }
  203. if err == nil || err == io.EOF {
  204. return
  205. }
  206. if _, ok := err.(transport.ConnectionError); !ok {
  207. cs.closeTransportStream(err)
  208. }
  209. err = toRPCErr(err)
  210. }()
  211. out, err := encode(cs.codec, m, cs.cp, cs.cbuf)
  212. defer func() {
  213. if cs.cbuf != nil {
  214. cs.cbuf.Reset()
  215. }
  216. }()
  217. if err != nil {
  218. return transport.StreamErrorf(codes.Internal, "grpc: %v", err)
  219. }
  220. return cs.t.Write(cs.s, out, &transport.Options{Last: false})
  221. }
  222. func (cs *clientStream) RecvMsg(m interface{}) (err error) {
  223. err = recv(cs.p, cs.codec, cs.s, cs.dc, m)
  224. defer func() {
  225. // err != nil indicates the termination of the stream.
  226. if err != nil {
  227. cs.finish(err)
  228. }
  229. }()
  230. if err == nil {
  231. if cs.tracing {
  232. cs.mu.Lock()
  233. if cs.trInfo.tr != nil {
  234. cs.trInfo.tr.LazyLog(&payload{sent: false, msg: m}, true)
  235. }
  236. cs.mu.Unlock()
  237. }
  238. if !cs.desc.ClientStreams || cs.desc.ServerStreams {
  239. return
  240. }
  241. // Special handling for client streaming rpc.
  242. err = recv(cs.p, cs.codec, cs.s, cs.dc, m)
  243. cs.closeTransportStream(err)
  244. if err == nil {
  245. return toRPCErr(errors.New("grpc: client streaming protocol violation: get <nil>, want <EOF>"))
  246. }
  247. if err == io.EOF {
  248. if cs.s.StatusCode() == codes.OK {
  249. cs.finish(err)
  250. return nil
  251. }
  252. return Errorf(cs.s.StatusCode(), "%s", cs.s.StatusDesc())
  253. }
  254. return toRPCErr(err)
  255. }
  256. if _, ok := err.(transport.ConnectionError); !ok {
  257. cs.closeTransportStream(err)
  258. }
  259. if err == io.EOF {
  260. if cs.s.StatusCode() == codes.OK {
  261. // Returns io.EOF to indicate the end of the stream.
  262. return
  263. }
  264. return Errorf(cs.s.StatusCode(), "%s", cs.s.StatusDesc())
  265. }
  266. return toRPCErr(err)
  267. }
  268. func (cs *clientStream) CloseSend() (err error) {
  269. err = cs.t.Write(cs.s, nil, &transport.Options{Last: true})
  270. defer func() {
  271. if err != nil {
  272. cs.finish(err)
  273. }
  274. }()
  275. if err == nil || err == io.EOF {
  276. return
  277. }
  278. if _, ok := err.(transport.ConnectionError); !ok {
  279. cs.closeTransportStream(err)
  280. }
  281. err = toRPCErr(err)
  282. return
  283. }
  284. func (cs *clientStream) closeTransportStream(err error) {
  285. cs.mu.Lock()
  286. if cs.closed {
  287. cs.mu.Unlock()
  288. return
  289. }
  290. cs.closed = true
  291. cs.mu.Unlock()
  292. cs.t.CloseStream(cs.s, err)
  293. }
  294. func (cs *clientStream) finish(err error) {
  295. if !cs.tracing {
  296. return
  297. }
  298. cs.mu.Lock()
  299. defer cs.mu.Unlock()
  300. if cs.put != nil {
  301. cs.put()
  302. cs.put = nil
  303. }
  304. if cs.trInfo.tr != nil {
  305. if err == nil || err == io.EOF {
  306. cs.trInfo.tr.LazyPrintf("RPC: [OK]")
  307. } else {
  308. cs.trInfo.tr.LazyPrintf("RPC: [%v]", err)
  309. cs.trInfo.tr.SetError()
  310. }
  311. cs.trInfo.tr.Finish()
  312. cs.trInfo.tr = nil
  313. }
  314. }
  315. // ServerStream defines the interface a server stream has to satisfy.
  316. type ServerStream interface {
  317. // SendHeader sends the header metadata. It should not be called
  318. // after SendProto. It fails if called multiple times or if
  319. // called after SendProto.
  320. SendHeader(metadata.MD) error
  321. // SetTrailer sets the trailer metadata which will be sent with the
  322. // RPC status.
  323. SetTrailer(metadata.MD)
  324. Stream
  325. }
  326. // serverStream implements a server side Stream.
  327. type serverStream struct {
  328. t transport.ServerTransport
  329. s *transport.Stream
  330. p *parser
  331. codec Codec
  332. cp Compressor
  333. dc Decompressor
  334. cbuf *bytes.Buffer
  335. statusCode codes.Code
  336. statusDesc string
  337. trInfo *traceInfo
  338. mu sync.Mutex // protects trInfo.tr after the service handler runs.
  339. }
  340. func (ss *serverStream) Context() context.Context {
  341. return ss.s.Context()
  342. }
  343. func (ss *serverStream) SendHeader(md metadata.MD) error {
  344. return ss.t.WriteHeader(ss.s, md)
  345. }
  346. func (ss *serverStream) SetTrailer(md metadata.MD) {
  347. if md.Len() == 0 {
  348. return
  349. }
  350. ss.s.SetTrailer(md)
  351. return
  352. }
  353. func (ss *serverStream) SendMsg(m interface{}) (err error) {
  354. defer func() {
  355. if ss.trInfo != nil {
  356. ss.mu.Lock()
  357. if ss.trInfo.tr != nil {
  358. if err == nil {
  359. ss.trInfo.tr.LazyLog(&payload{sent: true, msg: m}, true)
  360. } else {
  361. ss.trInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
  362. ss.trInfo.tr.SetError()
  363. }
  364. }
  365. ss.mu.Unlock()
  366. }
  367. }()
  368. out, err := encode(ss.codec, m, ss.cp, ss.cbuf)
  369. defer func() {
  370. if ss.cbuf != nil {
  371. ss.cbuf.Reset()
  372. }
  373. }()
  374. if err != nil {
  375. err = transport.StreamErrorf(codes.Internal, "grpc: %v", err)
  376. return err
  377. }
  378. return ss.t.Write(ss.s, out, &transport.Options{Last: false})
  379. }
  380. func (ss *serverStream) RecvMsg(m interface{}) (err error) {
  381. defer func() {
  382. if ss.trInfo != nil {
  383. ss.mu.Lock()
  384. if ss.trInfo.tr != nil {
  385. if err == nil {
  386. ss.trInfo.tr.LazyLog(&payload{sent: false, msg: m}, true)
  387. } else if err != io.EOF {
  388. ss.trInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
  389. ss.trInfo.tr.SetError()
  390. }
  391. }
  392. ss.mu.Unlock()
  393. }
  394. }()
  395. return recv(ss.p, ss.codec, ss.s, ss.dc, m)
  396. }