-
Notifications
You must be signed in to change notification settings - Fork 110
/
env.go
74 lines (62 loc) · 2.55 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package utils
import (
"os"
"runtime"
"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"
// AndroidFilesDir is hardcoded because golang inits before our android code can override HOME var.
AndroidFilesDir = "/data/user/0/com.viam.rdk.fgservice/cache"
)
// 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
}
// PlatformHomeDir wraps Getenv("HOME"), except on android, where it returns the app cache directory.
func PlatformHomeDir() string {
if runtime.GOOS == "android" {
return AndroidFilesDir
}
return os.Getenv("HOME")
}
// PlatformMkdirTemp wraps MkdirTemp. On android, when dir is empty, it uses a path
// that is writable + executable.
func PlatformMkdirTemp(dir, pattern string) (string, error) {
if runtime.GOOS == "android" && dir == "" {
dir = AndroidFilesDir
}
return os.MkdirTemp(dir, pattern)
}