parse ping_interval as duration, else assume seconds and warn

Signed-off-by: Matthias Hanel <mh@synadia.com>
This commit is contained in:
Matthias Hanel
2020-02-17 12:57:21 -05:00
parent c22b2c097d
commit 3fbf8cac2e
3 changed files with 57 additions and 24 deletions

View File

@@ -41,7 +41,7 @@ max_control_line: 2048
max_payload: 65536
# ping interval and no pong threshold
ping_interval: 60
ping_interval: "60s"
ping_max: 3
# how long server can block on a socket write to a client

View File

@@ -638,7 +638,7 @@ func (o *Options) processConfigFileLine(k string, v interface{}, errors *[]error
case "max_subscriptions", "max_subs":
o.MaxSubs = int(v.(int64))
case "ping_interval":
o.PingInterval = time.Duration(int(v.(int64))) * time.Second
o.PingInterval = parseDuration("ping_interval", tk, v, errors, warnings)
case "ping_max":
o.MaxPingsOut = int(v.(int64))
case "tls":
@@ -655,28 +655,7 @@ func (o *Options) processConfigFileLine(k string, v interface{}, errors *[]error
o.TLSTimeout = tc.Timeout
o.TLSMap = tc.Map
case "write_deadline":
wd, ok := v.(string)
if ok {
dur, err := time.ParseDuration(wd)
if err != nil {
err := &configErr{tk, fmt.Sprintf("error parsing write_deadline: %v", err)}
*errors = append(*errors, err)
return
}
o.WriteDeadline = dur
} else {
// Backward compatible with old type, assume this is the
// number of seconds.
o.WriteDeadline = time.Duration(v.(int64)) * time.Second
err := &configWarningErr{
field: k,
configErr: configErr{
token: tk,
reason: "write_deadline should be converted to a duration",
},
}
*warnings = append(*warnings, err)
}
o.WriteDeadline = parseDuration("write_deadline", tk, v, errors, warnings)
case "lame_duck_duration":
dur, err := time.ParseDuration(v.(string))
if err != nil {
@@ -832,6 +811,30 @@ func (o *Options) processConfigFileLine(k string, v interface{}, errors *[]error
}
}
func parseDuration(field string, tk token, v interface{}, errors *[]error, warnings *[]error) time.Duration {
if wd, ok := v.(string); ok {
if dur, err := time.ParseDuration(wd); err != nil {
err := &configErr{tk, fmt.Sprintf("error parsing %s: %v", field, err)}
*errors = append(*errors, err)
return 0
} else {
return dur
}
} else {
// Backward compatible with old type, assume this is the
// number of seconds.
err := &configWarningErr{
field: field,
configErr: configErr{
token: tk,
reason: field + " should be converted to a duration",
},
}
*warnings = append(*warnings, err)
return time.Duration(v.(int64)) * time.Second
}
}
func trackExplicitVal(opts *Options, pm *map[string]bool, name string, val bool) {
m := *pm
if m == nil {

View File

@@ -1155,6 +1155,36 @@ func TestPanic(t *testing.T) {
}
}
func TestPingIntervalOld(t *testing.T) {
conf := createConfFile(t, []byte(`ping_interval: 5`))
defer os.Remove(conf)
opts := &Options{}
err := opts.ProcessConfigFile(conf)
if err == nil {
t.Fatalf("expected an error")
} else if err, ok := err.(*processConfigErr); !ok {
t.Fatalf("expected an error of type processConfigErr")
} else if len(err.warnings) != 1 {
t.Fatalf("expected processConfigErr to have one warning")
} else if len(err.errors) != 0 {
t.Fatalf("expected processConfigErr to have no error")
} else if opts.PingInterval != 5*time.Second {
t.Fatalf("expected ping interval to be 5 seconds")
}
}
func TestPingIntervalNew(t *testing.T) {
conf := createConfFile(t, []byte(`ping_interval: "5m"`))
defer os.Remove(conf)
opts := &Options{}
err := opts.ProcessConfigFile(conf)
if err != nil {
t.Fatalf("expected no error")
} else if opts.PingInterval != 5*time.Minute {
t.Fatalf("expected ping interval to be 5 minutes")
}
}
func TestOptionsProcessConfigFile(t *testing.T) {
// Create options with default values of Debug and Trace
// that are the opposite of what is in the config file.