stream.go 11 KB

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