Skip to content

Commit

Permalink
configure exponential backoff from cmdline
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Mar 29, 2022
1 parent ba306df commit 7b21b35
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/job/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type GlobalConfig struct {
SkipEncrypted bool
EnablePrimitiveJobs bool
ScaleFactor int
Backoff utils.BackoffConfig
}

// NewGlobalConfigWithFlags returns a GlobalConfig initialized with command line flags.
Expand All @@ -60,6 +61,13 @@ func NewGlobalConfigWithFlags() *GlobalConfig {
flag.IntVar(&res.ScaleFactor, "scale", utils.GetEnvIntDefault("SCALE_FACTOR", 1),
"used to scale the amount of jobs being launched, effect is similar to launching multiple instances at once")

flag.IntVar(&res.Backoff.Limit, "backoff-limit", utils.GetEnvIntDefault("BACKOFF_LIMIT", utils.DefaultBackoffConfig().Limit),
"how much exponential backoff can be scaled")
flag.IntVar(&res.Backoff.Multiplier, "backoff-multiplier", utils.GetEnvIntDefault("BACKOFF_MULTIPLIER", utils.DefaultBackoffConfig().Multiplier),
"how much exponential backoff is scaled with each new error")
flag.DurationVar(&res.Backoff.Timeout, "backoff-timeout", utils.GetEnvDurationDefault("BACKOFF_TIMEOUT", utils.DefaultBackoffConfig().Timeout),
"initial exponential backoff timeout")

return &res
}

Expand Down
2 changes: 1 addition & 1 deletion src/job/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func fastHTTPJob(ctx context.Context, logger *zap.Logger, globalConfig *GlobalCo
return nil, err
}

backoffController := utils.NewBackoffController(jobConfig.BackoffConfig)
backoffController := utils.NewBackoffController(utils.NonNilBackoffConfigOrDefault(jobConfig.BackoffConfig, globalConfig.Backoff))

client := http.NewClient(ctx, *clientConfig, logger)

Expand Down
2 changes: 1 addition & 1 deletion src/job/rawnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func tcpJob(ctx context.Context, logger *zap.Logger, globalConfig *GlobalConfig,
return nil, err
}

backoffController := utils.NewBackoffController(jobConfig.BackoffConfig)
backoffController := utils.NewBackoffController(utils.NonNilBackoffConfigOrDefault(jobConfig.BackoffConfig, globalConfig.Backoff))

if globalConfig.ProxyURLs != "" {
jobConfig.proxyURLs = templates.ParseAndExecute(logger, globalConfig.ProxyURLs, ctx)
Expand Down
14 changes: 9 additions & 5 deletions src/utils/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,21 @@ func DefaultBackoffConfig() BackoffConfig {
return BackoffConfig{Multiplier: defaultMultiplier, Limit: defaultLimit, Timeout: time.Microsecond}
}

func NonNilBackoffConfigOrDefault(c *BackoffConfig, defaultConfig BackoffConfig) *BackoffConfig {
if c != nil {
return c
}

return &defaultConfig
}

type BackoffController struct {
BackoffConfig
count int
}

func NewBackoffController(c *BackoffConfig) BackoffController {
if c != nil {
return BackoffController{BackoffConfig: *c}
}

return BackoffController{BackoffConfig: DefaultBackoffConfig()}
return BackoffController{BackoffConfig: *NonNilBackoffConfigOrDefault(c, DefaultBackoffConfig())}
}

func (c BackoffController) GetTimeout() time.Duration {
Expand Down

0 comments on commit 7b21b35

Please sign in to comment.