-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
224 lines (194 loc) · 6.6 KB
/
config.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
package kafka
import (
"context"
"math"
"time"
"github.com/segmentio/kafka-go"
)
type configuration struct {
Brokers []string
MessageTypeIdentifiers map[uint32]string
messageTypeIdentifiers map[string]uint32
ContentTypeIdentifiers map[uint8]string
contentTypeIdentifiers map[string]uint8
CompressionMethod compressionMethod
PartitionSelection partitionSelection
RequiredWrites requiredWrites
MaxWriteAttempts uint8
MaxWriteBatchSize uint16
BatchWriteInterval time.Duration
Context context.Context
Monitor Monitor
Logger Logger
DriverLogger Logger
}
var Options singleton
type singleton struct{}
type option func(*configuration)
func (singleton) Brokers(value ...string) option {
return func(this *configuration) { this.Brokers = value }
}
func (singleton) MessageTypeIdentifiers(value map[uint32]string) option {
reverse := make(map[string]uint32, len(value))
for key, item := range value {
reverse[item] = key
}
return func(this *configuration) {
this.MessageTypeIdentifiers = value
this.messageTypeIdentifiers = reverse
}
}
func (singleton) ContentTypeIdentifiers(value map[uint8]string) option {
reverse := make(map[string]uint8, len(value))
for key, item := range value {
reverse[item] = key
}
return func(this *configuration) {
this.ContentTypeIdentifiers = value
this.contentTypeIdentifiers = reverse
}
}
func (singleton) CompressionMethod(value compressionMethod) option {
return func(this *configuration) { this.CompressionMethod = value }
}
func (singleton) PartitionSelection(value partitionSelection) option {
return func(this *configuration) { this.PartitionSelection = value }
}
func (singleton) RequiredWrites(value requiredWrites) option {
return func(this *configuration) { this.RequiredWrites = value }
}
func (singleton) MaxWriteAttempts(value uint8) option {
return func(this *configuration) { this.MaxWriteAttempts = value }
}
func (singleton) MaxWriteBatchSize(value uint16) option {
return func(this *configuration) { this.MaxWriteBatchSize = value }
}
func (singleton) BatchWriteInterval(value time.Duration) option {
return func(this *configuration) { this.BatchWriteInterval = value }
}
func (singleton) Context(value context.Context) option {
return func(this *configuration) { this.Context = value }
}
func (singleton) Logger(value Logger) option {
return func(this *configuration) { this.Logger = value }
}
func (singleton) Monitor(value Monitor) option {
return func(this *configuration) { this.Monitor = value }
}
func (singleton) DriverLogger(value Logger) option {
return func(this *configuration) { this.DriverLogger = value }
}
func (singleton) apply(options ...option) option {
return func(this *configuration) {
for _, option := range Options.defaults(options...) {
option(this)
}
}
}
func (singleton) defaults(options ...option) []option {
return append([]option{
Options.Brokers("127.0.0.1:9092"),
Options.CompressionMethod(CompressionMethodLz4),
Options.PartitionSelection(PartitionSelectionRoundRobin),
Options.RequiredWrites(RequiredWritesOne),
Options.MaxWriteAttempts(1),
Options.MaxWriteBatchSize(math.MaxUint16),
Options.BatchWriteInterval(time.Millisecond),
Options.Context(context.Background()),
Options.Logger(nop{}),
Options.Monitor(nop{}),
}, options...)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type nop struct{}
func (nop) Printf(string, ...interface{}) {}
func (nop) ConnectionOpened(error) {}
func (nop) ConnectionClosed() {}
func (nop) DispatchPublished() {}
func (nop) DeliveryReceived() {}
func (nop) DeliveryAcknowledged(uint64, error) {}
func (nop) TransactionCommitted(error) {}
func (nop) TransactionRolledBack(error) {}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type Logger interface {
Printf(string, ...interface{})
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type Monitor interface {
ConnectionOpened(error)
ConnectionClosed()
DispatchPublished()
DeliveryReceived()
DeliveryAcknowledged(uint64, error)
TransactionCommitted(error)
TransactionRolledBack(error)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type compressionMethod uint8
var (
CompressionMethodNone compressionMethod = 0
CompressionMethodGZip compressionMethod = 1
CompressionMethodLz4 compressionMethod = 2
CompressionMethodSnappy compressionMethod = 3
CompressionMethodZStandard compressionMethod = 4
)
func computeCompressionMethod(method compressionMethod) kafka.Compression {
switch method {
case CompressionMethodGZip:
return kafka.Gzip
case CompressionMethodLz4:
return kafka.Lz4
case CompressionMethodSnappy:
return kafka.Snappy
case CompressionMethodZStandard:
return kafka.Zstd
case CompressionMethodNone:
return 0
default:
panic("unknown compression method value")
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type partitionSelection uint8
var (
PartitionSelectionRoundRobin partitionSelection = 0
PartitionSelectionMurmurHash partitionSelection = 1
PartitionSelectionFNVHash partitionSelection = 2
PartitionSelectionCrc32Hash partitionSelection = 3
PartitionSelectionLeastBytes partitionSelection = 4
)
func computePartitionSelection(method partitionSelection) kafka.Balancer {
switch method {
case PartitionSelectionRoundRobin:
return &kafka.RoundRobin{}
case PartitionSelectionMurmurHash:
return kafka.Murmur2Balancer{}
case PartitionSelectionFNVHash:
return &kafka.Hash{}
case PartitionSelectionCrc32Hash:
return kafka.CRC32Balancer{}
case PartitionSelectionLeastBytes:
return &kafka.LeastBytes{}
default:
panic("unknown partition selection method value")
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type requiredWrites uint8
var (
RequiredWritesNone requiredWrites = 0
RequiredWritesOne requiredWrites = 1
RequiredWritesAll requiredWrites = 2
)
func computeRequiredWrites(required requiredWrites) kafka.RequiredAcks {
switch required {
case RequiredWritesOne:
return kafka.RequireOne
case RequiredWritesAll:
return kafka.RequireAll
case RequiredWritesNone:
return kafka.RequireNone
default:
panic("unknown required acknowledgement value")
}
}