-
Notifications
You must be signed in to change notification settings - Fork 10
/
dial-tpt-addr.go
148 lines (136 loc) · 3.28 KB
/
dial-tpt-addr.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
package transport_controller
import (
"context"
"time"
"github.com/aperturerobotics/bifrost/link"
"github.com/aperturerobotics/bifrost/tptaddr"
"github.com/aperturerobotics/bifrost/transport/common/dialer"
"github.com/aperturerobotics/controllerbus/directive"
)
// dialTptAddrResolver resolves DialTptAddr directives
type dialTptAddrResolver struct {
c *Controller
ctx context.Context
di directive.Instance
dir tptaddr.DialTptAddr
}
// 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 *dialTptAddrResolver) Resolve(ctx context.Context, handler directive.ResolverHandler) error {
tpt, err := o.c.GetTransport(ctx)
if err != nil {
return err
}
tptDialer, ok := tpt.(dialer.TransportDialer)
if !ok {
return nil
}
transportID, dialAddr, err := tptaddr.ParseTptAddr(o.dir.DialTptAddr())
if err != nil {
return nil
}
if !tptDialer.MatchTransportType(transportID) {
return nil
}
c := o.c
destPeerID := o.dir.DialTptAddrTargetPeerId()
wakeDialer := make(chan time.Time, 1)
linkIDs := make(map[link.Link]uint32)
c.mtx.Lock()
lw := o.c.pushLinkWaiter(destPeerID, 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()
defer func() {
c.mtx.Lock()
o.c.clearLinkWaiter(lw)
_ = o.c.clearLinkDialerLocked(linkDialerKey{
peerID: destPeerID.String(),
dialAddress: dialAddr,
})
c.mtx.Unlock()
}()
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
}
c.mtx.Lock()
// If we already have a link, do nothing.
var hasLink bool
for _, lnk := range c.links {
if lnk.lnk.GetRemotePeer() == destPeerID {
hasLink = true
break
}
}
if !hasLink {
err = c.startLinkDialerLocked(destPeerID, &dialer.DialerOpts{
Address: dialAddr,
}, tptDialer)
}
c.mtx.Unlock()
if err != nil {
return err
}
}
}
// resolveDialTptAddr returns a resolver for dialing a transport address.
func (c *Controller) resolveDialTptAddr(
ctx context.Context,
di directive.Instance,
dir tptaddr.DialTptAddr,
) ([]directive.Resolver, error) {
if len(dir.DialTptAddrTargetPeerId()) == 0 || dir.DialTptAddr() == "" {
return nil, nil
}
// Return resolver.
return directive.Resolvers(&dialTptAddrResolver{
c: c,
ctx: ctx,
di: di,
dir: dir,
}), nil
}
// _ is a type assertion
var _ directive.Resolver = ((*dialTptAddrResolver)(nil))