diff --git a/api/rest/config.go b/api/rest/config.go index 4cea9a82..6f82faf0 100644 --- a/api/rest/config.go +++ b/api/rest/config.go @@ -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 { diff --git a/api/rest/config_test.go b/api/rest/config_test.go index 7cb7b79f..3f7cbfb5 100644 --- a/api/rest/config_test.go +++ b/api/rest/config_test.go @@ -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) diff --git a/config/util.go b/config/util.go index 8655a6e1..42394983 100644 --- a/config/util.go +++ b/config/util.go @@ -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 +} diff --git a/ipfsconn/ipfshttp/config.go b/ipfsconn/ipfshttp/config.go index 5e76502b..ab12dd64 100644 --- a/ipfsconn/ipfshttp/config.go +++ b/ipfsconn/ipfshttp/config.go @@ -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)