-
Notifications
You must be signed in to change notification settings - Fork 3
/
servicesapi_router.pb.go
328 lines (278 loc) · 8.16 KB
/
servicesapi_router.pb.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Code generated by protoc-gen-router. DO NOT EDIT.
package gen
import (
context "context"
fmt "fmt"
router "github.com/smart-core-os/sc-golang/pkg/router"
grpc "google.golang.org/grpc"
io "io"
)
// ServicesApiRouter is a ServicesApiServer that allows routing named requests to specific ServicesApiClient
type ServicesApiRouter struct {
UnimplementedServicesApiServer
router.Router
}
// compile time check that we implement the interface we need
var _ ServicesApiServer = (*ServicesApiRouter)(nil)
func NewServicesApiRouter(opts ...router.Option) *ServicesApiRouter {
return &ServicesApiRouter{
Router: router.NewRouter(opts...),
}
}
// WithServicesApiClientFactory instructs the router to create a new
// client the first time Get is called for that name.
func WithServicesApiClientFactory(f func(name string) (ServicesApiClient, error)) router.Option {
return router.WithFactory(func(name string) (any, error) {
return f(name)
})
}
func (r *ServicesApiRouter) Register(server *grpc.Server) {
RegisterServicesApiServer(server, r)
}
// Add extends Router.Add to panic if client is not of type ServicesApiClient.
func (r *ServicesApiRouter) Add(name string, client any) any {
if !r.HoldsType(client) {
panic(fmt.Sprintf("not correct type: client of type %T is not a ServicesApiClient", client))
}
return r.Router.Add(name, client)
}
func (r *ServicesApiRouter) HoldsType(client any) bool {
_, ok := client.(ServicesApiClient)
return ok
}
func (r *ServicesApiRouter) AddServicesApiClient(name string, client ServicesApiClient) ServicesApiClient {
res := r.Add(name, client)
if res == nil {
return nil
}
return res.(ServicesApiClient)
}
func (r *ServicesApiRouter) RemoveServicesApiClient(name string) ServicesApiClient {
res := r.Remove(name)
if res == nil {
return nil
}
return res.(ServicesApiClient)
}
func (r *ServicesApiRouter) GetServicesApiClient(name string) (ServicesApiClient, error) {
res, err := r.Get(name)
if err != nil {
return nil, err
}
if res == nil {
return nil, nil
}
return res.(ServicesApiClient), nil
}
func (r *ServicesApiRouter) GetService(ctx context.Context, request *GetServiceRequest) (*Service, error) {
child, err := r.GetServicesApiClient(request.Name)
if err != nil {
return nil, err
}
return child.GetService(ctx, request)
}
func (r *ServicesApiRouter) PullService(request *PullServiceRequest, server ServicesApi_PullServiceServer) error {
child, err := r.GetServicesApiClient(request.Name)
if err != nil {
return err
}
// so we can cancel our forwarding request if we can't send responses to our caller
reqCtx, reqDone := context.WithCancel(server.Context())
// issue the request
stream, err := child.PullService(reqCtx, request)
if err != nil {
return err
}
// send the stream header
header, err := stream.Header()
if err != nil {
return err
}
if err = server.SendHeader(header); err != nil {
return err
}
// send all the messages
// false means the error is from the child, true means the error is from the caller
var callerError bool
for {
// Impl note: we could improve throughput here by issuing the Recv and Send in different goroutines, but we're doing
// it synchronously until we have a need to change the behaviour
var msg *PullServiceResponse
msg, err = stream.Recv()
if err != nil {
break
}
err = server.Send(msg)
if err != nil {
callerError = true
break
}
}
// err is guaranteed to be non-nil as it's the only way to exit the loop
if callerError {
// cancel the request
reqDone()
return err
} else {
if trailer := stream.Trailer(); trailer != nil {
server.SetTrailer(trailer)
}
if err == io.EOF {
return nil
}
return err
}
}
func (r *ServicesApiRouter) CreateService(ctx context.Context, request *CreateServiceRequest) (*Service, error) {
child, err := r.GetServicesApiClient(request.Name)
if err != nil {
return nil, err
}
return child.CreateService(ctx, request)
}
func (r *ServicesApiRouter) DeleteService(ctx context.Context, request *DeleteServiceRequest) (*Service, error) {
child, err := r.GetServicesApiClient(request.Name)
if err != nil {
return nil, err
}
return child.DeleteService(ctx, request)
}
func (r *ServicesApiRouter) ListServices(ctx context.Context, request *ListServicesRequest) (*ListServicesResponse, error) {
child, err := r.GetServicesApiClient(request.Name)
if err != nil {
return nil, err
}
return child.ListServices(ctx, request)
}
func (r *ServicesApiRouter) PullServices(request *PullServicesRequest, server ServicesApi_PullServicesServer) error {
child, err := r.GetServicesApiClient(request.Name)
if err != nil {
return err
}
// so we can cancel our forwarding request if we can't send responses to our caller
reqCtx, reqDone := context.WithCancel(server.Context())
// issue the request
stream, err := child.PullServices(reqCtx, request)
if err != nil {
return err
}
// send the stream header
header, err := stream.Header()
if err != nil {
return err
}
if err = server.SendHeader(header); err != nil {
return err
}
// send all the messages
// false means the error is from the child, true means the error is from the caller
var callerError bool
for {
// Impl note: we could improve throughput here by issuing the Recv and Send in different goroutines, but we're doing
// it synchronously until we have a need to change the behaviour
var msg *PullServicesResponse
msg, err = stream.Recv()
if err != nil {
break
}
err = server.Send(msg)
if err != nil {
callerError = true
break
}
}
// err is guaranteed to be non-nil as it's the only way to exit the loop
if callerError {
// cancel the request
reqDone()
return err
} else {
if trailer := stream.Trailer(); trailer != nil {
server.SetTrailer(trailer)
}
if err == io.EOF {
return nil
}
return err
}
}
func (r *ServicesApiRouter) StartService(ctx context.Context, request *StartServiceRequest) (*Service, error) {
child, err := r.GetServicesApiClient(request.Name)
if err != nil {
return nil, err
}
return child.StartService(ctx, request)
}
func (r *ServicesApiRouter) ConfigureService(ctx context.Context, request *ConfigureServiceRequest) (*Service, error) {
child, err := r.GetServicesApiClient(request.Name)
if err != nil {
return nil, err
}
return child.ConfigureService(ctx, request)
}
func (r *ServicesApiRouter) StopService(ctx context.Context, request *StopServiceRequest) (*Service, error) {
child, err := r.GetServicesApiClient(request.Name)
if err != nil {
return nil, err
}
return child.StopService(ctx, request)
}
func (r *ServicesApiRouter) GetServiceMetadata(ctx context.Context, request *GetServiceMetadataRequest) (*ServiceMetadata, error) {
child, err := r.GetServicesApiClient(request.Name)
if err != nil {
return nil, err
}
return child.GetServiceMetadata(ctx, request)
}
func (r *ServicesApiRouter) PullServiceMetadata(request *PullServiceMetadataRequest, server ServicesApi_PullServiceMetadataServer) error {
child, err := r.GetServicesApiClient(request.Name)
if err != nil {
return err
}
// so we can cancel our forwarding request if we can't send responses to our caller
reqCtx, reqDone := context.WithCancel(server.Context())
// issue the request
stream, err := child.PullServiceMetadata(reqCtx, request)
if err != nil {
return err
}
// send the stream header
header, err := stream.Header()
if err != nil {
return err
}
if err = server.SendHeader(header); err != nil {
return err
}
// send all the messages
// false means the error is from the child, true means the error is from the caller
var callerError bool
for {
// Impl note: we could improve throughput here by issuing the Recv and Send in different goroutines, but we're doing
// it synchronously until we have a need to change the behaviour
var msg *PullServiceMetadataResponse
msg, err = stream.Recv()
if err != nil {
break
}
err = server.Send(msg)
if err != nil {
callerError = true
break
}
}
// err is guaranteed to be non-nil as it's the only way to exit the loop
if callerError {
// cancel the request
reqDone()
return err
} else {
if trailer := stream.Trailer(); trailer != nil {
server.SetTrailer(trailer)
}
if err == io.EOF {
return nil
}
return err
}
}