Repository navigation
Expand file tree
/
Copy pathclient.go
More file actions
377 lines (319 loc) · 11.6 KB
/
Copy pathclient.go
File metadata and controls
377 lines (319 loc) · 11.6 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package querator
import (
"bytes"
"context"
"crypto/tls"
"errors"
"fmt"
"github.com/duh-rpc/duh-go"
v1 "github.com/duh-rpc/duh-go/proto/v1"
"github.com/kapetan-io/querator/internal"
pb "github.com/kapetan-io/querator/proto"
"github.com/kapetan-io/querator/transport"
"github.com/kapetan-io/tackle/clock"
"github.com/kapetan-io/tackle/set"
"google.golang.org/protobuf/proto"
"net"
"net/http"
)
var Version = "dev-build"
const (
MsgRequestTimeout = internal.MsgRequestTimeout
MsgDuplicateClientID = internal.MsgDuplicateClientID
MsgServiceInShutdown = internal.MsgServiceInShutdown
MsgQueueInShutdown = internal.MsgQueueInShutdown
MsgQueueOverLoaded = internal.MsgQueueOverLoaded
)
type ListOptions struct {
Pivot string
Limit int
Namespace string
}
type ClientConfig struct {
Client *http.Client
Endpoint string
APIKey string
}
type Client struct {
client *duh.Client
conf ClientConfig
}
func NewClient(conf ClientConfig) (*Client, error) {
set.Default(&conf.Client, &http.Client{
Transport: &http.Transport{
MaxConnsPerHost: 5_000,
MaxIdleConns: 5_000,
MaxIdleConnsPerHost: 5_000,
IdleConnTimeout: 60 * clock.Second,
},
})
if len(conf.Endpoint) == 0 {
return nil, errors.New("conf.Endpoint is empty; must provide an http endpoint")
}
return &Client{
client: &duh.Client{
Client: conf.Client,
},
conf: conf,
}, nil
}
type doArgs struct {
Request proto.Message
Reply proto.Message
Path string
}
func (c *Client) do(ctx context.Context, call doArgs) error {
payload, err := proto.Marshal(call.Request)
if err != nil {
return duh.NewClientError("while marshaling request payload: %w", err, nil)
}
r, err := http.NewRequestWithContext(ctx, http.MethodPost,
fmt.Sprintf("%s%s", c.conf.Endpoint, call.Path), bytes.NewReader(payload))
if err != nil {
return duh.NewClientError("", err, nil)
}
r.Header.Set("Content-Type", duh.ContentTypeProtoBuf)
if c.conf.APIKey != "" {
r.Header.Set("Authorization", "Bearer "+c.conf.APIKey)
}
return c.client.Do(r, call.Reply)
}
func (c *Client) QueueProduce(ctx context.Context, req *pb.QueueProduceRequest) error {
return c.do(ctx, doArgs{Path: transport.RPCQueueProduce, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) QueueLease(ctx context.Context, req *pb.QueueLeaseRequest, res *pb.QueueLeaseResponse) error {
return c.do(ctx, doArgs{Path: transport.RPCQueueLease, Request: req, Reply: res})
}
func (c *Client) QueueComplete(ctx context.Context, req *pb.QueueCompleteRequest) error {
return c.do(ctx, doArgs{Path: transport.RPCQueueComplete, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) QueueRetry(ctx context.Context, req *pb.QueueRetryRequest) error {
return c.do(ctx, doArgs{Path: transport.RPCQueueRetry, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) QueueClear(ctx context.Context, req *pb.QueueClearRequest) error {
return c.do(ctx, doArgs{Path: transport.RPCQueueClear, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) QueueReload(ctx context.Context, req *pb.QueueReloadRequest) error {
return c.do(ctx, doArgs{Path: transport.RPCQueueReload, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) QueuesCreate(ctx context.Context, req *pb.QueueInfo) error {
return c.do(ctx, doArgs{Path: transport.RPCQueuesCreate, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) QueuesList(ctx context.Context, res *pb.QueuesListResponse, opts *ListOptions) error {
var req pb.QueuesListRequest
if opts != nil {
req.Limit = int32(opts.Limit)
req.Pivot = opts.Pivot
req.Namespace = opts.Namespace
}
return c.do(ctx, doArgs{Path: transport.RPCQueuesList, Request: &req, Reply: res})
}
func (c *Client) QueuesUpdate(ctx context.Context, req *pb.QueuesUpdateRequest) error {
return c.do(ctx, doArgs{Path: transport.RPCQueuesUpdate, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) QueuesDelete(ctx context.Context, req *pb.QueuesDeleteRequest) error {
return c.do(ctx, doArgs{Path: transport.RPCQueuesDelete, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) QueuesInfo(ctx context.Context, req *pb.QueuesInfoRequest, res *pb.QueueInfo) error {
return c.do(ctx, doArgs{Path: transport.RPCQueuesInfo, Request: req, Reply: res})
}
func (c *Client) StorageItemsList(ctx context.Context, name string, partition int, res *pb.StorageItemsListResponse,
opts *ListOptions) error {
if opts == nil {
opts = &ListOptions{}
}
return c.do(ctx, doArgs{
Path: transport.RPCStorageItemsList,
Request: &pb.StorageItemsListRequest{
Limit: int32(opts.Limit),
Partition: int32(partition),
Pivot: opts.Pivot,
QueueName: name,
},
Reply: res,
})
}
func (c *Client) StorageScheduledList(ctx context.Context, name string, partition int, res *pb.StorageItemsListResponse,
opts *ListOptions) error {
if opts == nil {
opts = &ListOptions{}
}
return c.do(ctx, doArgs{
Path: transport.RPCStorageScheduledList,
Request: &pb.StorageItemsListRequest{
Limit: int32(opts.Limit),
Partition: int32(partition),
Pivot: opts.Pivot,
QueueName: name,
},
Reply: res,
})
}
func (c *Client) StorageItemsImport(ctx context.Context, req *pb.StorageItemsImportRequest,
res *pb.StorageItemsImportResponse) error {
return c.do(ctx, doArgs{Path: transport.RPCStorageItemsImport, Request: req, Reply: res})
}
func (c *Client) StorageItemsDelete(ctx context.Context, req *pb.StorageItemsDeleteRequest) error {
return c.do(ctx, doArgs{Path: transport.RPCStorageItemsDelete, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) StorageBackendsUpdate(ctx context.Context, req *pb.StorageBackendsUpdateRequest) error {
return c.do(ctx, doArgs{Path: transport.RPCStorageBackendsUpdate, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) StorageBackendsList(ctx context.Context, res *pb.StorageBackendsListResponse) error {
return c.do(ctx, doArgs{Path: transport.RPCStorageBackendsList, Request: &pb.StorageBackendsListRequest{},
Reply: res})
}
func (c *Client) QueueStats(ctx context.Context, req *pb.QueueStatsRequest,
res *pb.QueueStatsResponse) error {
return c.do(ctx, doArgs{Path: transport.RPCQueueStats, Request: req, Reply: res})
}
func WithNoTLS(address string) ClientConfig {
return ClientConfig{
Endpoint: fmt.Sprintf("http://%s", address),
Client: &http.Client{
Transport: &http.Transport{
MaxConnsPerHost: 2_000,
MaxIdleConns: 2_000,
MaxIdleConnsPerHost: 2_000,
IdleConnTimeout: 60 * clock.Second,
},
},
}
}
func WithTLS(tls *tls.Config, address string) ClientConfig {
return ClientConfig{
Endpoint: fmt.Sprintf("https://%s", address),
Client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: tls,
MaxConnsPerHost: 2_000,
MaxIdleConns: 2_000,
MaxIdleConnsPerHost: 2_000,
IdleConnTimeout: 60 * clock.Second,
},
},
}
}
func WithConn(conn net.Conn) ClientConfig {
return ClientConfig{
Endpoint: "http://memory",
Client: &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, network, addr string) (net.Conn, error) {
return conn, nil
},
MaxConnsPerHost: 1,
MaxIdleConns: 1,
MaxIdleConnsPerHost: 1,
IdleConnTimeout: 60 * clock.Second,
},
},
}
}
type ItemsWithIDs interface {
GetId() string
}
func CollectIDs[S ~[]E, E ItemsWithIDs](items S) []string {
var result []string
for _, v := range items {
result = append(result, v.GetId())
}
return result
}
func (c *Client) NamespacesCreate(ctx context.Context, req *pb.NamespaceInfo) error {
return c.do(ctx, doArgs{Path: transport.RPCNamespacesCreate, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) NamespacesUpdate(ctx context.Context, req *pb.NamespaceInfo) error {
return c.do(ctx, doArgs{Path: transport.RPCNamespacesUpdate, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) NamespacesList(ctx context.Context, res *pb.NamespacesListResponse, opts *ListOptions) error {
var req pb.NamespacesListRequest
if opts != nil {
req.Limit = int32(opts.Limit)
req.Pivot = opts.Pivot
}
return c.do(ctx, doArgs{Path: transport.RPCNamespacesList, Request: &req, Reply: res})
}
func (c *Client) NamespacesDelete(ctx context.Context, req *pb.NamespacesDeleteRequest) error {
return c.do(ctx, doArgs{Path: transport.RPCNamespacesDelete, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) UsersCreate(ctx context.Context, req *pb.UserCreateRequest,
res *pb.UserCreateResponse) error {
return c.do(ctx, doArgs{Path: transport.RPCUsersCreate, Request: req, Reply: res})
}
func (c *Client) UsersList(ctx context.Context, res *pb.UsersListResponse, opts *ListOptions) error {
var req pb.UsersListRequest
if opts != nil {
req.Limit = int32(opts.Limit)
req.Pivot = opts.Pivot
}
return c.do(ctx, doArgs{Path: transport.RPCUsersList, Request: &req, Reply: res})
}
func (c *Client) UsersDelete(ctx context.Context, req *pb.UsersDeleteRequest) error {
return c.do(ctx, doArgs{Path: transport.RPCUsersDelete, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) APIKeysCreate(ctx context.Context, req *pb.APIKeyCreateRequest,
res *pb.APIKeyCreateResponse) error {
return c.do(ctx, doArgs{Path: transport.RPCAPIKeysCreate, Request: req, Reply: res})
}
func (c *Client) APIKeysList(ctx context.Context, res *pb.APIKeysListResponse, opts *ListOptions) error {
var req pb.APIKeysListRequest
if opts != nil {
req.Limit = int32(opts.Limit)
req.Pivot = opts.Pivot
}
return c.do(ctx, doArgs{Path: transport.RPCAPIKeysList, Request: &req, Reply: res})
}
func (c *Client) APIKeysListByUser(ctx context.Context, userID string, res *pb.APIKeysListResponse,
opts *ListOptions) error {
req := pb.APIKeysListRequest{
UserId: userID,
}
if opts != nil {
req.Limit = int32(opts.Limit)
req.Pivot = opts.Pivot
}
return c.do(ctx, doArgs{Path: transport.RPCAPIKeysList, Request: &req, Reply: res})
}
func (c *Client) APIKeysDelete(ctx context.Context, req *pb.APIKeysDeleteRequest) error {
return c.do(ctx, doArgs{Path: transport.RPCAPIKeysDelete, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) RolesCreate(ctx context.Context, req *pb.RoleCreateRequest,
res *pb.RoleCreateResponse) error {
return c.do(ctx, doArgs{Path: transport.RPCRolesCreate, Request: req, Reply: res})
}
func (c *Client) RolesList(ctx context.Context, namespace string, res *pb.RolesListResponse,
opts *ListOptions) error {
req := pb.RolesListRequest{
Namespace: namespace,
}
if opts != nil {
req.Limit = int32(opts.Limit)
req.Pivot = opts.Pivot
}
return c.do(ctx, doArgs{Path: transport.RPCRolesList, Request: &req, Reply: res})
}
func (c *Client) RolesUpdate(ctx context.Context, req *pb.RoleUpdateRequest) error {
return c.do(ctx, doArgs{Path: transport.RPCRolesUpdate, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) RolesDelete(ctx context.Context, req *pb.RolesDeleteRequest) error {
return c.do(ctx, doArgs{Path: transport.RPCRolesDelete, Request: req, Reply: &v1.Reply{}})
}
func (c *Client) RoleBindingsCreate(ctx context.Context, req *pb.RoleBindingCreateRequest,
res *pb.RoleBindingCreateResponse) error {
return c.do(ctx, doArgs{Path: transport.RPCRoleBindingsCreate, Request: req, Reply: res})
}
func (c *Client) RoleBindingsList(ctx context.Context, namespace string, res *pb.RoleBindingsListResponse,
opts *ListOptions) error {
req := pb.RoleBindingsListRequest{
Namespace: namespace,
}
if opts != nil {
req.Limit = int32(opts.Limit)
req.Pivot = opts.Pivot
}
return c.do(ctx, doArgs{Path: transport.RPCRoleBindingsList, Request: &req, Reply: res})
}
func (c *Client) RoleBindingsDelete(ctx context.Context, req *pb.RoleBindingDeleteRequest) error {
return c.do(ctx, doArgs{Path: transport.RPCRoleBindingsDelete, Request: req, Reply: &v1.Reply{}})
}