Config: provide utility to parse duration arguments. Use it.

Should reduce complexity (codeclimate).

License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
This commit is contained in:
Hector Sanjuan 2018-03-16 14:22:11 +01:00
parent b989e1726d
commit a9a58a50a0
4 changed files with 52 additions and 55 deletions

View File

@ -228,32 +228,13 @@ func (cfg *Config) loadHTTPOptions(jcfg *jsonConfig) error {
cfg.TLS = tlsCfg
}
// only overwrite defaults when we can parse the time
// 0 is a valid value
t, err := time.ParseDuration(jcfg.ReadTimeout)
if err != nil {
return fmt.Errorf("error parsing restapi.read_timeout: %s", err)
}
cfg.ReadTimeout = t
t, err = time.ParseDuration(jcfg.ReadHeaderTimeout)
if err != nil {
return fmt.Errorf("error parsing restapi.read_header_timeout: %s", err)
}
cfg.ReadHeaderTimeout = t
t, err = time.ParseDuration(jcfg.WriteTimeout)
if err != nil {
return fmt.Errorf("error parsing restapi.write_timeout: %s", err)
}
cfg.WriteTimeout = t
t, err = time.ParseDuration(jcfg.IdleTimeout)
if err != nil {
return fmt.Errorf("error parsing restapi.idle_timeout: %s", err)
}
cfg.IdleTimeout = t
return nil
return config.ParseDurations(
"restapi",
&config.DurationOpt{jcfg.ReadTimeout, &cfg.ReadTimeout, "read_timeout"},
&config.DurationOpt{jcfg.ReadHeaderTimeout, &cfg.ReadHeaderTimeout, "read_header_timeout"},
&config.DurationOpt{jcfg.WriteTimeout, &cfg.WriteTimeout, "write_timeout"},
&config.DurationOpt{jcfg.IdleTimeout, &cfg.IdleTimeout, "idle_timeout"},
)
}
func (cfg *Config) loadLibp2pOptions(jcfg *jsonConfig) error {

View File

@ -3,6 +3,7 @@ package rest
import (
"encoding/json"
"testing"
"time"
crypto "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
@ -29,6 +30,13 @@ func TestLoadJSON(t *testing.T) {
t.Fatal(err)
}
if cfg.ReadTimeout != 30*time.Second ||
cfg.WriteTimeout != time.Minute ||
cfg.ReadHeaderTimeout != 5*time.Second ||
cfg.IdleTimeout != 2*time.Minute {
t.Error("error parsing timeouts")
}
j := &jsonConfig{}
json.Unmarshal(cfgJSON, j)

View File

@ -2,6 +2,7 @@ package config
import (
"encoding/json"
"fmt"
"time"
)
@ -81,3 +82,30 @@ func SetIfNotDefault(src interface{}, dest interface{}) {
}
}
}
// DurationOpt provides a datatype to use with ParseDurations
type DurationOpt struct {
// The duration we need to parse
Duration string
// Where to store the result
Dst *time.Duration
// A variable name associated to it for helpful errors.
Name string
}
// ParseDurations takes a time.Duration src and saves it to the given dst. into the given
func ParseDurations(component string, args ...*DurationOpt) error {
for _, arg := range args {
t, err := time.ParseDuration(arg.Duration)
if err != nil {
return fmt.Errorf(
"error parsing %s.%s: %s",
component,
arg.Name,
err,
)
}
*arg.Dst = t
}
return nil
}

View File

@ -154,37 +154,17 @@ func (cfg *Config) LoadJSON(raw []byte) error {
cfg.ProxyAddr = proxyAddr
cfg.NodeAddr = nodeAddr
// only overwrite defaults when we can parse the time
// Note for these 0 is a valid value.
t, err := time.ParseDuration(jcfg.ProxyReadTimeout)
err = config.ParseDurations(
"ipfshttp",
&config.DurationOpt{jcfg.ProxyReadTimeout, &cfg.ProxyReadTimeout, "proxy_read_timeout"},
&config.DurationOpt{jcfg.ProxyReadHeaderTimeout, &cfg.ProxyReadHeaderTimeout, "proxy_read_header_timeout"},
&config.DurationOpt{jcfg.ProxyWriteTimeout, &cfg.ProxyWriteTimeout, "proxy_write_timeout"},
&config.DurationOpt{jcfg.ProxyIdleTimeout, &cfg.ProxyIdleTimeout, "proxy_idle_timeout"},
&config.DurationOpt{jcfg.ConnectSwarmsDelay, &cfg.ConnectSwarmsDelay, "connect_swarms_delay"},
)
if err != nil {
return fmt.Errorf("error parsing proxy_read_timeout: %s", err)
return err
}
cfg.ProxyReadTimeout = t
t, err = time.ParseDuration(jcfg.ProxyReadHeaderTimeout)
if err != nil {
return fmt.Errorf("error parsing proxy_read_header_timeout: %s", err)
}
cfg.ProxyReadHeaderTimeout = t
t, err = time.ParseDuration(jcfg.ProxyWriteTimeout)
if err != nil {
return fmt.Errorf("error parsing proxy_write_timeout: %s", err)
}
cfg.ProxyWriteTimeout = t
t, err = time.ParseDuration(jcfg.ProxyIdleTimeout)
if err != nil {
return fmt.Errorf("error parsing proxy_idle_timeout: %s", err)
}
cfg.ProxyIdleTimeout = t
t, err = time.ParseDuration(jcfg.ConnectSwarmsDelay)
if err != nil {
return fmt.Errorf("error parsing connect_swarms_delay: %s", err)
}
cfg.ConnectSwarmsDelay = t
config.SetIfNotDefault(jcfg.PinMethod, &cfg.PinMethod)