-
Notifications
You must be signed in to change notification settings - Fork 10
/
client-controller.go
154 lines (138 loc) · 4.21 KB
/
client-controller.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
package bifrost_rpc_access
import (
"context"
"regexp"
"time"
bifrost_rpc "github.com/aperturerobotics/bifrost/rpc"
"github.com/aperturerobotics/controllerbus/controller"
"github.com/aperturerobotics/controllerbus/directive"
"github.com/aperturerobotics/util/refcount"
"github.com/cenkalti/backoff"
"github.com/sirupsen/logrus"
)
// ClientController resolves LookupRpcService with an AccessRpcService client.
type ClientController struct {
le *logrus.Entry
info *controller.Info
svc AccessClientFunc
waitAck bool
backoff backoff.BackOff
clientRc *refcount.RefCount[SRPCAccessRpcServiceClient]
serviceIDRe *regexp.Regexp
serverIDRe *regexp.Regexp
}
// AccessClientFunc is a function to access the AccessRpcServiceClient.
// The client should be released after the function returns.
// Released is a function to call when the value is no longer valid.
// Returns a release function.
// If the client is nil, an err must be returned.
type AccessClientFunc func(
ctx context.Context,
released func(),
) (SRPCAccessRpcServiceClient, func(), error)
// NewAccessClientFunc constructs a AccessClientFunc with a static client.
func NewAccessClientFunc(svc SRPCAccessRpcServiceClient) AccessClientFunc {
return func(
ctx context.Context,
released func(),
) (SRPCAccessRpcServiceClient, func(), error) {
return svc, nil, nil
}
}
// NewClientController constructs the controller.
// The regex fields can both be nil to accept any.
//
// if waitAck is set, waits for ack from the remote before starting the proxied rpc.
// note: usually you do not need waitAck set to true.
//
// if backoff is nil, uses a default backoff for retrying the rpc call.
func NewClientController(
le *logrus.Entry,
info *controller.Info,
svc AccessClientFunc,
serviceIDRe *regexp.Regexp,
serverIDRe *regexp.Regexp,
waitAck bool,
bo backoff.BackOff,
) *ClientController {
if bo == nil {
exb := backoff.NewExponentialBackOff()
exb.MaxElapsedTime = 0
exb.MaxInterval = time.Second * 5
bo = exb
}
c := &ClientController{
le: le,
info: info,
svc: svc,
serviceIDRe: serviceIDRe,
serverIDRe: serverIDRe,
waitAck: waitAck,
backoff: bo,
}
c.clientRc = refcount.NewRefCount(
nil, false, nil, nil,
func(ctx context.Context, released func()) (SRPCAccessRpcServiceClient, func(), error) {
val, rel, err := svc(ctx, released)
if err != nil || val == nil {
return nil, nil, err
}
return val, rel, nil
},
)
return c
}
// GetControllerInfo returns the controller info.
func (c *ClientController) GetControllerInfo() *controller.Info {
return c.info.Clone()
}
// Execute executes the given controller.
func (c *ClientController) Execute(ctx context.Context) error {
c.clientRc.SetContext(ctx)
return nil
}
// HandleDirective asks if the handler can resolve the directive.
func (c *ClientController) HandleDirective(ctx context.Context, di directive.Instance) ([]directive.Resolver, error) {
switch dir := di.GetDirective().(type) {
case bifrost_rpc.LookupRpcService:
// filter by regex
if c.serviceIDRe != nil {
serviceID := dir.LookupRpcServiceID()
if serviceID != "" && !c.serviceIDRe.MatchString(serviceID) {
return nil, nil
}
}
if c.serverIDRe != nil {
serverID := dir.LookupRpcServerID()
if serverID != "" && !c.serverIDRe.MatchString(serverID) {
return nil, nil
}
}
// only returns an error if the RPC call failed.
// if the service doesn't exist on the remote, it does not return an error.
res := directive.NewRetryResolver(c.le, NewLookupRpcServiceResolver(
dir,
c.AccessClient,
c.waitAck,
), c.backoff)
return directive.R(res, nil)
}
return nil, nil
}
// AccessClient adds a reference to the client and waits for it to be built.
// The released function will be called if the value was released.
func (c *ClientController) AccessClient(
ctx context.Context,
released func(),
) (SRPCAccessRpcServiceClient, func(), error) {
return c.clientRc.ResolveWithReleased(ctx, released)
}
// Close releases any resources used by the controller.
func (c *ClientController) Close() error {
return nil
}
// _ is a type assertion
var (
_ controller.Controller = ((*ClientController)(nil))
_ AccessClientFunc = ((*ClientController)(nil)).AccessClient
)