mirror of
https://github.com/openfaas/faas.git
synced 2025-06-15 11:46:46 +00:00
added go duration support for read_timeout, write_timeout, exec_timeout in watchdog
Signed-off-by: Vivek Singh <vivekkmr45@yahoo.in> updated testcases applied go fmt to readconfig Signed-off-by: Vivek Singh <vivekkmr45@yahoo.in> added requested changes in code review Signed-off-by: Vivek Singh <vivekkmr45@yahoo.in>
This commit is contained in:
parent
ed6a09f71f
commit
62900cf46e
@ -162,16 +162,34 @@ func TestRead_ReadAndWriteTimeoutConfig(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRead_ReadAndWriteTimeoutDurationConfig(t *testing.T) {
|
||||||
|
defaults := NewEnvBucket()
|
||||||
|
defaults.Setenv("read_timeout", "20s")
|
||||||
|
defaults.Setenv("write_timeout", "1m30s")
|
||||||
|
|
||||||
|
readConfig := ReadConfig{}
|
||||||
|
config := readConfig.Read(defaults)
|
||||||
|
|
||||||
|
if (config.readTimeout) != time.Duration(20)*time.Second {
|
||||||
|
t.Logf("readTimeout incorrect, got: %d\n", config.readTimeout)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if (config.writeTimeout) != time.Duration(90)*time.Second {
|
||||||
|
t.Logf("writeTimeout incorrect, got: %d\n", config.writeTimeout)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRead_ExecTimeoutConfig(t *testing.T) {
|
func TestRead_ExecTimeoutConfig(t *testing.T) {
|
||||||
defaults := NewEnvBucket()
|
defaults := NewEnvBucket()
|
||||||
defaults.Setenv("exec_timeout", "3")
|
defaults.Setenv("exec_timeout", "3s")
|
||||||
|
|
||||||
readConfig := ReadConfig{}
|
readConfig := ReadConfig{}
|
||||||
config := readConfig.Read(defaults)
|
config := readConfig.Read(defaults)
|
||||||
|
|
||||||
want := time.Duration(3) * time.Second
|
want := time.Duration(3) * time.Second
|
||||||
if (config.execTimeout) != want {
|
if (config.execTimeout) != want {
|
||||||
t.Logf("readTimeout incorrect, got: %d - want: %s\n", config.execTimeout, want)
|
t.Logf("execTimeout incorrect, got: %d - want: %s\n", config.execTimeout, want)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,16 +28,19 @@ func parseBoolValue(val string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseIntValue(val string) int {
|
func parseIntOrDurationValue(val string, fallback time.Duration) time.Duration {
|
||||||
if len(val) > 0 {
|
if len(val) > 0 {
|
||||||
parsedVal, parseErr := strconv.Atoi(val)
|
parsedVal, parseErr := strconv.Atoi(val)
|
||||||
|
|
||||||
if parseErr == nil && parsedVal >= 0 {
|
if parseErr == nil && parsedVal >= 0 {
|
||||||
|
return time.Duration(parsedVal) * time.Second
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return parsedVal
|
duration, durationErr := time.ParseDuration(val)
|
||||||
|
if durationErr != nil {
|
||||||
|
return fallback
|
||||||
}
|
}
|
||||||
}
|
return duration
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read fetches config from environmental variables.
|
// Read fetches config from environmental variables.
|
||||||
@ -49,21 +52,10 @@ func (ReadConfig) Read(hasEnv HasEnv) WatchdogConfig {
|
|||||||
|
|
||||||
cfg.faasProcess = hasEnv.Getenv("fprocess")
|
cfg.faasProcess = hasEnv.Getenv("fprocess")
|
||||||
|
|
||||||
readTimeout := parseIntValue(hasEnv.Getenv("read_timeout"))
|
cfg.readTimeout = parseIntOrDurationValue(hasEnv.Getenv("read_timeout"), time.Second*5)
|
||||||
writeTimeout := parseIntValue(hasEnv.Getenv("write_timeout"))
|
cfg.writeTimeout = parseIntOrDurationValue(hasEnv.Getenv("write_timeout"), time.Second*5)
|
||||||
|
|
||||||
cfg.execTimeout = time.Duration(parseIntValue(hasEnv.Getenv("exec_timeout"))) * time.Second
|
cfg.execTimeout = parseIntOrDurationValue(hasEnv.Getenv("exec_timeout"), time.Second*0)
|
||||||
|
|
||||||
if readTimeout == 0 {
|
|
||||||
readTimeout = 5
|
|
||||||
}
|
|
||||||
|
|
||||||
if writeTimeout == 0 {
|
|
||||||
writeTimeout = 5
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg.readTimeout = time.Duration(readTimeout) * time.Second
|
|
||||||
cfg.writeTimeout = time.Duration(writeTimeout) * time.Second
|
|
||||||
|
|
||||||
writeDebugEnv := hasEnv.Getenv("write_debug")
|
writeDebugEnv := hasEnv.Getenv("write_debug")
|
||||||
if isBoolValueSet(writeDebugEnv) {
|
if isBoolValueSet(writeDebugEnv) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user