/
options.go
364 lines (309 loc) · 9.53 KB
/
options.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
package kafka
import (
"github.com/devexps/go-micro/v2/broker"
kafkaGo "github.com/segmentio/kafka-go"
"github.com/segmentio/kafka-go/sasl/plain"
"github.com/segmentio/kafka-go/sasl/scram"
"hash"
"time"
)
type BalancerName string
const (
LeastBytesBalancer BalancerName = "LeastBytes"
RoundRobinBalancer BalancerName = "RoundRobin"
HashBalancer BalancerName = "Hash"
ReferenceHashBalancer BalancerName = "ReferenceHash"
Crc32Balancer BalancerName = "CRC32Balancer"
Murmur2Balancer BalancerName = "Murmur2Balancer"
)
type ScramAlgorithm string
const (
ScramAlgorithmSHA256 ScramAlgorithm = "SHA256"
ScramAlgorithmSHA512 ScramAlgorithm = "SHA512"
)
///
/// Option
///
type retriesCountKey struct{}
type queueCapacityKey struct{}
type minBytesKey struct{}
type maxBytesKey struct{}
type maxWaitKey struct{}
type readLagIntervalKey struct{}
type heartbeatIntervalKey struct{}
type commitIntervalKey struct{}
type partitionWatchIntervalKey struct{}
type watchPartitionChangesKey struct{}
type sessionTimeoutKey struct{}
type rebalanceTimeoutKey struct{}
type retentionTimeKey struct{}
type startOffsetKey struct{}
type mechanismKey struct{}
type readerConfigKey struct{}
type writerConfigKey struct{}
type dialerConfigKey struct{}
type dialerTimeoutKey struct{}
type loggerKey struct{}
type errorLoggerKey struct{}
type enableLoggerKey struct{}
type enableErrorLoggerKey struct{}
type enableOneTopicOneWriterKey struct{}
type batchSizeKey struct{}
type batchTimeoutKey struct{}
type batchBytesKey struct{}
type asyncKey struct{}
type maxAttemptsKey struct{}
type readTimeoutKey struct{}
type writeTimeoutKey struct{}
type allowPublishAutoTopicCreationKey struct{}
// WithReaderConfig .
func WithReaderConfig(cfg kafkaGo.ReaderConfig) broker.Option {
return broker.WithContextAndValue(readerConfigKey{}, cfg)
}
// WithWriterConfig .
func WithWriterConfig(cfg WriterConfig) broker.Option {
return broker.WithContextAndValue(writerConfigKey{}, cfg)
}
// WithEnableOneTopicOneWriter .
func WithEnableOneTopicOneWriter(enable bool) broker.Option {
return broker.WithContextAndValue(enableOneTopicOneWriterKey{}, enable)
}
// WithPlainMechanism .
func WithPlainMechanism(username, password string) broker.Option {
mechanism := plain.Mechanism{
Username: username,
Password: password,
}
return broker.WithContextAndValue(mechanismKey{}, mechanism)
}
// WithScramMechanism .
func WithScramMechanism(algoName ScramAlgorithm, username, password string) broker.Option {
var algo scram.Algorithm
switch algoName {
case ScramAlgorithmSHA256:
algo = scram.SHA256
case ScramAlgorithmSHA512:
algo = scram.SHA512
}
mechanism, err := scram.Mechanism(algo, username, password)
if err != nil {
panic(err)
}
return broker.WithContextAndValue(mechanismKey{}, mechanism)
}
// WithDialer .
func WithDialer(cfg *kafkaGo.Dialer) broker.Option {
return broker.WithContextAndValue(dialerConfigKey{}, cfg)
}
// WithDialerTimeout .
func WithDialerTimeout(tm time.Duration) broker.Option {
return broker.WithContextAndValue(dialerTimeoutKey{}, tm)
}
// WithRetries .
func WithRetries(cnt int) broker.Option {
return broker.WithContextAndValue(retriesCountKey{}, cnt)
}
// WithQueueCapacity .
func WithQueueCapacity(cap int) broker.Option {
return broker.WithContextAndValue(queueCapacityKey{}, cap)
}
// WithMinBytes fetch.min.bytes
func WithMinBytes(bytes int) broker.Option {
return broker.WithContextAndValue(minBytesKey{}, bytes)
}
// WithMaxBytes .
func WithMaxBytes(bytes int) broker.Option {
return broker.WithContextAndValue(maxBytesKey{}, bytes)
}
// WithMaxWait fetch.max.wait.ms
func WithMaxWait(time time.Duration) broker.Option {
return broker.WithContextAndValue(maxWaitKey{}, time)
}
// WithReadLagInterval .
func WithReadLagInterval(interval time.Duration) broker.Option {
return broker.WithContextAndValue(readLagIntervalKey{}, interval)
}
// WithHeartbeatInterval .
func WithHeartbeatInterval(interval time.Duration) broker.Option {
return broker.WithContextAndValue(heartbeatIntervalKey{}, interval)
}
// WithCommitInterval .
func WithCommitInterval(interval time.Duration) broker.Option {
return broker.WithContextAndValue(commitIntervalKey{}, interval)
}
// WithPartitionWatchInterval .
func WithPartitionWatchInterval(interval time.Duration) broker.Option {
return broker.WithContextAndValue(partitionWatchIntervalKey{}, interval)
}
// WithWatchPartitionChanges .
func WithWatchPartitionChanges(enable bool) broker.Option {
return broker.WithContextAndValue(watchPartitionChangesKey{}, enable)
}
// WithSessionTimeout .
func WithSessionTimeout(timeout time.Duration) broker.Option {
return broker.WithContextAndValue(sessionTimeoutKey{}, timeout)
}
// WithRebalanceTimeout .
func WithRebalanceTimeout(timeout time.Duration) broker.Option {
return broker.WithContextAndValue(rebalanceTimeoutKey{}, timeout)
}
// WithRetentionTime .
func WithRetentionTime(time time.Duration) broker.Option {
return broker.WithContextAndValue(retentionTimeKey{}, time)
}
// WithStartOffset .
func WithStartOffset(offset int64) broker.Option {
return broker.WithContextAndValue(startOffsetKey{}, offset)
}
// WithMaxAttempts .
func WithMaxAttempts(cnt int) broker.Option {
return broker.WithContextAndValue(maxAttemptsKey{}, cnt)
}
// WithLogger inject info logger
func WithLogger(l kafkaGo.Logger) broker.Option {
return broker.WithContextAndValue(loggerKey{}, l)
}
// WithErrorLogger inject error logger
func WithErrorLogger(l kafkaGo.Logger) broker.Option {
return broker.WithContextAndValue(errorLoggerKey{}, l)
}
// WithEnableLogger enable go-micro info logger
func WithEnableLogger(enable bool) broker.Option {
return broker.WithContextAndValue(enableLoggerKey{}, enable)
}
// WithEnableErrorLogger enable go-micro error logger
func WithEnableErrorLogger(enable bool) broker.Option {
return broker.WithContextAndValue(enableErrorLoggerKey{}, enable)
}
// WithBatchSize batch.size
// default:100
func WithBatchSize(size int) broker.Option {
return broker.WithContextAndValue(batchSizeKey{}, size)
}
// WithBatchTimeout linger.ms
// default:10ms
func WithBatchTimeout(timeout time.Duration) broker.Option {
return broker.WithContextAndValue(batchTimeoutKey{}, timeout)
}
// WithBatchBytes
// default:1048576 bytes
func WithBatchBytes(by int64) broker.Option {
return broker.WithContextAndValue(batchBytesKey{}, by)
}
// WithAsync
// default:true
func WithAsync(enable bool) broker.Option {
return broker.WithContextAndValue(asyncKey{}, enable)
}
// WithPublishMaxAttempts .
func WithPublishMaxAttempts(cnt int) broker.Option {
return broker.WithContextAndValue(maxAttemptsKey{}, cnt)
}
// WithReadTimeout
// default:10s
func WithReadTimeout(timeout time.Duration) broker.Option {
return broker.WithContextAndValue(readTimeoutKey{}, timeout)
}
// WithWriteTimeout
// default:10s
func WithWriteTimeout(timeout time.Duration) broker.Option {
return broker.WithContextAndValue(writeTimeoutKey{}, timeout)
}
// WithAllowPublishAutoTopicCreation .
func WithAllowPublishAutoTopicCreation(enable bool) broker.Option {
return broker.WithContextAndValue(allowPublishAutoTopicCreationKey{}, enable)
}
///
/// PublishOption
///
type messageHeadersKey struct{}
type messageKeyKey struct{}
type messageOffsetKey struct{}
type balancerKey struct{}
type balancerValue struct {
Name BalancerName
Consistent bool
Hasher hash.Hash32
}
// WithHeaders .
func WithHeaders(headers map[string]interface{}) broker.PublishOption {
return broker.WithPublishContextAndValue(messageHeadersKey{}, headers)
}
// WithMessageKey .
func WithMessageKey(key []byte) broker.PublishOption {
return broker.WithPublishContextAndValue(messageKeyKey{}, key)
}
// WithMessageOffset .
func WithMessageOffset(offset int64) broker.PublishOption {
return broker.WithPublishContextAndValue(messageOffsetKey{}, offset)
}
// WithLeastBytesBalancer .
func WithLeastBytesBalancer() broker.PublishOption {
return broker.WithPublishContextAndValue(balancerKey{},
&balancerValue{
Name: LeastBytesBalancer,
},
)
}
// WithRoundRobinBalancer .
func WithRoundRobinBalancer() broker.PublishOption {
return broker.WithPublishContextAndValue(balancerKey{},
&balancerValue{
Name: RoundRobinBalancer,
},
)
}
// WithHashBalancer .
func WithHashBalancer(hasher hash.Hash32) broker.PublishOption {
return broker.WithPublishContextAndValue(balancerKey{},
&balancerValue{
Name: HashBalancer,
Hasher: hasher,
},
)
}
// WithReferenceHashBalancer .
func WithReferenceHashBalancer(hasher hash.Hash32) broker.PublishOption {
return broker.WithPublishContextAndValue(balancerKey{},
&balancerValue{
Name: ReferenceHashBalancer,
Hasher: hasher,
},
)
}
// WithCrc32Balancer .
func WithCrc32Balancer(consistent bool) broker.PublishOption {
return broker.WithPublishContextAndValue(balancerKey{},
&balancerValue{
Name: Crc32Balancer,
Consistent: consistent,
},
)
}
// WithMurmur2Balancer .
func WithMurmur2Balancer(consistent bool) broker.PublishOption {
return broker.WithPublishContextAndValue(balancerKey{},
&balancerValue{
Name: Murmur2Balancer,
Consistent: consistent,
},
)
}
///
/// SubscribeOption
///
type autoSubscribeCreateTopicKey struct{}
type autoSubscribeCreateTopicValue struct {
Topic string
NumPartitions int
ReplicationFactor int
}
func WithSubscribeAutoCreateTopic(topic string, numPartitions, replicationFactor int) broker.SubscribeOption {
return broker.WithSubscribeContextAndValue(autoSubscribeCreateTopicKey{},
&autoSubscribeCreateTopicValue{
Topic: topic,
NumPartitions: numPartitions,
ReplicationFactor: replicationFactor,
},
)
}