-
Notifications
You must be signed in to change notification settings - Fork 10
/
lookup-transport.go
73 lines (62 loc) · 1.87 KB
/
lookup-transport.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
package transport_controller
import (
"context"
"github.com/aperturerobotics/bifrost/transport"
"github.com/aperturerobotics/controllerbus/directive"
)
// lookupTransportResolver resolves lookupTransport directives
type lookupTransportResolver struct {
c *Controller
ctx context.Context
dir transport.LookupTransport
}
// 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 *lookupTransportResolver) Resolve(ctx context.Context, handler directive.ResolverHandler) error {
tpt, err := o.c.GetTransport(ctx)
if err != nil {
return err
}
if !checkLookupMatchesTpt(o.dir, tpt) {
return nil
}
handler.AddValue(tpt)
return nil
}
// checkLookupMatchesTpt checks if a lookuptransport matches a tpt
func checkLookupMatchesTpt(dir transport.LookupTransport, tpt transport.Transport) bool {
if tptIDConstraint := dir.LookupTransportIDConstraint(); tptIDConstraint != 0 {
if tpt.GetUUID() != tptIDConstraint {
return false
}
}
if peerIDConstraint := dir.LookupTransportPeerIDConstraint(); len(peerIDConstraint) != 0 {
if tpt.GetPeerID() != peerIDConstraint {
return false
}
}
return true
}
// resolveLookupTransport returns a resolver for looking up a transport.
func (c *Controller) resolveLookupTransport(
ctx context.Context,
di directive.Instance,
dir transport.LookupTransport,
) ([]directive.Resolver, error) {
if tpt := c.tptCtr.GetValue(); tpt != nil {
if !checkLookupMatchesTpt(dir, *tpt) {
return nil, nil
}
}
// Return resolver.
return directive.Resolvers(&lookupTransportResolver{
c: c,
ctx: ctx,
dir: dir,
}), nil
}
// _ is a type assertion
var _ directive.Resolver = ((*lookupTransportResolver)(nil))