stream.go 11 KB

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