-
Notifications
You must be signed in to change notification settings - Fork 1
/
conn.go
612 lines (568 loc) · 14.1 KB
/
conn.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// Copyright (c) 2019 Meng Huang (mhboy@outlook.com)
// This package is licensed under a MIT license that can be found in the LICENSE file.
package rpc
import (
"context"
"errors"
"github.com/hslam/buffer"
"github.com/hslam/scheduler"
"github.com/hslam/socket"
"io"
"runtime"
"sync"
)
// ErrTimeout is returned after the timeout,.
var ErrTimeout = errors.New("timeout")
const shutdownMsg = "The connection is shut down"
// ErrShutdown is returned when the connection is shut down.
var ErrShutdown = errors.New(shutdownMsg)
var (
callPool = &sync.Pool{New: func() interface{} { return &Call{} }}
donePool = &sync.Pool{New: func() interface{} { return make(chan *Call, 10) }}
ctxPool = &sync.Pool{New: func() interface{} { return &Context{} }}
upgradePool = &sync.Pool{New: func() interface{} { return &upgrade{} }}
upgradeBufferPool = &sync.Pool{New: func() interface{} { return make([]byte, upgradeSize) }}
)
func getUpgrade() *upgrade {
return upgradePool.Get().(*upgrade)
}
func putUpgrade(u *upgrade) {
u.Reset()
upgradePool.Put(u)
}
func getUpgradeBuffer() []byte {
return upgradeBufferPool.Get().([]byte)
}
func putUpgradeBuffer(b []byte) {
if cap(b) == upgradeSize {
upgradeBufferPool.Put(b)
}
}
func getContext() *Context {
return ctxPool.Get().(*Context)
}
func putContext(ctx *Context) {
ctx.Reset()
ctxPool.Put(ctx)
}
// GetCall gets a call from the callPool.
func GetCall() *Call {
call := callPool.Get().(*Call)
call.Done = donePool.Get().(chan *Call)
return call
}
// PutCall puts a call to the callPool.
func PutCall(call *Call) {
if call.Done != nil {
done := call.Done
ResetDone(done)
donePool.Put(done)
}
*call = Call{}
callPool.Put(call)
}
// Call represents an active RPC.
type Call struct {
Buffer []byte
upgrade *upgrade
Value []byte
ServiceMethod string
Args interface{}
Reply interface{}
CallError bool
Error error
Done chan *Call
stream *stream
}
func (call *Call) done() {
select {
case call.Done <- call:
default:
}
}
func (call *Call) streaming() {
if call.stream != nil && call.upgrade.Stream == streaming {
e := getEvent()
e.Value = call.Value
e.Error = call.Error
call.stream.trigger(e)
}
}
// Conn represents an RPC Conn.
// There may be multiple outstanding Calls associated
// with a single Conn, and a Conn may be used by
// multiple goroutines simultaneously.
type Conn struct {
codec ClientCodec
mutex sync.Mutex
seq uint64
pending map[uint64]*Call
streams map[uint64]*Call
bufferPool *buffer.Pool
writeSched scheduler.Scheduler
readSched scheduler.Scheduler
readStream scheduler.Scheduler
noCopy bool
directIO bool
closing bool
shutdown bool
}
// NewClientCodecFunc is the function to make a new ClientCodec by socket.Messages.
type NewClientCodecFunc func(messages socket.Messages) ClientCodec
// NewConn returns a new Conn to handle requests to the
// set of services at the other end of the connection.
// It adds a buffer to the write side of the connection so
// the header and payload are sent as a unit.
//
// The read and write halves of the connection are serialized independently,
// so no interlocking is required. However each half may be accessed
// concurrently so the implementation of conn should protect against
// concurrent reads or concurrent writes.
func NewConn() *Conn {
return &Conn{
pending: make(map[uint64]*Call),
streams: make(map[uint64]*Call),
bufferPool: buffer.AssignPool(bufferSize),
readStream: scheduler.New(1, &scheduler.Options{Threshold: 2}),
}
}
// Dial connects to an RPC server at the specified network address.
func (conn *Conn) Dial(s socket.Socket, address string, New NewClientCodecFunc) (*Conn, error) {
c, err := s.Dial(address)
if err != nil {
return nil, err
}
conn.codec = New(c.Messages())
go conn.recv()
return conn, nil
}
// NewConnWithCodec is like NewConn but uses the specified
// codec to encode requests and decode responses.
func NewConnWithCodec(codec ClientCodec) *Conn {
if codec == nil {
return nil
}
c := NewConn()
c.codec = codec
go c.recv()
return c
}
// SetBufferSize sets buffer size.
func (conn *Conn) SetBufferSize(size int) {
if size < 1 {
size = bufferSize
}
conn.bufferPool = buffer.AssignPool(size)
if s, ok := conn.codec.(BufferSize); ok {
s.SetBufferSize(size)
}
messages := conn.codec.Messages()
if set, ok := messages.(socket.BufferedInput); ok {
set.SetBufferedInput(size)
}
}
// SetNoCopy reuses a buffer from the pool for minimizing memory allocations.
func (conn *Conn) SetNoCopy(noCopy bool) {
conn.noCopy = noCopy
}
func (conn *Conn) write(call *Call) {
if conn.writeSched != nil {
conn.writeSched.Schedule(func() {
conn.send(call)
})
} else {
conn.send(call)
}
}
func (conn *Conn) send(call *Call) {
conn.mutex.Lock()
if conn.shutdown || conn.closing {
conn.mutex.Unlock()
call.Error = ErrShutdown
call.done()
return
}
seq := conn.seq
var isStreaming bool
var closeStreaming bool
if call.upgrade.Stream > 0 {
switch call.upgrade.Stream {
case openStream:
call.stream.seq = seq
conn.streams[seq] = call
case streaming:
isStreaming = true
seq = call.stream.seq
case closeStream:
closeStreaming = true
seq = call.stream.seq
}
}
if !isStreaming {
if !closeStreaming {
conn.seq++
}
conn.pending[seq] = call
}
conn.mutex.Unlock()
ctx := Context{}
ctx.Seq = seq
ctx.upgrade = call.upgrade
var upgradeBuffer []byte
if !call.upgrade.IsZero() {
upgradeBuffer = getUpgradeBuffer()
ctx.Upgrade, _ = call.upgrade.Marshal(upgradeBuffer)
}
ctx.ServiceMethod = call.ServiceMethod
err := conn.codec.WriteRequest(&ctx, call.Args)
if err != nil {
conn.mutex.Lock()
delete(conn.pending, seq)
if call.upgrade.Stream == openStream {
delete(conn.streams, seq)
}
conn.mutex.Unlock()
if call != nil {
call.Error = err
call.done()
}
}
if isStreaming {
PutCall(call)
}
putUpgradeBuffer(upgradeBuffer)
}
func (conn *Conn) recv() {
var err error
var messages = conn.codec.Messages()
var pipeline = scheduler.New(1, &scheduler.Options{Threshold: 2})
for err == nil {
ctx := getContext()
ctx.buffer = conn.bufferPool.GetBuffer(0)
ctx.data, err = messages.ReadMessage(ctx.buffer)
if err != nil {
break
}
if conn.directIO {
conn.read(ctx, false)
} else {
pipeline.Schedule(func() {
conn.read(ctx, true)
})
}
}
conn.mutex.Lock()
conn.shutdown = true
if err == io.EOF {
err = ErrShutdown
}
for _, call := range conn.pending {
call.Error = err
call.done()
}
for _, call := range conn.streams {
if call.stream != nil {
call.stream.stop()
}
}
conn.mutex.Unlock()
if conn.readSched != nil {
conn.readSched.Close()
}
if conn.writeSched != nil {
conn.writeSched.Close()
}
if conn.readStream != nil {
conn.readStream.Close()
}
pipeline.Close()
}
func (conn *Conn) read(ctx *Context, async bool) {
var err error
err = conn.codec.ReadResponseHeader(ctx)
if err != nil {
return
}
seq := ctx.Seq
conn.mutex.Lock()
if conn.shutdown {
conn.mutex.Unlock()
return
}
call := conn.pending[seq]
if call != nil && call.upgrade.Stream != openStream && call.upgrade.Stream != streaming {
delete(conn.pending, seq)
}
conn.mutex.Unlock()
switch {
case call == nil:
err = conn.codec.ReadResponseBody(nil, nil)
if err != nil {
err = errors.New("reading error body: " + err.Error())
}
conn.bufferPool.PutBuffer(ctx.buffer)
putContext(ctx)
case len(ctx.Error) > 0:
if ctx.Error == shutdownMsg {
call.Error = ErrShutdown
} else {
call.Error = errors.New(ctx.Error)
}
err = conn.codec.ReadResponseBody(nil, nil)
if err != nil {
err = errors.New("reading error body: " + err.Error())
}
call.done()
conn.bufferPool.PutBuffer(ctx.buffer)
putContext(ctx)
default:
u := call.upgrade
if u.NoResponse == noResponse {
if u.Heartbeat == heartbeat {
call.done()
putUpgrade(u)
} else if u.Stream == closeStream {
conn.mutex.Lock()
if _, ok := conn.streams[ctx.Seq]; ok {
delete(conn.pending, seq)
}
delete(conn.streams, ctx.Seq)
conn.mutex.Unlock()
call.done()
putUpgrade(u)
} else if u.Stream == streaming {
if conn.directIO {
call.Value = GetBuffer(len(ctx.value))
copy(call.Value, ctx.value)
call.streaming()
} else {
conn.readStream.Schedule(func() {
call.Value = GetBuffer(len(ctx.value))
copy(call.Value, ctx.value)
call.streaming()
conn.bufferPool.PutBuffer(ctx.buffer)
putContext(ctx)
})
return
}
} else if u.Stream == openStream {
call.done()
}
conn.bufferPool.PutBuffer(ctx.buffer)
putContext(ctx)
return
}
putUpgrade(u)
if conn.readSched != nil {
conn.readSched.Schedule(func() {
conn.finishCall(ctx, call, seq)
})
} else if async {
scheduler.Schedule(func() {
conn.finishCall(ctx, call, seq)
})
} else {
conn.finishCall(ctx, call, seq)
}
}
}
func (conn *Conn) finishCall(ctx *Context, call *Call, seq uint64) {
if len(ctx.value) > 0 {
if cap(call.Buffer) >= len(ctx.value) {
call.Value = call.Buffer[:len(ctx.value)]
} else {
call.Value = make([]byte, len(ctx.value))
}
copy(call.Value, ctx.value)
}
err := conn.codec.ReadResponseBody(call.Value, call.Reply)
if err != nil {
call.Error = errors.New("reading body " + err.Error())
}
call.done()
buf := ctx.buffer
conn.bufferPool.PutBuffer(buf)
putContext(ctx)
}
// SetPipelining enables the client to use pipelining per connection.
func (conn *Conn) SetPipelining(enable bool) {
conn.writeSched = scheduler.New(1, &scheduler.Options{Threshold: 2})
conn.readSched = scheduler.New(1, &scheduler.Options{Threshold: 2})
}
// SetDirectIO disables async io.
func (conn *Conn) SetDirectIO(directIO bool) {
conn.directIO = directIO
if d, ok := conn.codec.(DirectIO); ok {
d.SetDirectIO(directIO)
}
}
// NumCalls returns the number of calls.
func (conn *Conn) NumCalls() (n uint64) {
conn.mutex.Lock()
p := uint64(len(conn.pending))
n = uint64(len(conn.streams))
if p > n {
n = p
}
conn.mutex.Unlock()
return
}
// Close calls the underlying codec's Close method. If the connection is already
// shutting down, ErrShutdown is returned.
func (conn *Conn) Close() (err error) {
conn.mutex.Lock()
if conn.closing {
conn.mutex.Unlock()
return ErrShutdown
}
conn.closing = true
conn.mutex.Unlock()
err = conn.codec.Close()
return
}
// RoundTrip executes a single RPC transaction, returning
// a Response for the provided Request.
func (conn *Conn) RoundTrip(call *Call) *Call {
done := call.Done
done = checkDone(done)
call.upgrade = getUpgrade()
call.Done = done
conn.write(call)
return call
}
// Go invokes the function asynchronously. It returns the Call structure representing
// the invocation. The done channel will signal when the call is complete by returning
// the same Call object. If done is nil, Go will allocate a new channel.
// If non-nil, done must be buffered or Go will deliberately crash.
func (conn *Conn) Go(serviceMethod string, args interface{}, reply interface{}, done chan *Call) *Call {
call := callPool.Get().(*Call)
call.ServiceMethod = serviceMethod
call.Args = args
call.Reply = reply
call.upgrade = getUpgrade()
done = checkDone(done)
call.Done = done
conn.write(call)
return call
}
// Call invokes the named function, waits for it to complete, and returns its error status.
func (conn *Conn) Call(serviceMethod string, args interface{}, reply interface{}) error {
call := GetCall()
call.ServiceMethod = serviceMethod
call.Args = args
call.Reply = reply
call.upgrade = getUpgrade()
conn.write(call)
<-call.Done
err := call.Error
PutCall(call)
return err
}
// CallWithContext acts like Call but takes a context.
func (conn *Conn) CallWithContext(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) error {
call := GetCall()
call.Buffer = GetContextBuffer(ctx)
call.ServiceMethod = serviceMethod
call.Args = args
call.Reply = reply
call.upgrade = getUpgrade()
conn.write(call)
var err error
select {
case <-call.Done:
err = call.Error
PutCall(call)
case <-ctx.Done():
err = ctx.Err()
}
return err
}
// NewStream creates a new Stream for the client side.
func (conn *Conn) NewStream(serviceMethod string) (Stream, error) {
stream := &stream{unmarshal: conn.codec.ReadResponseBody, noCopy: conn.noCopy}
stream.cond.L = &stream.mut
upgrade := getUpgrade()
upgrade.NoRequest = noRequest
upgrade.NoResponse = noResponse
upgrade.Stream = openStream
call := new(Call)
call.ServiceMethod = serviceMethod
call.upgrade = upgrade
call.Done = make(chan *Call, 1)
call.stream = stream
conn.write(call)
var err error
<-call.Done
stream.done = true
call.upgrade.NoRequest = 0
call.upgrade.Stream = streaming
call.ServiceMethod = ""
stream.write = func(m interface{}) (err error) {
streamCall := callPool.Get().(*Call)
streamCall.upgrade = call.upgrade
streamCall.stream = stream
streamCall.Args = m
conn.write(streamCall)
return
}
stream.close = func() error {
return conn.closeStream(stream)
}
err = call.Error
if err != nil {
stream.Close()
return nil, err
}
return stream, err
}
// closeStream stops the stream.
func (conn *Conn) closeStream(s *stream) error {
upgrade := getUpgrade()
upgrade.NoRequest = noRequest
upgrade.NoResponse = noResponse
upgrade.Stream = closeStream
call := GetCall()
call.stream = s
call.upgrade = upgrade
conn.write(call)
<-call.Done
err := call.Error
PutCall(call)
return err
}
// Ping is NOT ICMP ping, this is just used to test whether a connection is still alive.
func (conn *Conn) Ping() error {
upgrade := getUpgrade()
upgrade.NoRequest = noRequest
upgrade.NoResponse = noResponse
upgrade.Heartbeat = heartbeat
call := GetCall()
call.upgrade = upgrade
conn.write(call)
runtime.Gosched()
<-call.Done
err := call.Error
PutCall(call)
return err
}
// ResetDone resets the done.
func ResetDone(done chan *Call) {
for len(done) > 0 {
onceDone(done)
}
}
func onceDone(done chan *Call) {
select {
case <-done:
default:
}
}
func checkDone(done chan *Call) chan *Call {
if done == nil {
return make(chan *Call, 10)
}
if cap(done) == 0 {
panic("rpc: done channel is unbuffered")
}
return done
}