Debian Codegraph @

stream.go

raw symbols SCIP
1 package yarpc
2
3 import (
4 "context"
5 "encoding/binary"
6 "errors"
7 "io"
8
9 "github.com/influxdata/yamux"
10 "github.com/influxdata/yarpc/codes"
11 "github.com/influxdata/yarpc/status"
12 )
13
14 type StreamHandler func(srv interface{}, stream ServerStream) error
15
16 type StreamDesc struct {
17 Index uint8
18 StreamName string
19 Handler StreamHandler
20
21 ServerStreams bool
22 ClientStreams bool
23 }
24
25 // Stream defines the common interface a client or server stream has to satisfy.
26 type Stream interface {
27 // Context returns the context for this stream.
28 Context() context.Context
29 // SendMsg blocks until it sends m, the stream is done or the stream
30 // breaks.
31 // On error, it aborts the stream and returns an RPC status on client
32 // side. On server side, it simply returns the error to the caller.
33 // SendMsg is called by generated code. Also Users can call SendMsg
34 // directly when it is really needed in their use cases.
35 // It's safe to have a goroutine calling SendMsg and another goroutine calling
36 // recvMsg on the same stream at the same time.
37 // But it is not safe to call SendMsg on the same stream in different goroutines.
38 SendMsg(m interface{}) error
39 // RecvMsg blocks until it receives a message or the stream is
40 // done. On client side, it returns io.EOF when the stream is done. On
41 // any other error, it aborts the stream and returns an RPC status. On
42 // server side, it simply returns the error to the caller.
43 // It's safe to have a goroutine calling SendMsg and another goroutine calling
44 // recvMsg on the same stream at the same time.
45 // But it is not safe to call RecvMsg on the same stream in different goroutines.
46 RecvMsg(m interface{}) error
47 }
48
49 // ClientStream defines the interface a client stream has to satisfy.
50 type ClientStream interface {
51 // CloseSend closes the send direction of the stream. It closes the stream
52 // when non-nil error is met.
53 CloseSend() error
54 Stream
55 }
56
57 func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, api uint16) (ClientStream, error) {
58 cn, err := cc.NewStream()
59 if err != nil {
60 return nil, err
61 }
62
63 var tmp [2]byte
64 binary.BigEndian.PutUint16(tmp[:], api)
65 _, err = cn.Write(tmp[:])
66 if err != nil {
67 return nil, err
68 }
69
70 cs := &clientStream{
71 cn: cn,
72 codec: cc.dopts.codec,
73 p: &parser{r: cn},
74 desc: desc,
75 ctx: ctx,
76 closing: make(chan struct{}),
77 }
78 go func() {
79 select {
80 case <-ctx.Done():
81 cs.CloseSend()
82 case <-cs.closing:
83 }
84 }()
85
86 return cs, nil
87 }
88
89 type clientStream struct {
90 cn *yamux.Stream
91 codec Codec
92 p *parser
93 desc *StreamDesc
94
95 ctx context.Context
96 closing chan struct{}
97 }
98
99 func (c *clientStream) CloseSend() error {
100 select {
101 case <-c.closing:
102 default:
103 close(c.closing)
104 }
105 return c.cn.Close()
106 }
107
108 func (c *clientStream) Context() context.Context {
109 return c.ctx
110 }
111
112 func (c *clientStream) SendMsg(m interface{}) error {
113 select {
114 case <-c.closing:
115 return errors.New("stream closed")
116 default:
117 }
118 out, err := encode(c.codec, m)
119 if err != nil {
120 return err
121 }
122
123 _, err = c.cn.Write(out)
124 return err
125 }
126
127 func (c *clientStream) RecvMsg(m interface{}) error {
128 select {
129 case <-c.closing:
130 return errors.New("stream closed")
131 default:
132 }
133 err := decode(c.p, c.codec, c.cn, m)
134 if err == nil {
135 if !c.desc.ClientStreams || c.desc.ServerStreams {
136 return nil
137 }
138 }
139 return err
140 }
141
142 type ServerStream interface {
143 Stream
144 }
145
146 type serverStream struct {
147 cn *yamux.Stream
148 codec Codec
149 p *parser
150 buf []byte
151 }
152
153 func (s *serverStream) Context() context.Context {
154 panic("implement me")
155 }
156
157 func (s *serverStream) SendMsg(m interface{}) error {
158 out, err := encode(s.codec, m)
159 if err != nil {
160 return err
161 }
162
163 _, err = s.cn.Write(out)
164 if err != nil {
165 // TODO(sgc): wrap in status error
166 return err
167 }
168 return nil
169 }
170
171 func (s *serverStream) RecvMsg(m interface{}) error {
172 if err := decode(s.p, s.codec, s.cn, m); err != nil {
173 if err == io.EOF {
174 return err
175 }
176 if err == io.ErrUnexpectedEOF {
177 err = status.Errorf(codes.Internal, io.ErrUnexpectedEOF.Error())
178 }
179 // TODO(sgc): wrap in status error
180 return err
181 }
182 return nil
183 }