-
Notifications
You must be signed in to change notification settings - Fork 109
/
env.go
53 lines (44 loc) · 1.88 KB
/
env.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
package utils
import (
"os"
"time"
"go.viam.com/rdk/logging"
)
const (
// DefaultResourceConfigurationTimeout is the default resource configuration
// timeout.
DefaultResourceConfigurationTimeout = time.Minute
// ResourceConfigurationTimeoutEnvVar is the environment variable that can
// be set to override DefaultResourceConfigurationTimeout as the duration
// that resources are allowed to (re)configure.
ResourceConfigurationTimeoutEnvVar = "VIAM_RESOURCE_CONFIGURATION_TIMEOUT"
// DefaultModuleStartupTimeout is the default module startup timeout.
DefaultModuleStartupTimeout = 5 * time.Minute
// ModuleStartupTimeoutEnvVar is the environment variable that can
// be set to override DefaultModuleStartupTimeout as the duration
// that modules are allowed to startup.
ModuleStartupTimeoutEnvVar = "VIAM_MODULE_STARTUP_TIMEOUT"
)
// GetResourceConfigurationTimeout calculates the resource configuration
// timeout (env variable value if set, DefaultResourceConfigurationTimeout
// otherwise).
func GetResourceConfigurationTimeout(logger logging.Logger) time.Duration {
return timeoutHelper(DefaultResourceConfigurationTimeout, ResourceConfigurationTimeoutEnvVar, logger)
}
// GetModuleStartupTimeout calculates the module startup timeout
// (env variable value if set, DefaultModuleStartupTimeout otherwise).
func GetModuleStartupTimeout(logger logging.Logger) time.Duration {
return timeoutHelper(DefaultModuleStartupTimeout, ModuleStartupTimeoutEnvVar, logger)
}
func timeoutHelper(defaultTimeout time.Duration, timeoutEnvVar string, logger logging.Logger) time.Duration {
if timeoutVal := os.Getenv(timeoutEnvVar); timeoutVal != "" {
timeout, err := time.ParseDuration(timeoutVal)
if err != nil {
logger.Warn("Failed to parse %s env var, falling back to default %v timeout",
timeoutEnvVar, defaultTimeout)
return defaultTimeout
}
return timeout
}
return defaultTimeout
}