Skip to content

Commit f538ede

Browse files
authored
RSDK-11744 - Increase initial read timeout to 15s, settable by env var (#5234)
1 parent 986da5e commit f538ede

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

config/reader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func readFromCloud(
227227
if !cfg.Cloud.SignalingInsecure && (checkForNewCert || tls.certificate == "" || tls.privateKey == "") {
228228
logger.Debug("reading tlsCertificate from the cloud")
229229

230-
ctxWithTimeout, cancel := contextutils.GetTimeoutCtx(ctx, shouldReadFromCache, cloudCfg.ID)
230+
ctxWithTimeout, cancel := contextutils.GetTimeoutCtx(ctx, shouldReadFromCache, cloudCfg.ID, logger)
231231
certData, err := readCertificateDataFromCloudGRPC(ctxWithTimeout, cloudCfg, conn)
232232
if err != nil {
233233
cancel()
@@ -646,7 +646,7 @@ func getFromCloudOrCache(
646646
) (*Config, bool, error) {
647647
var cached bool
648648

649-
ctxWithTimeout, cancel := contextutils.GetTimeoutCtx(ctx, shouldReadFromCache, cloudCfg.ID)
649+
ctxWithTimeout, cancel := contextutils.GetTimeoutCtx(ctx, shouldReadFromCache, cloudCfg.ID, logger)
650650
defer cancel()
651651

652652
cfg, errorShouldCheckCache, err := getFromCloudGRPC(ctxWithTimeout, cloudCfg, logger, conn)

grpc/app_conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func NewAppConn(ctx context.Context, appAddress, secret, id string, logger loggi
4040
dialOpts = append(dialOpts, rpc.WithInsecure())
4141
}
4242

43-
ctxWithTimeout, ctxWithTimeoutCancel := contextutils.GetTimeoutCtx(ctx, true, id)
43+
ctxWithTimeout, ctxWithTimeoutCancel := contextutils.GetTimeoutCtx(ctx, true, id, logger)
4444
defer ctxWithTimeoutCancel()
4545
// there will always be a deadline
4646
if deadline, ok := ctxWithTimeout.Deadline(); ok {

utils/contextutils/context.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"google.golang.org/grpc"
1414
"google.golang.org/grpc/metadata"
1515

16+
"go.viam.com/rdk/logging"
1617
"go.viam.com/rdk/utils"
1718
)
1819

@@ -30,8 +31,8 @@ const (
3031
// to the time right after the point cloud was captured.
3132
TimeReceivedMetadataKey = "viam-time-received"
3233

33-
// timeout values to use when reading a config either from App, from App behind a proxy, or from a local (cached) file.
34-
readConfigFromCloudTimeout = 5 * time.Second
34+
// Timeout values to use when reading a config either from App behind a proxy, or from App with a local (cached) file.
35+
// The timeout is far shorter when a cached config exists because the machine can always fall back to the cached config.
3536
readConfigFromCloudBehindProxyTimeout = time.Minute
3637
readCachedConfigTimeout = 1 * time.Second
3738
)
@@ -86,10 +87,13 @@ func ContextWithTimeoutIfNoDeadline(ctx context.Context, timeout time.Duration)
8687
return context.WithCancel(ctx)
8788
}
8889

89-
// GetTimeoutCtx returns a context [and its cancel function] with a timeout value determined by whether we are behind a proxy and whether a
90-
// cached config exists.
91-
func GetTimeoutCtx(ctx context.Context, shouldReadFromCache bool, id string) (context.Context, func()) {
92-
timeout := readConfigFromCloudTimeout
90+
// GetTimeoutCtx returns a context [and its cancel function] with a timeout value determined by whether an environment variable is set,
91+
// we are behind a proxy and whether a cached config exists. The timeout will always use the environment variable if set.
92+
func GetTimeoutCtx(ctx context.Context, shouldReadFromCache bool, id string, logger logging.Logger) (context.Context, func()) {
93+
timeout, isDefault := utils.GetConfigReadTimeout(logger)
94+
if !isDefault {
95+
return context.WithTimeout(ctx, timeout)
96+
}
9397
// When environment indicates we are behind a proxy, bump timeout. Network
9498
// operations tend to take longer when behind a proxy.
9599
if proxyAddr := os.Getenv(rpc.SocksProxyEnvVar); proxyAddr != "" {

utils/env.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ const (
3131
// that modules are allowed to startup.
3232
ModuleStartupTimeoutEnvVar = "VIAM_MODULE_STARTUP_TIMEOUT"
3333

34+
// DefaultConfigReadTimeout is the default config read timeout. If there
35+
// is a cached config on the machine, a shorter default timeout will be used.
36+
DefaultConfigReadTimeout = 15 * time.Second
37+
38+
// ConfigReadTimeoutEnvVar is the environment variable that can
39+
// be set to override DefaultConfigReadTimeout as the duration
40+
// for config read.
41+
ConfigReadTimeoutEnvVar = "VIAM_CONFIG_READ_TIMEOUT"
42+
3443
// AndroidFilesDir is hardcoded because golang inits before our android code can override HOME var.
3544
AndroidFilesDir = "/data/user/0/com.viam.rdk.fgservice/cache"
3645

@@ -83,26 +92,34 @@ var windowsPathRegex = regexp.MustCompile(`^(\w:)?(.+)$`)
8392
// timeout (env variable value if set, DefaultResourceConfigurationTimeout
8493
// otherwise).
8594
func GetResourceConfigurationTimeout(logger logging.Logger) time.Duration {
86-
return timeoutHelper(DefaultResourceConfigurationTimeout, ResourceConfigurationTimeoutEnvVar, logger)
95+
timeout, _ := timeoutHelper(DefaultResourceConfigurationTimeout, ResourceConfigurationTimeoutEnvVar, logger)
96+
return timeout
8797
}
8898

8999
// GetModuleStartupTimeout calculates the module startup timeout
90100
// (env variable value if set, DefaultModuleStartupTimeout otherwise).
91101
func GetModuleStartupTimeout(logger logging.Logger) time.Duration {
92-
return timeoutHelper(DefaultModuleStartupTimeout, ModuleStartupTimeoutEnvVar, logger)
102+
timeout, _ := timeoutHelper(DefaultModuleStartupTimeout, ModuleStartupTimeoutEnvVar, logger)
103+
return timeout
104+
}
105+
106+
// GetConfigReadTimeout returns the config read timeout set by the env variable value,
107+
// DefaultConfigReadTimeout otherwise.
108+
func GetConfigReadTimeout(logger logging.Logger) (time.Duration, bool) {
109+
return timeoutHelper(DefaultConfigReadTimeout, ConfigReadTimeoutEnvVar, logger)
93110
}
94111

95-
func timeoutHelper(defaultTimeout time.Duration, timeoutEnvVar string, logger logging.Logger) time.Duration {
112+
func timeoutHelper(defaultTimeout time.Duration, timeoutEnvVar string, logger logging.Logger) (time.Duration, bool) {
96113
if timeoutVal := os.Getenv(timeoutEnvVar); timeoutVal != "" {
97114
timeout, err := time.ParseDuration(timeoutVal)
98115
if err != nil {
99116
logger.Warnf("Failed to parse %s env var, falling back to default %v timeout",
100117
timeoutEnvVar, defaultTimeout)
101-
return defaultTimeout
118+
return defaultTimeout, true
102119
}
103-
return timeout
120+
return timeout, false
104121
}
105-
return defaultTimeout
122+
return defaultTimeout, true
106123
}
107124

108125
// PlatformHomeDir wraps Getenv("HOME"), except on android, where it returns the app cache directory.

web/server/entrypoint.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ func logViamEnvVariables(logger logging.Logger) {
7777
if value, exists := os.LookupEnv("VIAM_MODULE_STARTUP_TIMEOUT"); exists {
7878
viamEnvVariables = append(viamEnvVariables, "VIAM_MODULE_STARTUP_TIMEOUT", value)
7979
}
80+
if value, exists := os.LookupEnv("VIAM_CONFIG_READ_TIMEOUT"); exists {
81+
viamEnvVariables = append(viamEnvVariables, "VIAM_CONFIG_READ_TIMEOUT", value)
82+
}
8083
if value, exists := os.LookupEnv("CWD"); exists {
8184
viamEnvVariables = append(viamEnvVariables, "CWD", value)
8285
}

0 commit comments

Comments
 (0)