Skip to content

Commit

Permalink
dedup NonNilOrDefault with generics
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Apr 1, 2022
1 parent 16545e1 commit dd6d94d
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 38 deletions.
18 changes: 9 additions & 9 deletions src/core/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func NewClient(ctx context.Context, clientConfig ClientConfig, logger *zap.Logge
defaultTimeout = 90 * time.Second
)

timeout := utils.NonNilDurationOrDefault(clientConfig.Timeout, defaultTimeout)
timeout := utils.NonNilOrDefault(clientConfig.Timeout, defaultTimeout)

tlsConfig := &tls.Config{
InsecureSkipVerify: true, //nolint:gosec // This is intentional
Expand All @@ -110,10 +110,10 @@ func NewClient(ctx context.Context, clientConfig ClientConfig, logger *zap.Logge
Addr: clientConfig.StaticHost.Addr,
IsTLS: clientConfig.StaticHost.IsTLS,
MaxConnDuration: timeout,
ReadTimeout: utils.NonNilDurationOrDefault(clientConfig.ReadTimeout, timeout),
WriteTimeout: utils.NonNilDurationOrDefault(clientConfig.WriteTimeout, timeout),
MaxIdleConnDuration: utils.NonNilDurationOrDefault(clientConfig.IdleTimeout, timeout),
MaxConns: utils.NonNilIntOrDefault(clientConfig.MaxIdleConns, defaultMaxConnsPerHost),
ReadTimeout: utils.NonNilOrDefault(clientConfig.ReadTimeout, timeout),
WriteTimeout: utils.NonNilOrDefault(clientConfig.WriteTimeout, timeout),
MaxIdleConnDuration: utils.NonNilOrDefault(clientConfig.IdleTimeout, timeout),
MaxConns: utils.NonNilOrDefault(clientConfig.MaxIdleConns, defaultMaxConnsPerHost),
NoDefaultUserAgentHeader: true, // Don't send: User-Agent: fasthttp
DisableHeaderNamesNormalizing: true, // If you set the case on your headers correctly you can enable this
DisablePathNormalizing: true,
Expand All @@ -124,10 +124,10 @@ func NewClient(ctx context.Context, clientConfig ClientConfig, logger *zap.Logge

return &fasthttp.Client{
MaxConnDuration: timeout,
ReadTimeout: utils.NonNilDurationOrDefault(clientConfig.ReadTimeout, timeout),
WriteTimeout: utils.NonNilDurationOrDefault(clientConfig.WriteTimeout, timeout),
MaxIdleConnDuration: utils.NonNilDurationOrDefault(clientConfig.IdleTimeout, timeout),
MaxConnsPerHost: utils.NonNilIntOrDefault(clientConfig.MaxIdleConns, defaultMaxConnsPerHost),
ReadTimeout: utils.NonNilOrDefault(clientConfig.ReadTimeout, timeout),
WriteTimeout: utils.NonNilOrDefault(clientConfig.WriteTimeout, timeout),
MaxIdleConnDuration: utils.NonNilOrDefault(clientConfig.IdleTimeout, timeout),
MaxConnsPerHost: utils.NonNilOrDefault(clientConfig.MaxIdleConns, defaultMaxConnsPerHost),
NoDefaultUserAgentHeader: true, // Don't send: User-Agent: fasthttp
DisableHeaderNamesNormalizing: true, // If you set the case on your headers correctly you can enable this
DisablePathNormalizing: true,
Expand Down
2 changes: 1 addition & 1 deletion src/job/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (c *BasicJobConfig) FromGlobal(global GlobalConfig) {
}

func (c BasicJobConfig) GetInterval() time.Duration {
return utils.NonNilDurationOrDefault(c.Interval, time.Duration(c.IntervalMs)*time.Millisecond)
return utils.NonNilOrDefault(c.Interval, time.Duration(c.IntervalMs)*time.Millisecond)
}

// Next comment for linter
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(utils.NonNilBackoffConfigOrDefault(jobConfig.Backoff, globalConfig.Backoff))
backoffController := utils.BackoffController{BackoffConfig: utils.NonNilOrDefault(jobConfig.Backoff, globalConfig.Backoff)}

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

Expand Down
2 changes: 1 addition & 1 deletion src/job/packetgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func packetgenJob(ctx context.Context, logger *zap.Logger, globalConfig *GlobalC
return nil, fmt.Errorf("error parsing job config: %w", err)
}

backoffController := utils.NewBackoffController(utils.NonNilBackoffConfigOrDefault(jobConfig.Backoff, globalConfig.Backoff))
backoffController := utils.BackoffController{BackoffConfig: utils.NonNilOrDefault(jobConfig.Backoff, globalConfig.Backoff)}

trafficMonitor := metrics.Default.NewWriter(metrics.Traffic, uuid.New().String())
go trafficMonitor.Update(ctx, time.Second)
Expand Down
4 changes: 2 additions & 2 deletions 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(utils.NonNilBackoffConfigOrDefault(jobConfig.Backoff, globalConfig.Backoff))
backoffController := utils.BackoffController{BackoffConfig: utils.NonNilOrDefault(jobConfig.Backoff, globalConfig.Backoff)}

if globalConfig.ProxyURLs != "" {
jobConfig.proxyURLs = templates.ParseAndExecute(logger, globalConfig.ProxyURLs, ctx)
Expand Down Expand Up @@ -202,6 +202,6 @@ func parseRawNetJobArgs(ctx context.Context, logger *zap.Logger, globalConfig *G
addr: targetAddress,
bodyTpl: bodyTpl,
proxyURLs: proxyURLs,
timeout: utils.NonNilDurationOrDefault(jobConfig.Timeout, time.Minute),
timeout: utils.NonNilOrDefault(jobConfig.Timeout, time.Minute),
}, nil
}
12 changes: 0 additions & 12 deletions src/utils/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,11 @@ 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 {
return BackoffController{BackoffConfig: *NonNilBackoffConfigOrDefault(c, DefaultBackoffConfig())}
}

func (c BackoffController) GetTimeout() time.Duration {
result := c.Timeout
for i := 0; i < c.count; i++ {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/countrychecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func CheckCountry(countriesToAvoid []string, strictCountryCheck bool, proxyURLs
)

counter := Counter{Count: maxFetchRetries}
backoffController := NewBackoffController(nil)
backoffController := BackoffController{BackoffConfig: DefaultBackoffConfig()}

for counter.Next() {
log.Printf("Checking IP address, attempt #%d", counter.iter)
Expand Down
14 changes: 3 additions & 11 deletions src/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,9 @@ func GetEnvDurationDefault(key string, defaultValue time.Duration) time.Duration
return v
}

func NonNilDurationOrDefault(d *time.Duration, dflt time.Duration) time.Duration {
if d != nil {
return *d
}

return dflt
}

func NonNilIntOrDefault(i *int, dflt int) int {
if i != nil {
return *i
func NonNilOrDefault[T any](v *T, dflt T) T {
if v != nil {
return *v
}

return dflt
Expand Down

0 comments on commit dd6d94d

Please sign in to comment.