/
conn.go
1060 lines (949 loc) · 29.3 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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package conn
import (
"fmt"
"net"
"sync"
"time"
v23 "v.io/v23"
"v.io/v23/context"
"v.io/v23/flow"
"v.io/v23/flow/message"
"v.io/v23/naming"
"v.io/v23/rpc/version"
"v.io/v23/security"
"v.io/v23/verror"
"v.io/x/lib/vlog"
slib "v.io/x/ref/lib/security"
rpcversion "v.io/x/ref/runtime/internal/rpc/version"
)
const (
invalidFlowID = iota
blessingsFlowID
reservedFlows = 10
)
// minChannelTimeout keeps track of minimum values that we allow for channel
// timeout on a per-protocol basis. This is to prevent people setting some
// overall limit that doesn't make sense for very slow protocols.
// TODO(mattr): We should consider allowing users to set this per-protocol, or
// perhaps having the protocol implementation expose it via some kind of
// ChannelOpts interface.
var minChannelTimeout = map[string]time.Duration{
"bt": 10 * time.Second,
}
const (
proxyOverhead = 32
// DefaultBytesBuffered is the default value used for Opts.BytesBuffered
DefaultBytesBuffered = 1 << 20
// DefaultMTU is the default value used for Opts.MTU
DefaultMTU = 1 << 16
// DefaultChannelTimeout is the default value used for Opts.ChannelTimeout
DefaultChannelTimeout = 30 * time.Minute
// DefaultHandshakeTimeout is the default value used for Opts.HandshakeTimeout
DefaultHandshakeTimeout = time.Minute
)
// A FlowHandler processes accepted flows.
type FlowHandler interface {
// HandleFlow processes an accepted flow.
HandleFlow(flow.Flow) error
}
type Status int
// Note that this is a progression of states that can only
// go in one direction. We use inequality operators to see
// how far along in the progression we are, so the order of
// these is important.
const (
Active Status = iota
EnteringLameDuck
LameDuckAcknowledged
Closing
Closed
)
type healthCheckState struct {
requestSent time.Time
requestTimer *time.Timer
requestDeadline time.Time
lastRTT time.Duration
closeTimer *time.Timer
closeDeadline time.Time
}
// A Conn acts as a multiplexing encrypted channel that can host Flows.
type Conn struct {
// All the variables here are set before the constructor returns
// and never changed after that.
mp *messagePipe
version version.RPCVersion
local, remote naming.Endpoint
remoteAddr net.Addr
closed chan struct{}
lameDucked chan struct{}
blessingsFlow *blessingsFlow
loopWG sync.WaitGroup
unopenedFlows sync.WaitGroup
cancel context.CancelFunc
handler FlowHandler
mtu uint64
// The following fields for managing flow control and the writeq
// are locked independently.
flowControl flowControlConnStats
writeq writeq
releaseSender messageSender // use for release messages
healthAndLameDuckSender messageSender // use for health+lameduck
setupCloseSender messageSender // use for setup, auth, close and teardown
mu sync.Mutex // All the variables below here are protected by mu.
localBlessings, remoteBlessings security.Blessings
localDischarges, remoteDischarges security.Discharges
localValid <-chan struct{}
remoteValid chan struct{}
rPublicKey security.PublicKey
status Status
remoteLameDuck bool
nextFid uint64
lastUsedTime time.Time
flows map[uint64]*flw
hcstate *healthCheckState
acceptChannelTimeout time.Duration
}
type messageSender struct {
sync.Mutex
writer
}
func (ms *messageSender) wait(ctx *context.T, q *writeq) (*writer, error) {
w := &ms.writer
if err := q.wait(nil, w, expressPriority); err != nil {
return nil, err
}
return w, nil
}
// Ensure that *Conn implements flow.ManagedConn.
var _ flow.ManagedConn = &Conn{}
type Opts struct {
// Proxy controls whether the connection is for a proxy or a direct connection.
Proxy bool
// HandshakeTimeout is the time allowed for establishing a connection to a peer.
HandshakeTimeout time.Duration
// ChannelTimeout is the timeout used for healthchecks.
ChannelTimeout time.Duration
// MTU defines the MTU size to use for this connection. This may be reduced
// if the connection's peer requests a smaller MTU.
MTU uint64
// BytesBuffered defines the default number of bytes that can be buffered
// by a single flow before flow control is invoked.
BytesBuffered uint64
}
func (co *Opts) initValues(protocol string) error {
if co.ChannelTimeout == 0 {
co.ChannelTimeout = DefaultChannelTimeout
}
if min := minChannelTimeout[protocol]; co.ChannelTimeout < min {
co.ChannelTimeout = min
}
if co.MTU == 0 {
co.MTU = DefaultMTU
}
if co.BytesBuffered == 0 {
co.BytesBuffered = DefaultBytesBuffered
}
if co.HandshakeTimeout == 0 {
co.HandshakeTimeout = DefaultHandshakeTimeout
}
if co.ChannelTimeout == 0 {
co.ChannelTimeout = DefaultChannelTimeout
}
if co.MTU > co.BytesBuffered {
return fmt.Errorf("mtu of: %v, is larger than bytes buffered per flow: %v", co.MTU, co.BytesBuffered)
}
return nil
}
// NewDialed dials a new Conn on the given conn. In the case when it is not
// dialing a proxy, it can return an error indicating that the context was canceled
// (verror.ErrCanceled) along with a handshake completes within the
// specified handshakeTimeout duration. Or put another way, NewDialed will
// always waut for at most handshakeTimeout duration to complete the handshake
// even if the context is canceled. The behaviour is different for a proxy
// connection, in which case a cancelation is immediate and no attempt is made
// to establish the connection.
func NewDialed(
ctx *context.T,
conn flow.MsgReadWriteCloser,
local, remote naming.Endpoint,
versions version.RPCVersionRange,
auth flow.PeerAuthorizer,
handler FlowHandler,
opts Opts,
) (c *Conn, names []string, rejected []security.RejectedBlessing, err error) {
if _, err := version.CommonVersion(ctx, rpcversion.Supported, versions); err != nil {
return nil, nil, nil, err
}
if err := opts.initValues(local.Protocol); err != nil {
return nil, nil, nil, err
}
var remoteAddr net.Addr
if flowConn, ok := conn.(flow.Conn); ok {
remoteAddr = flowConn.RemoteAddr()
}
dctx := ctx
ctx, cancel := context.WithRootCancel(ctx)
// If the conn is being built on an encapsulated flow, we must update the
// cancellation of the flow, to ensure that the conn doesn't get killed
// when the context passed in is cancelled.
if f, ok := conn.(*flw); ok {
ctx = f.SetDeadlineContext(ctx, time.Time{})
}
c = &Conn{
mp: newMessagePipe(conn),
handler: handler,
local: local,
remote: remote,
remoteAddr: remoteAddr,
closed: make(chan struct{}),
lameDucked: make(chan struct{}),
nextFid: reservedFlows,
flows: map[uint64]*flw{},
lastUsedTime: time.Now(),
cancel: cancel,
acceptChannelTimeout: opts.ChannelTimeout,
mtu: opts.MTU,
}
c.initWriters()
c.flowControl.init(opts.BytesBuffered)
handshakeCh := make(chan dialHandshakeResult, 1)
var handshakeResult dialHandshakeResult
c.loopWG.Add(1)
go c.dialHandshake(ctx, versions, auth, handshakeCh)
var canceled bool
timer := time.NewTimer(opts.HandshakeTimeout)
select {
case handshakeResult = <-handshakeCh:
err = handshakeResult.err
case <-timer.C:
err = verror.ErrTimeout.Errorf(ctx, "timeout")
case <-dctx.Done():
if opts.Proxy {
err = verror.ErrCanceled.Errorf(ctx, "canceled")
} else {
// The context has been canceled, but let's give this connection
// an opportunity to run to completion just in case this connection
// is racing to become established as per the race documented in:
// https://github.com/vanadium/core/issues/40.
select {
case handshakeResult = <-handshakeCh:
canceled = true
// There is always the possibility that the handshake fails
// (eg. the client doesn't trust the server), so make sure to
// record any such error.
err = handshakeResult.err
// Handshake done.
case <-timer.C:
// Report the timeout not the cancelation, hence
// leave canceled as false.
err = verror.ErrTimeout.Errorf(ctx, "timeout")
}
}
}
timer.Stop()
if err != nil {
c.Close(ctx, err)
return nil, nil, nil, err
}
c.initializeHealthChecks(ctx, handshakeResult.rtt)
// We send discharges asynchronously to prevent making a second RPC while
// trying to build up the connection for another. If the two RPCs happen to
// go to the same server a deadlock will result.
// This commonly happens when we make a Resolve call. During the Resolve we
// will try to fetch discharges to send to the mounttable, leading to a
// Resolve of the discharge server name. The two resolve calls may be to
// the same mounttable.
if handler != nil {
c.loopWG.Add(1)
go c.blessingsLoop(ctx, time.Now(), nil)
}
c.loopWG.Add(1)
go c.readLoop(ctx)
c.mu.Lock()
c.lastUsedTime = time.Now()
c.mu.Unlock()
if canceled {
err = verror.ErrCanceled.Errorf(ctx, "canceled")
}
return c, handshakeResult.names, handshakeResult.rejected, err
}
// NewAccepted accepts a new Conn on the given conn.
//
// NOTE: that the FlowHandler must be called asynchronously since it may
//
// block until this function returns.
func NewAccepted(
ctx *context.T,
lAuthorizedPeers []security.BlessingPattern,
conn flow.MsgReadWriteCloser,
local naming.Endpoint,
versions version.RPCVersionRange,
handler FlowHandler,
opts Opts) (*Conn, error) {
if _, err := version.CommonVersion(ctx, rpcversion.Supported, versions); err != nil {
return nil, err
}
if err := opts.initValues(local.Protocol); err != nil {
return nil, err
}
var remoteAddr net.Addr
if flowConn, ok := conn.(flow.Conn); ok {
remoteAddr = flowConn.RemoteAddr()
}
ctx, cancel := context.WithCancel(ctx)
c := &Conn{
mp: newMessagePipe(conn),
handler: handler,
local: local,
remoteAddr: remoteAddr,
closed: make(chan struct{}),
lameDucked: make(chan struct{}),
nextFid: reservedFlows + 1,
flows: map[uint64]*flw{},
lastUsedTime: time.Now(),
cancel: cancel,
acceptChannelTimeout: opts.ChannelTimeout,
mtu: opts.MTU,
}
c.initWriters()
c.flowControl.init(opts.BytesBuffered)
handshakeCh := make(chan acceptHandshakeResult, 1)
var handshakeResult acceptHandshakeResult
c.loopWG.Add(1)
go c.acceptHandshake(ctx, versions, lAuthorizedPeers, handshakeCh)
timer := time.NewTimer(opts.HandshakeTimeout)
var err error
select {
case handshakeResult = <-handshakeCh:
err = handshakeResult.err
case <-timer.C:
err = verror.ErrTimeout.Errorf(ctx, "timeout")
case <-ctx.Done():
err = verror.ErrCanceled.Errorf(ctx, "canceled")
}
timer.Stop()
if err != nil {
// Call internalClose with closedWhileAccepting set to true
// to avoid waiting on the go routine above to complete.
// This avoids blocking on the loopWG waitgroup which is
// pointless since we've decided to not wait on it!
c.internalClose(ctx, false, true, err)
<-c.closed
return nil, err
}
c.initializeHealthChecks(ctx, handshakeResult.rtt)
c.loopWG.Add(2)
// NOTE: there is a race for refreshTime since it gets set above
// in a goroutine but read here without any synchronization.
go c.blessingsLoop(ctx, handshakeResult.refreshTime, lAuthorizedPeers)
go c.readLoop(ctx)
c.mu.Lock()
c.lastUsedTime = time.Now()
c.mu.Unlock()
return c, nil
}
func (c *Conn) initWriters() {
c.setupCloseSender.notify = make(chan struct{})
}
func (c *Conn) blessingsLoop(
ctx *context.T,
refreshTime time.Time,
authorizedPeers []security.BlessingPattern) {
defer c.loopWG.Done()
for {
if refreshTime.IsZero() {
select {
case <-c.localValid:
case <-ctx.Done():
return
}
} else {
timer := time.NewTimer(time.Until(refreshTime))
select {
case <-timer.C:
case <-c.localValid:
case <-ctx.Done():
timer.Stop()
return
}
timer.Stop()
}
var discharges security.Discharges
blessings, valid := v23.GetPrincipal(ctx).BlessingStore().Default()
discharges, refreshTime = slib.PrepareDischarges(ctx, blessings, nil, "", nil)
// Need to access the underlying message pipe with the connections
// lock held.
bkey, dkey, err := c.blessingsFlow.send(ctx, blessings, discharges, authorizedPeers)
if err != nil {
c.internalClose(ctx, false, false, err)
return
}
c.mu.Lock()
c.localBlessings = blessings
c.localDischarges = discharges
c.localValid = valid
c.mu.Unlock()
err = c.sendAuthMessage(ctx, message.Auth{
BlessingsKey: bkey,
DischargeKey: dkey,
})
if err != nil {
c.internalClose(ctx, false, false, err)
return
}
}
}
// MTU Returns the maximum transimission unit for the connection in bytes.
func (c *Conn) MTU() uint64 {
return c.mtu
}
// RTT returns the round trip time of a message to the remote end.
// Note the initial estimate of the RTT from the accepted side of a connection
// my be long because we don't fully factor out certificate verification time.
// The RTT will be updated with the receipt of every healthCheckResponse, so
// this overestimate doesn't remain for long when the channel timeout is low.
func (c *Conn) RTT() time.Duration {
c.mu.Lock()
defer c.mu.Unlock()
rtt := c.hcstate.lastRTT
if !c.hcstate.requestSent.IsZero() {
if waitRTT := time.Since(c.hcstate.requestSent); waitRTT > rtt {
rtt = waitRTT
}
}
return rtt
}
func (c *Conn) newHealthChecksLocked(ctx *context.T, firstRTT time.Duration) *healthCheckState {
now := time.Now()
h := &healthCheckState{
requestDeadline: now.Add(c.acceptChannelTimeout / 2),
closeTimer: time.AfterFunc(c.acceptChannelTimeout, func() {
c.internalClose(ctx, false, false, ErrChannelTimeout.Errorf(ctx, "the channel has become unresponsive"))
}),
closeDeadline: now.Add(c.acceptChannelTimeout),
lastRTT: firstRTT,
}
requestTimer := time.AfterFunc(c.acceptChannelTimeout/2, func() {
c.sendHealthCheckMessage(ctx, true)
c.mu.Lock()
h.requestSent = time.Now()
c.mu.Unlock()
})
h.requestTimer = requestTimer
return h
}
func (c *Conn) initializeHealthChecks(ctx *context.T, firstRTT time.Duration) {
c.mu.Lock()
defer c.mu.Unlock()
if c.hcstate != nil {
return
}
c.hcstate = c.newHealthChecksLocked(ctx, firstRTT)
}
func (c *Conn) healthCheckNewFlowLocked(ctx *context.T, timeout time.Duration) {
if timeout != 0 {
if c.hcstate == nil {
// There's a scheduling race between initializing healthchecks
// and accepting a connection since each is handled on a different
// goroutine. Hence there may be an attempt to update the health
// checks before they have been initialized. The simplest fix is
// to just initialize them here.
c.hcstate = c.newHealthChecksLocked(ctx, timeout)
return
}
if min := minChannelTimeout[c.local.Protocol]; timeout < min {
timeout = min
}
now := time.Now()
if rd := now.Add(timeout / 2); rd.Before(c.hcstate.requestDeadline) {
c.hcstate.requestDeadline = rd
c.hcstate.requestTimer.Reset(timeout / 2)
}
if cd := now.Add(timeout); cd.Before(c.hcstate.closeDeadline) {
c.hcstate.closeDeadline = cd
c.hcstate.closeTimer.Reset(timeout)
}
}
}
func (c *Conn) healthCheckCloseDeadline() time.Time {
c.mu.Lock()
defer c.mu.Unlock()
return c.hcstate.closeDeadline
}
// EnterLameDuck enters lame duck mode, the returned channel will be closed when
// the remote end has ack'd or the Conn is closed.
func (c *Conn) EnterLameDuck(ctx *context.T) chan struct{} {
var enterLameDuck bool
c.mu.Lock()
if c.status < EnteringLameDuck {
c.status = EnteringLameDuck
enterLameDuck = true
}
c.mu.Unlock()
if enterLameDuck {
err := c.sendLameDuckMessage(ctx, false, true)
if err != nil {
c.Close(ctx, ErrSend.Errorf(ctx, "failure sending release message to %v: %v", c.remote.String(), err))
}
}
return c.lameDucked
}
// Dial dials a new flow on the Conn.
func (c *Conn) Dial(ctx *context.T, blessings security.Blessings, discharges []security.Discharge,
remote naming.Endpoint, channelTimeout time.Duration, sideChannel bool) (flow.Flow, error) {
if c.remote.RoutingID == naming.NullRoutingID {
return nil, ErrDialingNonServer.Errorf(ctx, "attempting to dial on a connection with no remote server: %v", c.remote.String())
}
if blessings.IsZero() {
// its safe to ignore this error since c.lBlessings must be valid, so the
// encoding of the publicKey can never error out.
blessings, _ = security.NamelessBlessing(v23.GetPrincipal(ctx).PublicKey())
}
c.mu.Lock()
defer c.mu.Unlock()
if c.status >= Closing {
return nil, ErrConnectionClosed
}
// It may happen that in the case of bidirectional RPC the dialer of the connection
// has sent blessings, but not yet discharges. In this case we will wait for them
// to send the discharges before allowing a bidirectional flow dial.
if valid := c.remoteValid; valid != nil && len(c.remoteDischarges) == 0 && len(c.remoteBlessings.ThirdPartyCaveats()) > 0 {
c.mu.Unlock()
<-valid
c.mu.Lock()
}
if c.remoteLameDuck || c.status >= Closing {
return nil, ErrConnectionClosed.Errorf(ctx, "connection closed")
}
id := c.nextFid
c.nextFid += 2
remote = remote.WithBlessingNames(c.remote.BlessingNames())
flw := c.newFlowLocked(
ctx,
id,
blessings,
c.remoteBlessings,
discharges,
c.remoteDischarges,
remote,
true,
channelTimeout,
sideChannel,
0)
return flw, nil
}
// LocalEndpoint returns the local vanadium Endpoint
func (c *Conn) LocalEndpoint() naming.Endpoint { return c.local }
// RemoteEndpoint returns the remote vanadium Endpoint
func (c *Conn) RemoteEndpoint() naming.Endpoint {
return c.remote
}
// LocalBlessings returns the local blessings.
func (c *Conn) LocalBlessings() security.Blessings {
c.mu.Lock()
defer c.mu.Unlock()
return c.localBlessings
}
// RemoteBlessings returns the remote blessings.
func (c *Conn) RemoteBlessings() security.Blessings {
c.mu.Lock()
defer c.mu.Unlock()
return c.remoteBlessings
}
// LocalDischarges fetches the most recently sent discharges for the local
// ends blessings.
func (c *Conn) LocalDischarges() security.Discharges {
c.mu.Lock()
defer c.mu.Unlock()
return c.localDischarges
}
// RemoteDischarges fetches the most recently received discharges for the remote
// ends blessings.
func (c *Conn) RemoteDischarges() security.Discharges {
c.mu.Lock()
defer c.mu.Unlock()
// It may happen that in the case of bidirectional RPC the dialer of the connection
// has sent blessings, but not yet discharges. In this case we will wait for them
// to send the discharges instead of returning the initial nil discharges.
if valid := c.remoteValid; valid != nil && len(c.remoteDischarges) == 0 && len(c.remoteBlessings.ThirdPartyCaveats()) > 0 {
c.mu.Unlock()
<-valid
c.mu.Lock()
}
return c.remoteDischarges
}
// CommonVersion returns the RPCVersion negotiated between the local and remote endpoints.
func (c *Conn) CommonVersion() version.RPCVersion { return c.version }
// LastUsed returns the time at which the Conn had bytes read or written on it.
func (c *Conn) LastUsed() time.Time {
c.mu.Lock()
defer c.mu.Unlock()
return c.lastUsedTime
}
// RemoteLameDuck returns true if the other end of the connection has announced that
// it is in lame duck mode indicating that new flows should not be dialed on this
// conn.
func (c *Conn) RemoteLameDuck() bool {
c.mu.Lock()
defer c.mu.Unlock()
return c.remoteLameDuck
}
// Closed returns a channel that will be closed after the Conn is shutdown.
// After this channel is closed it is guaranteed that all Dial calls will fail
// with an error and no more flows will be sent to the FlowHandler.
func (c *Conn) Closed() <-chan struct{} { return c.closed }
func (c *Conn) Status() Status {
c.mu.Lock()
defer c.mu.Unlock()
return c.status
}
// Close shuts down a conn.
func (c *Conn) Close(ctx *context.T, err error) {
c.internalClose(ctx, false, false, err)
<-c.closed
}
// CloseIfIdle closes the connection if the conn has been idle for idleExpiry,
// returning true if it closed it.
func (c *Conn) CloseIfIdle(ctx *context.T, idleExpiry time.Duration) bool {
c.mu.Lock()
defer c.mu.Unlock()
if c.isIdleLocked(ctx, idleExpiry) {
c.internalCloseLocked(ctx, false, false, ErrIdleConnKilled.Errorf(ctx, "connection killed because idle expiry was reached"))
return true
}
return false
}
func (c *Conn) IsIdle(ctx *context.T, idleExpiry time.Duration) bool {
c.mu.Lock()
defer c.mu.Unlock()
return c.isIdleLocked(ctx, idleExpiry)
}
// isIdleLocked returns true if the connection has been idle for idleExpiry.
func (c *Conn) isIdleLocked(ctx *context.T, idleExpiry time.Duration) bool {
if c.hasActiveFlowsLocked() {
return false
}
return c.lastUsedTime.Add(idleExpiry).Before(time.Now())
}
func (c *Conn) HasActiveFlows() bool {
c.mu.Lock()
defer c.mu.Unlock()
return c.hasActiveFlowsLocked()
}
func (c *Conn) hasActiveFlowsLocked() bool {
for _, f := range c.flows {
if !f.sideChannel {
return true
}
}
return false
}
func (c *Conn) internalClose(ctx *context.T, closedRemotely, closedWhileAccepting bool, err error) {
c.mu.Lock()
c.internalCloseLocked(ctx, closedRemotely, closedWhileAccepting, err)
c.mu.Unlock()
}
func (c *Conn) internalCloseAsync(ctx *context.T, flows map[uint64]*flw, closedRemotely, closedWhileAccepting bool, err error) {
if !closedRemotely {
msg := ""
if err != nil {
msg = err.Error()
}
cerr := c.sendTearDownMessage(ctx, msg)
if cerr != nil {
ctx.VI(2).Infof("Error sending tearDown on connection to %s: %v", c.remote, cerr)
}
}
if err == nil {
err = ErrConnectionClosed.Errorf(ctx, "connection closed")
}
for _, f := range flows {
f.close(ctx, false, err)
}
if cerr := c.mp.Close(); cerr != nil && ctx.V(2) {
ctx.Infof("Error closing underlying connection for %s: %v", c.remote, cerr)
}
if c.cancel != nil {
c.cancel()
}
if !closedWhileAccepting {
// given that the accept handshake timed out or was
// cancelled it doesn't make sense to wait for it here.
c.loopWG.Wait()
}
c.mu.Lock()
c.status = Closed
close(c.closed)
c.mu.Unlock()
}
func (c *Conn) internalCloseLocked(ctx *context.T, closedRemotely, closedWhileAccepting bool, err error) {
if ctx.V(2) {
ctx.Infof("Closing connection: %v", err)
}
if c.status >= Closing {
// This conn is already being torn down.
return
}
if c.status < LameDuckAcknowledged {
close(c.lameDucked)
}
c.status = Closing
if c.remoteValid != nil {
close(c.remoteValid)
c.remoteValid = nil
}
if c.hcstate != nil {
c.hcstate.requestTimer.Stop()
c.hcstate.closeTimer.Stop()
}
flows := c.flows
c.flows = nil
go c.internalCloseAsync(ctx, flows, closedRemotely, closedWhileAccepting, err)
}
func (c *Conn) state() Status {
c.mu.Lock()
defer c.mu.Unlock()
return c.status
}
// deleteFlow removes the specified flow id from the Conn's set of active
// flows. This has the following consequences for flow control:
// - flow control token releases received from a peer will be treated
// as 'borrowed' and assigned back to the shared pool
// (see releaseOutstandingBorrowed).
// - flow control token releases due to be sent to a remote peer will not be
// assigned to the local map used to keep track of the available tokens
// for that flow.
// - when the Conn is closed, this flow will not be used to calculate healthcheck
// timeouts.
func (c *Conn) deleteFlow(fid uint64) {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.flows, fid)
}
func (c *Conn) fragmentReleaseMessage(ctx *context.T, toRelease []message.Counter) error {
limit := c.flowControl.releaseMessageLimit
for {
if len(toRelease) < limit {
return c.sendReleaseMessage(ctx,
message.Release{Counters: toRelease})
}
if err := c.sendReleaseMessage(ctx,
message.Release{Counters: toRelease[:limit]}); err != nil {
return err
}
toRelease = toRelease[limit:]
}
}
func (c *Conn) sendRelease(ctx *context.T, fs *flowControlFlowStats, count uint64) {
fid := fs.id
c.mu.Lock()
_, open := c.flows[fid]
var toReleaseCounters []message.Counter
toRelease := c.flowControl.determineReleaseMessage(fs, count, open)
if toRelease {
toReleaseCounters = c.flowControl.toReleaseClosed
c.flowControl.mu.Lock()
for _, f := range c.flows {
if f.flowControl.toRelease != 0 {
toReleaseCounters = append(toReleaseCounters,
message.Counter{
FlowID: f.id,
Tokens: f.flowControl.toRelease})
}
f.flowControl.clearLocked()
}
fs.clearLocked()
fs.shared.clearToReleaseLocked()
c.flowControl.mu.Unlock()
}
c.mu.Unlock()
var err error
if toRelease {
err = c.fragmentReleaseMessage(ctx, toReleaseCounters)
}
if err != nil {
c.Close(ctx, ErrSend.Errorf(ctx, "failure sending release message to %v: %v", c.remote.String(), err))
}
}
func (c *Conn) readLoop(ctx *context.T) {
defer c.loopWG.Done()
var err error
for {
msg, nBuf, rerr := c.mp.readAnyMsg(ctx)
if rerr != nil {
putNetBuf(nBuf)
err = ErrRecv.Errorf(ctx, "error reading from: %v: %v", c.remote.String(), rerr)
break
}
if err = c.handleAnyMessage(ctx, msg, nBuf); err != nil {
break
}
}
if err != nil && verror.ErrorID(err) == ErrCounterOverflow.ID {
vlog.Infof("conn.readLoop: unexpected error: %v", err)
}
c.internalClose(ctx, false, false, err)
}
func (c *Conn) markUsed() {
c.mu.Lock()
c.markUsedLocked()
c.mu.Unlock()
}
func (c *Conn) markUsedLocked() {
c.lastUsedTime = time.Now()
}
func (c *Conn) IsEncapsulated() bool {
return c.mp.isEncapsulated()
}
func (c *Conn) DebugString() string {
c.mu.Lock()
defer c.mu.Unlock()
return fmt.Sprintf(`
Remote:
Endpoint %v
Blessings: %v (claimed)
PublicKey: %v
Local:
Endpoint: %v
Blessings: %v
Version: %v
MTU: %d
LastUsed: %v
#Flows: %d
`,
c.remote,
c.remoteBlessings,
c.rPublicKey,
c.local,
c.localBlessings,
c.version,
c.mtu,
c.lastUsedTime,
len(c.flows))
}
func (c *Conn) writeEncodedBlessings(ctx *context.T, w *writer, data []byte) error {
if err := c.writeq.wait(ctx, w, flowPriority); err != nil {
ctx.Infof("writeq.wait: error %v", err)
return err
}
// TODO(cnicolaou): what about fragmentation and flow control here?
err := c.mp.writeData(ctx, message.Data{
ID: blessingsFlowID,
Payload: data,
})
c.writeq.done(w)
return err
}
func (c *Conn) sendReleaseMessage(
ctx *context.T,
m message.Release) error {
c.releaseSender.Lock()
defer c.releaseSender.Unlock()
allocChannel(&c.releaseSender)
w, err := c.releaseSender.wait(nil, &c.writeq)
if err != nil {
ctx.Infof("writeq.wait: error %v", err)
return err
}
defer c.writeq.done(w)
return c.mp.writeRelease(ctx, m)
}
func (c *Conn) sendAuthMessage(ctx *context.T, m message.Auth) error {
c.setupCloseSender.Lock()
defer c.setupCloseSender.Unlock()
w, err := c.setupCloseSender.wait(ctx, &c.writeq)
if err != nil {
ctx.Infof("writeq.wait: error %v", err)
return err
}
defer c.writeq.done(w)
return c.mp.writeAnyMsg(ctx, m.Append)
}
func (c *Conn) sendSetupMessage(
ctx *context.T,
m message.Setup) error {
c.setupCloseSender.Lock()
defer c.setupCloseSender.Unlock()
w, err := c.setupCloseSender.wait(ctx, &c.writeq)
if err != nil {
ctx.Infof("writeq.wait: error %v", err)
return err
}
defer c.writeq.done(w)
return c.mp.writeSetup(ctx, m)
}
func allocChannel(ms *messageSender) {
if ms.notify == nil {
ms.notify = make(chan struct{})
}
}
func (c *Conn) sendHealthCheckMessage(
ctx *context.T,
request bool) error {
c.healthAndLameDuckSender.Lock()
defer c.healthAndLameDuckSender.Unlock()
allocChannel(&c.healthAndLameDuckSender)
w, err := c.healthAndLameDuckSender.wait(ctx, &c.writeq)
if err != nil {
ctx.Infof("writeq.wait: error %v", err)
return err
}