forked from obsidiandynamics/goharvest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
227 lines (208 loc) · 7.84 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
225
226
227
package goharvest
import (
"fmt"
"os"
"time"
validation "github.com/go-ozzo/ozzo-validation"
"github.com/obsidiandynamics/goneli"
"github.com/obsidiandynamics/libstdgo/scribe"
"gopkg.in/yaml.v2"
)
// Duration is a convenience for deriving a pointer from a given Duration argument.
func Duration(d time.Duration) *time.Duration {
return &d
}
// Int is a convenience for deriving a pointer from a given int argument.
func Int(i int) *int {
return &i
}
// Limits configuration.
type Limits struct {
IOErrorBackoff *time.Duration `yaml:"ioErrorBackoff"`
PollDuration *time.Duration `yaml:"pollDuration"`
MinPollInterval *time.Duration `yaml:"minPollInterval"`
MaxPollInterval *time.Duration `yaml:"maxPollInterval"`
HeartbeatTimeout *time.Duration `yaml:"heartbeatTimeout"`
DrainInterval *time.Duration `yaml:"drainInterval"`
QueueTimeout *time.Duration `yaml:"queueTimeout"`
MarkBackoff *time.Duration `yaml:"markBackoff"`
MaxInFlightRecords *int `yaml:"maxInFlightRecords"`
SendConcurrency *int `yaml:"sendConcurrency"`
SendBuffer *int `yaml:"sendBuffer"`
MarkQueryRecords *int `yaml:"markQueryRecords"`
MinMetricsInterval *time.Duration `yaml:"minMetricsInterval"`
}
func defaultInt(i **int, def int) {
if *i == nil {
*i = &def
}
}
func defaultDuration(d **time.Duration, def time.Duration) {
if *d == nil {
*d = &def
}
}
// SetDefaults assigns the defaults for optional values.
func (l *Limits) SetDefaults() {
defaultDuration(&l.IOErrorBackoff, 500*time.Millisecond)
defaultDuration(&l.HeartbeatTimeout, goneli.DefaultHeartbeatTimeout)
defaultDuration(&l.MaxPollInterval, *l.HeartbeatTimeout/2)
defaultDuration(&l.QueueTimeout, 30*time.Second)
defaultDuration(&l.DrainInterval, minDuration(*l.MaxPollInterval, *l.QueueTimeout))
defaultDuration(&l.MarkBackoff, 10*time.Millisecond)
defaultInt(&l.MaxInFlightRecords, 1000)
defaultInt(&l.SendConcurrency, 8)
defaultInt(&l.SendBuffer, 10)
defaultInt(&l.MarkQueryRecords, 100)
defaultDuration(&l.MinMetricsInterval, 5*time.Second)
}
func minDuration(d0, d1 time.Duration) time.Duration {
if d0 < d1 {
return d0
}
return d1
}
// Validate the Limits configuration, returning an error if invalid
func (l Limits) Validate() error {
minimumMaxPollInterval := 1 * time.Millisecond
if l.MinPollInterval != nil {
minimumMaxPollInterval = *l.MinPollInterval
}
return validation.ValidateStruct(&l,
validation.Field(&l.IOErrorBackoff, validation.Min(0)),
validation.Field(&l.DrainInterval, validation.Required, validation.Min(1*time.Millisecond)),
validation.Field(&l.MaxPollInterval, validation.Required, validation.Min(minimumMaxPollInterval)),
validation.Field(&l.QueueTimeout, validation.Required, validation.Min(1*time.Millisecond)),
validation.Field(&l.MarkBackoff, validation.Min(0)),
validation.Field(&l.MaxInFlightRecords, validation.Required, validation.Min(1)),
validation.Field(&l.SendConcurrency, validation.Required, validation.Min(1)),
validation.Field(&l.SendBuffer, validation.Min(0)),
validation.Field(&l.MarkQueryRecords, validation.Required, validation.Min(1)),
validation.Field(&l.MinMetricsInterval, validation.Min(0)),
)
}
// String obtains a textural representation of Limits.
func (l Limits) String() string {
return fmt.Sprint(
"Limits[IOErrorBackoff=", l.IOErrorBackoff,
", PollDuration=", l.PollDuration,
", MinPollInterval=", l.MinPollInterval,
", MaxPollInterval=", l.MaxPollInterval,
", HeartbeatTimeout=", l.HeartbeatTimeout,
", DrainInterval=", l.DrainInterval,
", QueueTimeout=", l.QueueTimeout,
", MarkBackoff=", l.MarkBackoff,
", MaxInFlightRecords=", l.MaxInFlightRecords,
", SendConcurrency=", l.SendConcurrency,
", SendBuffer=", l.SendBuffer,
", MarkQueryRecords=", l.MarkQueryRecords,
", MinMetricsInterval=", l.MinMetricsInterval, "]",
)
}
// KafkaConfigMap represents the Kafka key-value configuration.
type KafkaConfigMap map[string]interface{}
// Config encapsulates configuration for Harvest.
type Config struct {
BaseKafkaConfig KafkaConfigMap `yaml:"baseKafkaConfig"`
ProducerKafkaConfig KafkaConfigMap `yaml:"producerKafkaConfig"`
LeaderTopic string `yaml:"leaderTopic"`
LeaderGroupID string `yaml:"leaderGroupID"`
DataSource string `yaml:"dataSource"`
OutboxTable string `yaml:"outboxTable"`
Limits Limits `yaml:"limits"`
KafkaConsumerProvider KafkaConsumerProvider
KafkaProducerProvider KafkaProducerProvider
DatabaseBindingProvider DatabaseBindingProvider
NeliProvider NeliProvider
Scribe scribe.Scribe
Name string `yaml:"name"`
}
// Validate the Config, returning an error if invalid.
func (c Config) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(&c.BaseKafkaConfig, validation.NotNil),
validation.Field(&c.ProducerKafkaConfig, validation.NotNil),
validation.Field(&c.DataSource, validation.Required),
validation.Field(&c.OutboxTable, validation.Required),
validation.Field(&c.Limits),
validation.Field(&c.KafkaConsumerProvider, validation.NotNil),
validation.Field(&c.KafkaProducerProvider, validation.NotNil),
validation.Field(&c.DatabaseBindingProvider, validation.NotNil),
validation.Field(&c.NeliProvider, validation.NotNil),
validation.Field(&c.Scribe, validation.NotNil),
validation.Field(&c.Name, validation.Required),
)
}
// Obtains a textual representation of the configuration.
func (c Config) String() string {
return fmt.Sprint(
"Config[BaseKafkaConfig=", c.BaseKafkaConfig,
", ProducerKafkaConfig=", c.ProducerKafkaConfig,
", LeaderTopic=", c.LeaderTopic,
", LeaderGroupID=", c.LeaderGroupID,
", DataSource=", c.DataSource,
", OutboxTable=", c.OutboxTable,
", Limits=", c.Limits,
", KafkaConsumerProvider=", c.KafkaConsumerProvider,
", KafkaProducerProvider=", c.KafkaProducerProvider,
", DatabaseBindingProvider=", c.DatabaseBindingProvider,
", NeliProvider=", c.NeliProvider,
", Scribe=", c.Scribe,
", Name=", c.Name, "]")
}
// SetDefaults assigns the default values to optional fields.
func (c *Config) SetDefaults() {
if c.BaseKafkaConfig == nil {
c.BaseKafkaConfig = KafkaConfigMap{}
}
if _, ok := c.BaseKafkaConfig["bootstrap.servers"]; !ok {
c.BaseKafkaConfig["bootstrap.servers"] = "localhost:9092"
}
if c.ProducerKafkaConfig == nil {
c.ProducerKafkaConfig = KafkaConfigMap{}
}
if c.DataSource == "" {
c.DataSource = "host=localhost port=5432 user=postgres password= dbname=postgres sslmode=disable"
}
if c.OutboxTable == "" {
c.OutboxTable = "outbox"
}
c.Limits.SetDefaults()
if c.KafkaConsumerProvider == nil {
c.KafkaConsumerProvider = StandardKafkaConsumerProvider()
}
if c.KafkaProducerProvider == nil {
c.KafkaProducerProvider = StandardKafkaProducerProvider()
}
if c.DatabaseBindingProvider == nil {
c.DatabaseBindingProvider = StandardPostgresBindingProvider()
}
if c.NeliProvider == nil {
c.NeliProvider = StandardNeliProvider()
}
if c.Scribe == nil {
c.Scribe = scribe.New(scribe.StandardBinding())
}
if c.Name == "" {
c.Name = fmt.Sprintf("%s_%d_%d", goneli.Sanitise(getString("localhost", os.Hostname)), os.Getpid(), time.Now().Unix())
}
}
// Unmarshal a configuration from a byte slice, returning the configuration struct with pre-initialised defaults,
// or an error if unmarshalling failed. The configuration is not validated prior to returning, in case further
// amendments are required by the caller. The caller should call Validate() independently.
func Unmarshal(in []byte) (Config, error) {
cfg := Config{}
err := yaml.UnmarshalStrict(in, &cfg)
if err == nil {
cfg.SetDefaults()
}
return cfg, err
}
type stringGetter func() (string, error)
func getString(def string, stringGetter stringGetter) string {
str, err := stringGetter()
if err != nil {
return def
}
return str
}