-
Notifications
You must be signed in to change notification settings - Fork 10
/
open-stream.go
144 lines (126 loc) · 3.25 KB
/
open-stream.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
package transport_controller
import (
"context"
"time"
"github.com/aperturerobotics/bifrost/link"
"github.com/aperturerobotics/bifrost/transport"
"github.com/aperturerobotics/controllerbus/directive"
)
// openStreamResolver resolves OpenStream directives
type openStreamResolver struct {
c *Controller
ctx context.Context
di directive.Instance
dir link.OpenStreamWithPeer
}
// 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 *openStreamResolver) Resolve(ctx context.Context, handler directive.ResolverHandler) error {
c := o.c
openOpts := o.dir.OpenStreamWPOpenOpts()
protocolID := o.dir.OpenStreamWPProtocolID()
estMsg := NewStreamEstablish(protocolID)
tpt, err := o.c.GetTransport(ctx)
if err != nil {
return err
}
if !checkOpenStreamMatchesTpt(o.dir, tpt) {
return nil
}
errCh := make(chan error, 1)
strmCh := make(chan link.MountedStream, 1)
c.mtx.Lock()
lw := c.pushLinkWaiter(
o.dir.OpenStreamWPTargetPeerID(),
true,
func(lnk link.Link, added bool) {
select {
case <-ctx.Done():
return
default:
}
strm, err := lnk.OpenStream(openOpts)
if err != nil {
errCh <- err
/*
if strm != nil {
strm.Close()
}
*/
return
}
_ = strm.SetWriteDeadline(time.Now().Add(streamEstablishTimeout))
if _, err := writeStreamEstablishHeader(strm, estMsg); err != nil {
errCh <- err
strm.Close()
return
}
_ = strm.SetDeadline(time.Time{})
o.c.le.
WithField("link-id", lnk.GetUUID()).
WithField("protocol-id", protocolID).
Debug("opened stream with peer")
strmCh <- newMountedStream(strm, openOpts, protocolID, lnk)
},
)
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
}
}
// checkOpenStreamMatchesTpt checks if a OpenStream matches a tpt
func checkOpenStreamMatchesTpt(dir link.OpenStreamWithPeer, tpt transport.Transport) bool {
if tptConstraint := dir.OpenStreamWPTransportConstraint(); tptConstraint != 0 {
if tpt.GetUUID() != tptConstraint {
return false
}
}
// Check peer ID constraint
if srcPeerID := dir.OpenStreamWPSourcePeerID(); len(srcPeerID) != 0 {
if srcPeerID != tpt.GetPeerID() {
return false
}
}
return true
}
// resolveOpenStreamWithPeer returns a resolver for opening a stream.
// Negotiates the protocol ID as well.
func (c *Controller) resolveOpenStreamWithPeer(
ctx context.Context,
di directive.Instance,
dir link.OpenStreamWithPeer,
) ([]directive.Resolver, error) {
if tpt := c.tptCtr.GetValue(); tpt != nil {
if !checkOpenStreamMatchesTpt(dir, *tpt) {
return nil, nil
}
}
// Check transport constraint
// Return resolver.
return directive.Resolvers(&openStreamResolver{
c: c,
ctx: ctx,
di: di,
dir: dir,
}), nil
}
// _ is a type assertion
var _ directive.Resolver = ((*openStreamResolver)(nil))