-
Notifications
You must be signed in to change notification settings - Fork 10
/
establish-link.go
165 lines (149 loc) · 4.05 KB
/
establish-link.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
package transport_controller
import (
"context"
"time"
"github.com/aperturerobotics/bifrost/link"
"github.com/aperturerobotics/bifrost/transport/common/dialer"
"github.com/aperturerobotics/controllerbus/directive"
)
// crossDialWaitDur is the default amount of time to wait to avoid cross-dial.
var crossDialWaitDur = time.Millisecond * 250
// establishLinkResolver resolves establishLink directives
type establishLinkResolver struct {
c *Controller
ctx context.Context
di directive.Instance
dir link.EstablishLinkWithPeer
}
// Resolve resolves the values, emitting them to the handler.
// The resolver may be canceled and restarted multiple times.
// Any fatal error resolving the value is returned.
// The resolver will not be retried after returning an error.
// Values will be maintained from the previous call.
func (o *establishLinkResolver) Resolve(ctx context.Context, handler directive.ResolverHandler) error {
c := o.c
targetPeerID := o.dir.EstablishLinkTargetPeerId()
tpt, err := o.c.GetTransport(ctx)
if err != nil {
return err
}
// If the directive filtered by source peer ID, add a EstablishLink
// directive for the destination as well to ensure there is a reference.
//
// XXX: the current approach to using EstablishLink to determine the
// lifecycle of the Link (when it is released) is a bit messy and
// counter-intuitive and could be improved by instead using a refcount
// mechanism to release links when no directives ask for them.
sourcePeerID := tpt.GetPeerID()
if o.dir.EstablishLinkSourcePeerId() != sourcePeerID {
_, estLinkRef, err := o.c.bus.AddDirective(link.NewEstablishLinkWithPeer(sourcePeerID, targetPeerID), nil)
if err != nil {
return err
}
defer estLinkRef.Release()
}
wakeDialer := make(chan time.Time, 1)
linkIDs := make(map[link.Link]uint32)
c.mtx.Lock()
lw := o.c.pushLinkWaiter(targetPeerID, false, func(lnk link.Link, added bool) {
if added {
if _, ok := linkIDs[lnk]; !ok {
if vid, ok := handler.AddValue(lnk); ok {
linkIDs[lnk] = vid
}
}
} else {
if vid, ok := linkIDs[lnk]; ok {
handler.RemoveValue(vid)
delete(linkIDs, lnk)
if len(linkIDs) == 0 {
var extraWaitDur time.Duration
if lnk.GetLocalPeer() < lnk.GetRemotePeer() {
extraWaitDur = crossDialWaitDur
}
select {
case wakeDialer <- time.Now().Add(extraWaitDur):
default:
}
}
}
}
})
c.mtx.Unlock()
// Remove the link waiter when the resolver exits.
defer func() {
c.mtx.Lock()
o.c.clearLinkWaiter(lw)
c.mtx.Unlock()
}()
// Attempt to dial the peer if no link is active and we have an address to dial.
tptDialer, ok := tpt.(dialer.TransportDialer)
if !ok {
// No transport dialer, just wait for a link.
<-ctx.Done()
return nil
}
dialerOpts, err := tptDialer.GetPeerDialer(ctx, targetPeerID)
if err != nil {
return err
}
if dialerOpts.GetAddress() == "" {
// No address, wait for a link.
<-ctx.Done()
return nil
}
var waitWake bool
for {
if waitWake {
select {
case <-ctx.Done():
return nil
case waitUntil := <-wakeDialer:
tu := time.Until(waitUntil)
if tu > time.Millisecond*50 {
tt := time.NewTimer(tu)
select {
case <-ctx.Done():
tt.Stop()
return nil
case <-tt.C:
}
}
}
} else {
waitWake = true
}
var hasLink bool
for _, lnk := range c.links {
if lnk.lnk.GetRemotePeer() == targetPeerID {
hasLink = true
break
}
}
if !hasLink {
if err := c.PushDialer(ctx, targetPeerID, dialerOpts); err != nil {
return err
}
}
}
}
// resolveEstablishLink returns a resolver for opening a stream.
// Negotiates the protocol ID as well.
func (c *Controller) resolveEstablishLink(
ctx context.Context,
di directive.Instance,
dir link.EstablishLinkWithPeer,
) ([]directive.Resolver, error) {
if len(dir.EstablishLinkTargetPeerId()) == 0 {
return nil, nil
}
// Return resolver.
return directive.Resolvers(&establishLinkResolver{
c: c,
ctx: ctx,
di: di,
dir: dir,
}), nil
}
// _ is a type assertion
var _ directive.Resolver = ((*establishLinkResolver)(nil))