-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsession_stream_client.go
340 lines (291 loc) · 7.42 KB
/
session_stream_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (C) 2018 Kun Zhong All rights reserved.
// Use of this source code is governed by a Licensed under the Apache License, Version 2.0 (the "License");
package grpcx
import (
"fmt"
"io"
"sync"
"github.com/pigogo/grpcx/codec"
xlog "github.com/pigogo/grpcx/grpclog"
"golang.org/x/net/context"
)
// NewStream creates a new Stream for the client side. This is typically
// called by generated code. ctx is used for the lifetime of the stream.
//
// To ensure resources are not leaked due to the stream returned, one of the following
// actions must be performed:
//
// 1. Call Close on the ClientConn.
// 2. Cancel the context provided.
// 3. Call RecvMsg until a non-nil error is returned. A protobuf-generated
// client-streaming RPC, for instance, might use the helper function
// CloseAndRecv (note that CloseSend does not Recv, therefore is not
// guaranteed to release all resources).
// 4. Receive a non-nil, non-io.EOF error from Header or SendMsg.
//
// If none of the above happen, a goroutine and a context will be leaked, and grpc
// will not call the optionally-configured stats handler with a stats.End message.
func (cc *ClientConn) NewStream(ctx context.Context, desc *StreamDesc, method string, opts ...CallOption) (ClientStream, error) {
// allow interceptor to see all applicable call options, which means those
// configured as defaults from dial option as well as per-call options
opts = combine(cc.dopts.callOptions, opts)
/*if cc.dopts.streamInt != nil {
return cc.dopts.streamInt(ctx, desc, cc, method, newClientStream, opts...)
}*/
return newClientStream(ctx, desc, cc, method, opts...)
}
// NewClientStream creates a new Stream for the client side. This is called
func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (_ ClientStream, err error) {
return newClientStream(ctx, desc, cc, method, opts...)
}
func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (_ ClientStream, err error) {
var (
conn *connDial
put func()
cancel context.CancelFunc
sctx context.Context
)
c := defaultCallInfo
mc := cc.GetMethodConfig(method)
if mc.WaitForReady != nil {
c.failFast = !*mc.WaitForReady
}
if mc.Timeout != nil {
sctx, cancel = context.WithTimeout(ctx, *mc.Timeout)
} else {
sctx, cancel = context.WithCancel(ctx)
}
opts = append(cc.dopts.callOptions, opts...)
for _, o := range opts {
if err := o.before(&c); err != nil {
return nil, err
}
}
c.maxSendMessageSize = getMaxSize(mc.MaxReqSize, c.maxSendMessageSize, defaultClientMaxSendMessageSize)
c.maxReceiveMessageSize = getMaxSize(mc.MaxRespSize, c.maxReceiveMessageSize, defaultClientMaxReceiveMessageSize)
hkey := uint32(0)
if c.hbKey != nil {
hkey = *c.hbKey
}
gopts := BalancerGetOptions{
BlockingWait: !c.failFast,
HashKey: hkey,
}
conn, put, err = cc.getConn(ctx, gopts)
cs := &clientStream{
opts: opts,
c: c,
desc: desc,
codec: cc.dopts.codec,
cancel: cancel,
ctx: sctx,
buf: newRecvBuffer(),
put: put,
conn: conn,
method: method,
sessionid: cc.genStreamID(),
}
cs.connCancel, err = conn.withSession(cs.sessionid, cs)
if err != nil {
err = fmt.Errorf("grpcx: newClientStream save session fail:%v", err)
xlog.Error(err)
return nil, err
}
if err = cs.init(); err != nil {
xlog.Errorf("grpcx: clientStream init fail:%v", err)
cs.connCancel()
return nil, err
}
// Listen on ctx.Done() to detect cancellation and s.Done() to detect normal termination
// when there is no pending I/O operations on this stream.
go func() {
select {
case <-conn.Error():
break
case <-cs.ctx.Done():
break
}
cs.finish()
}()
return cs, nil
}
// clientStream implements a client side Stream.
type clientStream struct {
ClientStream
buf *recvBuffer
opts []CallOption
c callInfo
conn *connDial
desc *StreamDesc
codec codec.Codec
cancel context.CancelFunc
ctx context.Context
sessionid int64
connCancel func()
err error
mu sync.Mutex
put func()
finished bool
method string
}
func (cs *clientStream) init() error {
return cs.send(&PackHeader{
Ptype: PackType_SINI,
Sessionid: cs.sessionid,
Methord: cs.method,
}, nil)
}
func (cs *clientStream) Context() context.Context {
return cs.ctx
}
func (cs *clientStream) streamID() int64 {
return cs.sessionid
}
func (cs *clientStream) SendMsg(m interface{}) (err error) {
defer func() {
if err != nil {
cs.finish()
}
if err == nil {
return
}
if err == io.EOF {
if !cs.desc.ClientStreams && cs.desc.ServerStreams {
err = nil
}
return
}
xlog.Errorf("grpcx: clientStream SendMsg fail:%v", err)
}()
if cs.err != nil {
return cs.err
}
head := &PackHeader{
Ptype: PackType_SREQ,
Sessionid: cs.sessionid,
Methord: cs.method,
}
return cs.send(head, m)
}
func (cs *clientStream) send(head *PackHeader, m interface{}) (err error) {
return cs.conn.send(head, m, *cs.c.maxSendMessageSize)
}
func (cs *clientStream) RecvMsg(m interface{}) (err error) {
defer func() {
if err != nil {
if err != io.EOF {
xlog.Errorf("grpcx: clientStream RecvMsg fail:%v sessionid:%v", err, cs.sessionid)
cs.finish()
}
}
}()
var msg *netPack
select {
case msg = <-cs.buf.get():
cs.buf.load()
break
case <-cs.ctx.Done():
select {
case msg = <-cs.buf.get():
cs.buf.load()
break
default:
err = cs.err
if err == nil {
return cs.ctx.Err()
}
return
}
}
if msg.head.Ptype == PackType_EOF {
return io.EOF
} else if msg.head.Ptype == PackType_ERROR || msg.head.Ptype == PackType_GoAway {
return cs.err
}
err = cs.codec.Unmarshal(msg.body, m)
if err == nil {
if !cs.desc.ClientStreams || cs.desc.ServerStreams {
return
}
select {
case msg = <-cs.buf.get():
cs.buf.load()
break
case <-cs.conn.Error():
err = fmt.Errorf("grpcx: connection error")
break
case <-cs.ctx.Done():
err = cs.err
if err == nil {
err = cs.ctx.Err()
}
}
if err != nil || msg.head.Ptype != PackType_EOF {
return fmt.Errorf("grpcx: client streaming protocol violation: err get:%v want <EOF>", err)
}
cs.finish()
return nil
}
return err
}
func (cs *clientStream) CloseSend() (err error) {
defer func() {
if err != nil {
cs.finish()
}
}()
if cs.err != nil {
return cs.err
}
head := &PackHeader{
Ptype: PackType_EOF,
Sessionid: cs.sessionid,
}
return cs.send(head, nil)
}
func (cs *clientStream) finish() {
cs.mu.Lock()
defer cs.mu.Unlock()
if cs.finished {
return
}
cs.finished = true
for _, o := range cs.opts {
o.after(&cs.c)
}
if cs.cancel != nil {
cs.cancel()
cs.cancel = nil
}
if cs.connCancel != nil {
cs.connCancel()
cs.connCancel = nil
}
if cs.put != nil {
cs.put()
cs.put = nil
}
}
func (cs *clientStream) onMessage(conn Conn, head *PackHeader, body []byte) error {
if cs.conn != conn {
cs.err = fmt.Errorf("grpcx clientStream::onMessage unknow error:conn mix up")
xlog.Error(cs.err)
cs.buf.put(&netPack{
head: head,
body: []byte(cs.err.Error()),
})
return cs.err
} else if head.Ptype == PackType_ERROR {
cs.err = fmt.Errorf("grpcx: receive srv error")
} else if head.Ptype == PackType_GoAway {
cs.err = errSrvGoaway
}
cs.buf.put(&netPack{
head: head,
body: body,
})
if head.Ptype != PackType_SRSP {
cs.finish()
}
return nil
}