-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathclient.go
376 lines (305 loc) · 9.29 KB
/
client.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
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
package topicclientinternal
import (
"context"
"errors"
"github.com/ydb-platform/ydb-go-genproto/Ydb_Topic_V1"
"google.golang.org/grpc"
"github.com/ydb-platform/ydb-go-sdk/v3/credentials"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/config"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawtopic"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawydb"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/topic"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/topic/topiclistenerinternal"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/topic/topicreadercommon"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/topic/topicreaderinternal"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/topic/topicwriterinternal"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/tx"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topiclistener"
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicoptions"
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicreader"
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topictypes"
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicwriter"
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
)
var errUnsupportedTransactionType = xerrors.Wrap(errors.New("ydb: unsuppotred transaction type. Use transaction from Driver().Query().DoTx(...)")) //nolint:lll
type Client struct {
cfg topic.Config
cred credentials.Credentials
defaultOperationParams rawydb.OperationParams
rawClient rawtopic.Client
}
func New(
ctx context.Context,
conn grpc.ClientConnInterface,
cred credentials.Credentials,
opts ...topicoptions.TopicOption,
) *Client {
rawClient := rawtopic.NewClient(Ydb_Topic_V1.NewTopicServiceClient(conn))
cfg := newTopicConfig(opts...)
var defaultOperationParams rawydb.OperationParams
topic.OperationParamsFromConfig(&defaultOperationParams, &cfg.Common)
return &Client{
cfg: cfg,
cred: cred,
defaultOperationParams: defaultOperationParams,
rawClient: rawClient,
}
}
func newTopicConfig(opts ...topicoptions.TopicOption) topic.Config {
c := topic.Config{
Trace: &trace.Topic{},
MaxGrpcMessageSize: config.DefaultGRPCMsgSize,
}
for _, opt := range opts {
if opt != nil {
opt(&c)
}
}
return c
}
// Close the client
func (c *Client) Close(_ context.Context) error {
return nil
}
// Alter topic options
func (c *Client) Alter(ctx context.Context, path string, opts ...topicoptions.AlterOption) error {
req := &rawtopic.AlterTopicRequest{}
req.OperationParams = c.defaultOperationParams
req.Path = path
for _, opt := range opts {
if opt != nil {
opt.ApplyAlterOption(req)
}
}
call := func(ctx context.Context) error {
_, alterErr := c.rawClient.AlterTopic(ctx, req)
return alterErr
}
if c.cfg.AutoRetry() {
return retry.Retry(ctx, call,
retry.WithIdempotent(true),
retry.WithTrace(c.cfg.TraceRetry()),
retry.WithBudget(c.cfg.RetryBudget()),
)
}
return call(ctx)
}
// Create new topic
func (c *Client) Create(
ctx context.Context,
path string,
opts ...topicoptions.CreateOption,
) error {
req := &rawtopic.CreateTopicRequest{}
req.OperationParams = c.defaultOperationParams
req.Path = path
for _, opt := range opts {
if opt != nil {
opt.ApplyCreateOption(req)
}
}
call := func(ctx context.Context) error {
_, createErr := c.rawClient.CreateTopic(ctx, req)
return createErr
}
if c.cfg.AutoRetry() {
return retry.Retry(ctx, call,
retry.WithIdempotent(true),
retry.WithTrace(c.cfg.TraceRetry()),
retry.WithBudget(c.cfg.RetryBudget()),
)
}
return call(ctx)
}
// Describe topic
func (c *Client) Describe(
ctx context.Context,
path string,
opts ...topicoptions.DescribeOption,
) (res topictypes.TopicDescription, _ error) {
req := rawtopic.DescribeTopicRequest{
OperationParams: c.defaultOperationParams,
Path: path,
}
for _, opt := range opts {
if opt != nil {
opt(&req)
}
}
var rawRes rawtopic.DescribeTopicResult
call := func(ctx context.Context) (describeErr error) {
rawRes, describeErr = c.rawClient.DescribeTopic(ctx, req)
return describeErr
}
var err error
if c.cfg.AutoRetry() {
err = retry.Retry(ctx, call,
retry.WithIdempotent(true),
retry.WithTrace(c.cfg.TraceRetry()),
retry.WithBudget(c.cfg.RetryBudget()),
)
} else {
err = call(ctx)
}
if err != nil {
return res, err
}
res.FromRaw(&rawRes)
return res, nil
}
// Describe topic consumer
func (c *Client) DescribeTopicConsumer(
ctx context.Context,
path string,
consumer string,
opts ...topicoptions.DescribeConsumerOption,
) (res topictypes.TopicConsumerDescription, _ error) {
req := rawtopic.DescribeConsumerRequest{
OperationParams: c.defaultOperationParams,
Path: path,
Consumer: consumer,
}
for _, opt := range opts {
if opt != nil {
opt(&req)
}
}
var rawRes rawtopic.DescribeConsumerResult
call := func(ctx context.Context) (describeErr error) {
rawRes, describeErr = c.rawClient.DescribeConsumer(ctx, req)
return describeErr
}
var err error
if c.cfg.AutoRetry() {
err = retry.Retry(ctx, call,
retry.WithIdempotent(true),
retry.WithTrace(c.cfg.TraceRetry()),
retry.WithBudget(c.cfg.RetryBudget()),
)
} else {
err = call(ctx)
}
if err != nil {
return res, err
}
res.FromRaw(&rawRes)
return res, nil
}
// Drop topic
func (c *Client) Drop(ctx context.Context, path string, opts ...topicoptions.DropOption) error {
req := rawtopic.DropTopicRequest{}
req.OperationParams = c.defaultOperationParams
req.Path = path
for _, opt := range opts {
if opt != nil {
opt.ApplyDropOption(&req)
}
}
call := func(ctx context.Context) error {
_, removeErr := c.rawClient.DropTopic(ctx, req)
return removeErr
}
if c.cfg.AutoRetry() {
return retry.Retry(ctx, call,
retry.WithIdempotent(true),
retry.WithTrace(c.cfg.TraceRetry()),
retry.WithBudget(c.cfg.RetryBudget()),
)
}
return call(ctx)
}
// StartListener starts read listen topic with the handler
// it is fast non block call, connection starts in background
func (c *Client) StartListener(
consumer string,
handler topiclistener.EventHandler,
readSelectors topicoptions.ReadSelectors,
opts ...topicoptions.ListenerOption,
) (*topiclistener.TopicListener, error) {
cfg := topiclistenerinternal.NewStreamListenerConfig()
cfg.Consumer = consumer
cfg.Selectors = make([]*topicreadercommon.PublicReadSelector, len(readSelectors))
for i := range readSelectors {
cfg.Selectors[i] = readSelectors[i].Clone()
}
for _, opt := range opts {
if opt == nil {
continue
}
opt(&cfg)
}
if err := cfg.Validate(); err != nil {
return nil, err
}
return topiclistener.NewTopicListener(&c.rawClient, &cfg, handler)
}
// StartReader create new topic reader and start pull messages from server
// it is fast non block call, connection will start in background
func (c *Client) StartReader(
consumer string,
readSelectors topicoptions.ReadSelectors,
opts ...topicoptions.ReaderOption,
) (*topicreader.Reader, error) {
var connector topicreaderinternal.TopicSteamReaderConnect = func(ctx context.Context) (
topicreadercommon.RawTopicReaderStream, error,
) {
return c.rawClient.StreamRead(ctx)
}
defaultOpts := []topicoptions.ReaderOption{
topicoptions.WithCommonConfig(c.cfg.Common),
topicreaderinternal.WithCredentials(c.cred),
topicreaderinternal.WithTrace(c.cfg.Trace),
topicoptions.WithReaderStartTimeout(topic.DefaultStartTimeout),
}
opts = append(defaultOpts, opts...)
internalReader, err := topicreaderinternal.NewReader(&c.rawClient, connector, consumer, readSelectors, opts...)
if err != nil {
return nil, err
}
internalReader.TopicOnReaderStart(consumer, err)
return topicreader.NewReader(internalReader), nil
}
// StartWriter create new topic writer wrapper
func (c *Client) StartWriter(topicPath string, opts ...topicoptions.WriterOption) (*topicwriter.Writer, error) {
cfg := c.createWriterConfig(topicPath, append(opts, topicwriterinternal.WithMaxGrpcMessageBytes(
c.cfg.MaxGrpcMessageSize,
)))
writer, err := topicwriterinternal.NewWriterReconnector(cfg)
if err != nil {
return nil, err
}
return topicwriter.NewWriter(writer), nil
}
func (c *Client) StartTransactionalWriter(
transaction tx.Identifier,
topicpath string,
opts ...topicoptions.WriterOption,
) (*topicwriter.TxWriter, error) {
internalTx, ok := transaction.(tx.Transaction)
if !ok {
return nil, xerrors.WithStackTrace(errUnsupportedTransactionType)
}
cfg := c.createWriterConfig(topicpath, opts)
writer, err := topicwriterinternal.NewWriterReconnector(cfg)
if err != nil {
return nil, err
}
txWriter := topicwriterinternal.NewTopicWriterTransaction(writer, internalTx, cfg.Tracer)
return topicwriter.NewTxWriterInternal(txWriter), nil
}
func (c *Client) createWriterConfig(
topicPath string,
opts []topicoptions.WriterOption,
) topicwriterinternal.WriterReconnectorConfig {
options := []topicoptions.WriterOption{
topicwriterinternal.WithRawClient(&c.rawClient),
topicwriterinternal.WithTopic(topicPath),
topicwriterinternal.WithCommonConfig(c.cfg.Common),
topicwriterinternal.WithTrace(c.cfg.Trace),
topicwriterinternal.WithCredentials(c.cred),
}
options = append(options, opts...)
return topicwriterinternal.NewWriterReconnectorConfig(options...)
}