forked from nsqio/go-nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.go
231 lines (224 loc) · 6.61 KB
/
configure.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
package nsq
import (
"errors"
"fmt"
"reflect"
"strconv"
"time"
)
// Configure takes an option as a string and a value as an interface and
// attempts to set the appropriate configuration option on the reader instance.
//
// It attempts to coerce the value into the right format depending on the named
// option and the underlying type of the value passed in.
//
// It returns an error for an invalid option or value.
func (q *Reader) Configure(option string, value interface{}) error {
getDuration := func(v interface{}) (time.Duration, error) {
switch v.(type) {
case string:
return time.ParseDuration(v.(string))
case int, int16, uint16, int32, uint32, int64, uint64:
// treat like ms
return time.Duration(reflect.ValueOf(v).Int()) * time.Millisecond, nil
case time.Duration:
return v.(time.Duration), nil
}
return 0, errors.New("invalid value type")
}
getBool := func(v interface{}) (bool, error) {
switch value.(type) {
case bool:
return value.(bool), nil
case string:
return strconv.ParseBool(v.(string))
case int, int16, uint16, int32, uint32, int64, uint64:
return reflect.ValueOf(value).Int() != 0, nil
}
return false, errors.New("invalid value type")
}
getFloat64 := func(v interface{}) (float64, error) {
switch value.(type) {
case string:
return strconv.ParseFloat(value.(string), 64)
case int, int16, uint16, int32, uint32, int64, uint64:
return float64(reflect.ValueOf(value).Int()), nil
case float64:
return value.(float64), nil
}
return 0, errors.New("invalid value type")
}
getInt64 := func(v interface{}) (int64, error) {
switch value.(type) {
case string:
return strconv.ParseInt(v.(string), 10, 64)
case int, int16, uint16, int32, uint32, int64, uint64:
return reflect.ValueOf(value).Int(), nil
}
return 0, errors.New("invalid value type")
}
switch option {
case "read_timeout":
v, err := getDuration(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
if v > 5*time.Minute || v < 100*time.Millisecond {
return errors.New(fmt.Sprintf("invalid %s ! 100ms <= %s <= 5m", option, v))
}
q.ReadTimeout = v
case "write_timeout":
v, err := getDuration(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
if v > 5*time.Minute || v < 100*time.Millisecond {
return errors.New(fmt.Sprintf("invalid %s ! 100ms <= %s <= 5m", option, v))
}
q.WriteTimeout = v
case "lookupd_poll_interval":
v, err := getDuration(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
if v > 5*time.Minute || v < 5*time.Second {
return errors.New(fmt.Sprintf("invalid %s ! 5s <= %s <= 5m", option, v))
}
q.LookupdPollInterval = v
case "lookupd_poll_jitter":
v, err := getFloat64(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
if v < 0 || v > 1 {
return errors.New(fmt.Sprintf("invalid %s ! 0 <= %v <= 1", option, v))
}
q.LookupdPollJitter = v
case "max_requeue_delay":
v, err := getDuration(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
if v > 60*time.Minute || v < 0 {
return errors.New(fmt.Sprintf("invalid %s ! 0 <= %s <= 60m", option, v))
}
q.MaxRequeueDelay = v
case "default_requeue_delay":
v, err := getDuration(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
if v > 60*time.Minute || v < 0 {
return errors.New(fmt.Sprintf("invalid %s ! 0 <= %s <= 60m", option, v))
}
q.DefaultRequeueDelay = v
case "backoff_multiplier":
v, err := getDuration(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
if v > 60*time.Minute || v < time.Second {
return errors.New(fmt.Sprintf("invalid %s ! 1s <= %s <= 60m", option, v))
}
q.BackoffMultiplier = v
case "max_attempt_count":
v, err := getInt64(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
if v < 1 || v > 65535 {
return errors.New(fmt.Sprintf("invalid %s ! 1 <= %d <= 65535", option, v))
}
q.MaxAttemptCount = uint16(v)
case "low_rdy_idle_timeout":
v, err := getDuration(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
if v > 5*time.Minute || v < time.Second {
return errors.New(fmt.Sprintf("invalid %s ! 1s <= %s <= 5m", option, v))
}
q.LowRdyIdleTimeout = v
case "heartbeat_interval":
v, err := getDuration(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
q.HeartbeatInterval = v
case "output_buffer_size":
v, err := getInt64(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
q.OutputBufferSize = v
case "output_buffer_timeout":
v, err := getDuration(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
q.OutputBufferTimeout = v
case "tls_v1":
v, err := getBool(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
q.TLSv1 = v
case "deflate":
v, err := getBool(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
q.Deflate = v
case "deflate_level":
v, err := getInt64(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
if v < 1 || v > 9 {
return errors.New(fmt.Sprintf("invalid %s ! 1 <= %d <= 9", option, v))
}
q.DeflateLevel = int(v)
case "sample_rate":
v, err := getInt64(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
if v < 0 || v > 99 {
return errors.New(fmt.Sprintf("invalid %s ! 0 <= %d <= 99", option, v))
}
q.SampleRate = int32(v)
case "user_agent":
q.UserAgent = value.(string)
case "snappy":
v, err := getBool(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
q.Snappy = v
case "max_in_flight":
v, err := getInt64(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
if v < 1 {
return errors.New(fmt.Sprintf("invalid %s ! 1 <= %d", option, v))
}
q.SetMaxInFlight(int(v))
case "max_backoff_duration":
v, err := getDuration(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
if v > 60*time.Minute || v < 0 {
return errors.New(fmt.Sprintf("invalid %s ! 0 <= %s <= 60m", option, v))
}
q.SetMaxBackoffDuration(v)
case "verbose":
v, err := getBool(value)
if err != nil {
return errors.New(fmt.Sprintf("invalid %s - %s", option, err))
}
q.VerboseLogging = v
}
return nil
}