-
Notifications
You must be signed in to change notification settings - Fork 10
/
open-stream-via-link.go
158 lines (135 loc) · 3.71 KB
/
open-stream-via-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
package transport_controller
import (
"context"
"sync"
"time"
"github.com/aperturerobotics/bifrost/link"
"github.com/aperturerobotics/bifrost/peer"
"github.com/aperturerobotics/controllerbus/directive"
)
// openStreamViaLinkResolver resolves OpenStreamViaLink directives
type openStreamViaLinkResolver struct {
c *Controller
ctx context.Context
di directive.Instance
dir link.OpenStreamViaLink
}
// 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 *openStreamViaLinkResolver) Resolve(ctx context.Context, handler directive.ResolverHandler) error {
c := o.c
lnkUUID := o.dir.OpenStreamViaLinkUUID()
openOpts := o.dir.OpenStreamViaLinkOpenOpts()
protocolID := o.dir.OpenStreamViaLinkProtocolID()
estMsg := NewStreamEstablish(protocolID)
tptID := o.dir.OpenStreamViaLinkTransportConstraint()
if tptID != 0 {
tpt, err := o.c.GetTransport(ctx)
if err != nil {
return err
}
if tpt.GetUUID() != tptID {
return nil
}
}
errCh := make(chan error, 1)
strmCh := make(chan link.MountedStream, 1)
var mtx sync.Mutex
var done bool
c.mtx.Lock()
linkWaiterCallback := func(establishedLink link.Link, successfullyAdded bool) {
correctLink := establishedLink.GetUUID() == lnkUUID
if !correctLink || !successfullyAdded || ctx.Err() != nil {
return
}
// Check if the operation was already completed.
mtx.Lock()
operationAlreadyCompleted := done
mtx.Unlock()
// If so, no further action is needed.
if operationAlreadyCompleted {
return
}
// Attempt to open a stream with the link.
stream, streamErr := establishedLink.OpenStream(openOpts)
if streamErr != nil {
errCh <- streamErr
return
}
_ = stream.SetWriteDeadline(time.Now().Add(streamEstablishTimeout))
_, headerWriteErr := writeStreamEstablishHeader(stream, estMsg)
if headerWriteErr != nil {
errCh <- headerWriteErr
stream.Close()
return
}
_ = stream.SetDeadline(time.Time{})
mtx.Lock()
if done {
stream.Close()
operationAlreadyCompleted = true
} else {
done = true
}
mtx.Unlock()
if !operationAlreadyCompleted {
o.c.le.
WithField("link-id", establishedLink.GetUUID()).
WithField("protocol-id", protocolID).
Debug("opened stream with peer")
strmCh <- newMountedStream(stream, openOpts, protocolID, establishedLink)
}
}
// Register the callback and wait for the link to establish.
lw := c.pushLinkWaiter(peer.ID(""), false, linkWaiterCallback)
// Unlock mutex
c.mtx.Unlock()
if lw != nil {
defer func() {
c.mtx.Lock()
c.clearLinkWaiter(lw)
c.mtx.Unlock()
}()
}
select {
case <-ctx.Done():
return nil
case err := <-errCh:
return err
case mstrm := <-strmCh:
if _, accepted := handler.AddValue(mstrm); !accepted {
mstrm.GetStream().Close()
}
return nil
}
}
// resolveOpenStreamViaLink returns a resolver for opening a stream.
// Negotiates the protocol ID as well.
func (c *Controller) resolveOpenStreamViaLink(
ctx context.Context,
di directive.Instance,
dir link.OpenStreamViaLink,
) ([]directive.Resolver, error) {
// opportune moment: if tpt is already available, filter
tptID := dir.OpenStreamViaLinkTransportConstraint()
if tptID != 0 {
if tpt := c.tptCtr.GetValue(); tpt != nil {
if (*tpt).GetUUID() != tptID {
return nil, nil
}
}
}
// Check transport constraint
// Return resolver.
return directive.Resolvers(&openStreamViaLinkResolver{
c: c,
ctx: ctx,
di: di,
dir: dir,
}), nil
}
// _ is a type assertion
var _ directive.Resolver = ((*openStreamViaLinkResolver)(nil))