/
config.go
222 lines (190 loc) · 5.23 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
package config
import (
"encoding/json"
"errors"
"fmt"
"os"
"time"
"github.com/BurntSushi/toml"
"github.com/Shopify/sarama"
)
// Config for the proxy
type Config struct {
Server ServerConfig
Kafka KafkaConfig
Consumer ConsumerConfig
Producer ProducerConfig
Replication ReplicationConfig
}
func newConfig() *Config {
conf := new(Config)
conf.Server.AccessLogEnable = true
conf.Server.Host = "0.0.0.0"
conf.Server.Port = 8080
conf.Server.AdminHost = "0.0.0.0"
conf.Server.AdminPort = 8081
conf.Server.LogDir = "."
conf.Kafka.Brokers = []string{"127.0.0.1:9092"}
conf.Kafka.Version.Version = sarama.MinVersion
conf.Kafka.Zookeepers = []string{"127.0.0.1:2181"}
conf.Kafka.ZKSessionTimeout.Duration = 6 * time.Second
conf.Consumer.OffsetAutoCommitInterval.Duration = 10 * time.Second
conf.Consumer.OffsetAutoReset.Reset = sarama.OffsetOldest
conf.Consumer.ClaimPartitionRetryInterval.Duration = 2 * time.Second
conf.Consumer.ChannelBufferSize = 4096
conf.Consumer.UnackManagerSize = 32
conf.Consumer.FetchDefault = 1048576
conf.Consumer.MaxWaitTime.Duration = 250 * time.Millisecond
conf.Consumer.MaxProcessingTime.Duration = 100 * time.Millisecond
conf.Producer.Clients = 5
conf.Producer.MaxMessageBytes = 1024 * 1024
conf.Replication.BatchChecksumSize = 20
conf.Replication.CrossDatacenterReplicationTopic = "__cross_datacenter_replication"
conf.Replication.ReplicationAclUpdateInterval.Duration = 10 * time.Second
return conf
}
func Load(path string) (*Config, error) {
_, err := os.Stat(path)
if err != nil {
return nil, err
}
conf := newConfig()
if _, err := toml.DecodeFile(path, conf); err != nil {
return nil, err
}
conf.Consumer.AutoReconnectInterval.Duration = conf.Kafka.ZKSessionTimeout.Duration / 3
if conf.Replication.MyDatacenterName == "" {
return nil, errors.New("datacenter name must not be empty")
}
return conf, nil
}
type ServerConfig struct {
Host string
Port int
AdminHost string
AdminPort int
LogDir string
LogFormat string
AccessLogEnable bool
}
type KafkaConfig struct {
Brokers []string
Version version
Zookeepers []string
ZKSessionTimeout duration
ZKChroot string
}
type ProducerConfig struct {
Clients int
MaxMessageBytes int
}
type ConsumerConfig struct {
OffsetAutoCommitInterval duration `json:"offset_auto_commit_interval"`
OffsetAutoReset reset `json:"offset_auto_reset"`
ClaimPartitionRetryInterval duration `json:"claim_partition_retry_interval"`
AutoReconnectInterval duration `json:"auto_reconnect_interval"`
UnackManagerSize int `json:"unack_manager_size"`
//sarama config
ChannelBufferSize int `json:"channel_buffer_size"`
FetchDefault int32 `json:"fetch_default"`
MaxWaitTime duration `json:"max_wait_time"`
MaxProcessingTime duration `json:"max_processing_time"`
}
type ReplicationConfig struct {
MyDatacenterName string
CrossDatacenterReplicationTopic string
ReplicationAclUpdateInterval duration
BatchChecksumSize int
UUIDServers []string
}
type duration struct {
time.Duration
}
func (d duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Duration.String())
}
func (d *duration) UnmarshalJSON(b []byte) error {
var err error
var s string
if err = json.Unmarshal(b, &s); err != nil {
return err
}
d.Duration, err = time.ParseDuration(s)
return err
}
func (d *duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
func (d *duration) MarshalText() ([]byte, error) {
return []byte(d.Duration.String()), nil
}
type reset struct {
Reset int64
}
func (r reset) MarshalJSON() ([]byte, error) {
switch r.Reset {
case sarama.OffsetOldest:
return json.Marshal("oldest")
case sarama.OffsetNewest:
return json.Marshal("newest")
default:
return nil, errors.New("invalid offset auto reset configuration")
}
}
func (r *reset) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
switch s {
case "oldest":
r.Reset = sarama.OffsetOldest
case "newest":
r.Reset = sarama.OffsetNewest
default:
return errors.New("offset auto reset must be oldest or newest")
}
return nil
}
func (r *reset) UnmarshalText(text []byte) error {
switch string(text) {
case "oldest":
r.Reset = sarama.OffsetOldest
case "newest":
r.Reset = sarama.OffsetNewest
default:
return errors.New("offset auto reset must be oldest or newest")
}
return nil
}
func (r *reset) MarshalText() ([]byte, error) {
switch r.Reset {
case sarama.OffsetOldest:
return []byte("oldest"), nil
case sarama.OffsetNewest:
return []byte("newest"), nil
default:
return nil, errors.New("invalid offset auto reset configuration")
}
}
type version struct {
Version sarama.KafkaVersion
}
func (v *version) UnmarshalText(text []byte) (err error) {
v.Version, err = sarama.ParseKafkaVersion(string(text))
if err != nil {
return err
}
supported := false
for _, sv := range sarama.SupportedVersions {
if v.Version.String() == sv.String() {
supported = true
}
}
if !supported {
return fmt.Errorf("invalid version %s ", string(text))
}
return nil
}