fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

grpc: zoekt-sourcegraph-indexserer: enable by default, support reading from SG_FEATURE_FLAG_GRPC (#650)

+131 -1
+34 -1
cmd/zoekt-sourcegraph-indexserver/main.go
··· 1213 1213 fs.IntVar(&rc.blockProfileRate, "block_profile_rate", getEnvWithDefaultInt("BLOCK_PROFILE_RATE", -1), "Sampling rate of Go's block profiler in nanoseconds. Values <=0 disable the blocking profiler Var(default). A value of 1 includes every blocking event. See https://pkg.go.dev/runtime#SetBlockProfileRate") 1214 1214 fs.DurationVar(&rc.backoffDuration, "backoff_duration", getEnvWithDefaultDuration("BACKOFF_DURATION", 10*time.Minute), "for the given duration we backoff from enqueue operations for a repository that's failed its previous indexing attempt. Consecutive failures increase the duration of the delay linearly up to the maxBackoffDuration. A negative value disables indexing backoff.") 1215 1215 fs.DurationVar(&rc.maxBackoffDuration, "max_backoff_duration", getEnvWithDefaultDuration("MAX_BACKOFF_DURATION", 120*time.Minute), "the maximum duration to backoff from enqueueing a repo for indexing. A negative value disables indexing backoff.") 1216 - fs.BoolVar(&rc.useGRPC, "use_grpc", getEnvWithDefaultBool("GRPC_ENABLED", false), "use the gRPC API to talk to Sourcegraph") 1216 + fs.BoolVar(&rc.useGRPC, "use_grpc", mustGetBoolFromEnvironmentVariables([]string{"GRPC_ENABLED", "SG_FEATURE_FLAG_GRPC"}, true), "use the gRPC API to talk to Sourcegraph") 1217 1217 1218 1218 // flags related to shard merging 1219 1219 fs.DurationVar(&rc.vacuumInterval, "vacuum_interval", getEnvWithDefaultDuration("SRC_VACUUM_INTERVAL", 24*time.Hour), "run vacuum this often") ··· 1596 1596 log.Fatal(err) 1597 1597 } 1598 1598 } 1599 + 1600 + // mustGetBoolFromEnvironmentVariables is like getBoolFromEnvironmentVariables, but it panics 1601 + // if any of the provided environment variables fails to parse as a boolean. 1602 + func mustGetBoolFromEnvironmentVariables(envVarNames []string, defaultBool bool) bool { 1603 + value, err := getBoolFromEnvironmentVariables(envVarNames, defaultBool) 1604 + if err != nil { 1605 + panic(err) 1606 + } 1607 + 1608 + return value 1609 + } 1610 + 1611 + // getBoolFromEnvironmentVariables returns the boolean defined by the first environment 1612 + // variable listed in envVarNames that is set in the current process environment, or the defaultBool if none are set. 1613 + // 1614 + // An error is returned of the provided environment variables fails to parse as a boolean. 1615 + func getBoolFromEnvironmentVariables(envVarNames []string, defaultBool bool) (bool, error) { 1616 + for _, envVar := range envVarNames { 1617 + v := os.Getenv(envVar) 1618 + if v == "" { 1619 + continue 1620 + } 1621 + 1622 + b, err := strconv.ParseBool(v) 1623 + if err != nil { 1624 + return false, fmt.Errorf("parsing environment variable %q to boolean: %v", envVar, err) 1625 + } 1626 + 1627 + return b, nil 1628 + } 1629 + 1630 + return defaultBool, nil 1631 + }
+97
cmd/zoekt-sourcegraph-indexserver/main_test.go
··· 286 286 } 287 287 } 288 288 289 + func TestGetBoolFromEnvironmentVariables(t *testing.T) { 290 + testCases := []struct { 291 + name string 292 + envVarsToSet map[string]string 293 + 294 + envVarNames []string 295 + defaultBool bool 296 + 297 + wantBool bool 298 + wantErr bool 299 + }{ 300 + { 301 + name: "respect default value: true", 302 + 303 + envVarsToSet: map[string]string{}, 304 + 305 + envVarNames: []string{"FOO", "BAR"}, 306 + defaultBool: true, 307 + 308 + wantBool: true, 309 + }, 310 + { 311 + name: "respect default value: false", 312 + 313 + envVarsToSet: map[string]string{}, 314 + 315 + envVarNames: []string{"FOO", "BAR"}, 316 + defaultBool: false, 317 + 318 + wantBool: false, 319 + }, 320 + { 321 + name: "read from environment", 322 + 323 + envVarsToSet: map[string]string{"FOO": "1"}, 324 + 325 + envVarNames: []string{"FOO"}, 326 + defaultBool: false, 327 + 328 + wantBool: true, 329 + }, 330 + { 331 + name: "read from first env var that is set", 332 + 333 + envVarsToSet: map[string]string{ 334 + "BAR": "false", 335 + "BAZ": "true", 336 + }, 337 + 338 + envVarNames: []string{"FOO", "BAR", "BAZ"}, 339 + defaultBool: true, 340 + 341 + wantBool: false, 342 + }, 343 + 344 + { 345 + name: "should error for invalid input", 346 + 347 + envVarsToSet: map[string]string{"INVALID": "not a boolean"}, 348 + 349 + envVarNames: []string{"INVALID"}, 350 + defaultBool: false, 351 + 352 + wantErr: true, 353 + }, 354 + } 355 + 356 + for _, tc := range testCases { 357 + t.Run("", func(t *testing.T) { 358 + // Prepare the environment by loading all the appropriate environment variables 359 + for _, v := range tc.envVarNames { 360 + _ = os.Unsetenv(v) 361 + } 362 + 363 + for k, _ := range tc.envVarsToSet { 364 + _ = os.Unsetenv(k) 365 + } 366 + 367 + for k, v := range tc.envVarsToSet { 368 + t.Setenv(k, v) 369 + } 370 + 371 + // Run the test 372 + got, err := getBoolFromEnvironmentVariables(tc.envVarNames, tc.defaultBool) 373 + 374 + // Examine the results 375 + if tc.wantErr != (err != nil) { 376 + t.Fatalf("unexpected error (wantErr = %t): %v", tc.wantErr, err) 377 + } 378 + 379 + if got != tc.wantBool { 380 + t.Errorf("got %v, want %v", got, tc.wantBool) 381 + } 382 + }) 383 + } 384 + } 385 + 289 386 func TestAddDefaultPort(t *testing.T) { 290 387 tests := []struct { 291 388 name string