github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/kafka/config.go (about) 1 package kafka 2 3 import ( 4 "fmt" 5 6 "gopkg.in/Shopify/sarama.v1" 7 8 "github.com/artisanhe/tools/conf" 9 "github.com/artisanhe/tools/kafka/consumergroup" 10 ) 11 12 type ProducerKafkaConfig struct { 13 ProducerKafka KafkaConfig 14 kafkaClient sarama.Client 15 } 16 17 func (producerKafkaConfig ProducerKafkaConfig) MarshalDefaults(v interface{}) { 18 if c, ok := v.(*ProducerKafkaConfig); ok { 19 if c.ProducerKafka.Name == "" { 20 c.ProducerKafka.Name = "producekafka" 21 } 22 } 23 } 24 25 type ConsumerKafkaConfig struct { 26 ConsumerKafka KafkaConfig 27 Zookeeper ZookeeperConfig 28 Topics []string 29 ConsumerGroupName string 30 cG *consumergroup.ConsumerGroup 31 kafkaClient sarama.Client 32 } 33 34 func (consumerKafkaConfig ConsumerKafkaConfig) MarshalDefaults(v interface{}) { 35 if c, ok := v.(*ConsumerKafkaConfig); ok { 36 if c.ConsumerKafka.Name == "" { 37 c.ConsumerKafka.Name = "consumekafka" 38 } 39 if c.Zookeeper.Name == "" { 40 c.Zookeeper.Name = "consumekafka" 41 } 42 } 43 } 44 45 type KafkaConfig struct { 46 Name string 47 Addrs []string `conf:"env"` 48 Net NetConfig 49 MetaData MetaDataConfig 50 Producer ProducerConfig 51 Consumer ConsumerConfig 52 Version string 53 } 54 55 func (kafkaConfig KafkaConfig) DockerDefaults() conf.DockerDefaults { 56 return conf.DockerDefaults{ 57 "Addrs": []string{ 58 fmt.Sprintf("%s:%d", 59 conf.RancherInternal("tool-deps", kafkaConfig.Name).String(), 60 9092, 61 ), 62 }, 63 } 64 } 65 66 func (kafkaConfig KafkaConfig) MarshalDefaults(v interface{}) { 67 if c, ok := v.(*KafkaConfig); ok { 68 if c.Version == "" { 69 c.Version = "0.8.2.2" 70 } 71 } 72 } 73 74 type ZookeeperConfig struct { 75 Name string 76 Addrs []string `conf:"env"` 77 Timeout int64 78 Chroot string 79 } 80 81 func (zookeeperConfig ZookeeperConfig) DockerDefaults() conf.DockerDefaults { 82 return conf.DockerDefaults{ 83 "Addrs": []string{ 84 fmt.Sprintf("%s:%d", 85 conf.RancherInternal("tool-deps", zookeeperConfig.Name).String(), 86 2181, 87 ), 88 }, 89 } 90 } 91 92 func (zookeeperConfig ZookeeperConfig) MarshalDefaults(v interface{}) { 93 if c, ok := v.(*ZookeeperConfig); ok { 94 if c.Timeout == 0 { 95 c.Timeout = 2000 96 } 97 } 98 } 99 100 type NetConfig struct { 101 MaxOpenRequests int 102 DialTimeout int64 103 ReadTimeout int64 104 WriteTimeout int64 105 KeepAlive int64 106 SASL SASLConfig 107 } 108 109 func (netConfig NetConfig) MarshalDefaults(v interface{}) { 110 if c, ok := v.(*NetConfig); ok { 111 if c.MaxOpenRequests == 0 { 112 c.MaxOpenRequests = 3 113 } 114 if c.DialTimeout == 0 { 115 c.DialTimeout = 500 116 } 117 if c.ReadTimeout == 0 { 118 c.ReadTimeout = 2000 119 } 120 if c.WriteTimeout == 0 { 121 c.WriteTimeout = 1000 122 } 123 } 124 } 125 126 type SASLConfig struct { 127 Enable bool 128 Handshake bool 129 User string 130 Password string 131 } 132 133 type RetryConfig struct { 134 Max int 135 Backoff int64 136 } 137 138 func (retryConfig RetryConfig) MarshalDefaults(v interface{}) { 139 if c, ok := v.(*RetryConfig); ok { 140 if c.Backoff == 0 { 141 c.Backoff = 200 142 } 143 } 144 } 145 146 type MetaDataConfig struct { 147 Retry RetryConfig 148 RefreshFrequency int64 149 } 150 151 func (metaDataConfig MetaDataConfig) MarshalDefaults(v interface{}) { 152 if c, ok := v.(*MetaDataConfig); ok { 153 if c.RefreshFrequency == 0 { 154 c.RefreshFrequency = 2000 155 } 156 } 157 } 158 159 type ReturnConfig struct { 160 Successes bool 161 Errors bool 162 } 163 164 func (returnConfig ReturnConfig) MarshalDefaults(v interface{}) { 165 if c, ok := v.(*ReturnConfig); ok { 166 if !c.Successes { 167 c.Successes = true 168 } 169 if !c.Errors { 170 c.Errors = true 171 } 172 } 173 } 174 175 type FlushConfig struct { 176 MaxMessages int 177 } 178 179 func (flushConfig FlushConfig) MarshalDefaults(v interface{}) { 180 if c, ok := v.(*FlushConfig); ok { 181 if c.MaxMessages == 0 { 182 c.MaxMessages = 1 183 } 184 } 185 } 186 187 type ProducerConfig struct { 188 MaxMessageBytes int 189 RequiredAcks int 190 Timeout int64 191 Compression int8 192 Return ReturnConfig 193 Flush FlushConfig 194 Retry RetryConfig 195 } 196 197 func (producerConfig ProducerConfig) MarshalDefaults(v interface{}) { 198 if c, ok := v.(*ProducerConfig); ok { 199 if c.MaxMessageBytes == 0 { 200 c.MaxMessageBytes = 1024000 201 } 202 if c.RequiredAcks == 0 { 203 c.RequiredAcks = 1 204 } 205 if c.Timeout == 0 { 206 c.Timeout = 2000 207 } 208 } 209 } 210 211 type FetchConfig struct { 212 Min int32 213 Default int32 214 Max int32 215 } 216 217 func (fetchConfig FetchConfig) MarshalDefaults(v interface{}) { 218 if c, ok := v.(*FetchConfig); ok { 219 if c.Min == 0 { 220 c.Min = 1 221 } 222 if c.Default == 0 { 223 c.Default = 32678 224 } 225 } 226 } 227 228 type OffsetsConfig struct { 229 CommitInterval int64 230 Initial int64 231 Retention int64 232 } 233 234 func (offsetsConfig OffsetsConfig) MarshalDefaults(v interface{}) { 235 if c, ok := v.(*OffsetsConfig); ok { 236 if c.CommitInterval == 0 { 237 c.CommitInterval = 1000 238 } 239 if c.Initial == 0 { 240 c.Initial = -2 241 } 242 if c.Retention == 0 { 243 c.Retention = 3600000 244 } 245 } 246 } 247 248 type ConsumerConfig struct { 249 Retry RetryConfig 250 Fetch FetchConfig 251 MaxWaitTime int64 252 MaxProcessingTime int64 253 Return ReturnConfig 254 Offsets OffsetsConfig 255 } 256 257 func (consumerConfig ConsumerConfig) MarshalDefaults(v interface{}) { 258 if c, ok := v.(*ConsumerConfig); ok { 259 if c.MaxWaitTime == 0 { 260 c.MaxWaitTime = 200 261 } 262 if c.MaxProcessingTime == 0 { 263 c.MaxProcessingTime = 1000 264 } 265 } 266 }