Skip to content

Commit

Permalink
retryable connection now accepts a config
Browse files Browse the repository at this point in the history
  • Loading branch information
kostaskoukouvis committed Dec 13, 2022
1 parent 83766f0 commit 2bbc576
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,16 @@ type RetryableConnection struct {
retryClient *retryablehttp.Client
}

type RetryableConfig struct {
Timeout time.Duration
RetryWaitMin time.Duration
RetryWaitMax time.Duration
RetryMax int
NoLog bool
}

// NewRetryableConnection ...
func NewRetryableConnection(host, core string, client *http.Client, maxTimeout time.Duration, noLog bool) (*RetryableConnection, error) {
func NewRetryableConnection(host, core string, client *http.Client, conf *RetryableConfig) (*RetryableConnection, error) {
if host == "" || core == "" {
return nil, ErrInvalidConfig
}
Expand All @@ -115,19 +123,30 @@ func NewRetryableConnection(host, core string, client *http.Client, maxTimeout t
return nil, err
}

if conf == nil {
conf = &RetryableConfig{
Timeout: 10 * time.Second,
RetryWaitMin: 50 * time.Millisecond,
RetryWaitMax: 2 * time.Second,
RetryMax: 4,
NoLog: true,
}
}

retryClient := retryablehttp.NewClient()
retryClient.HTTPClient = client
retryClient.RetryWaitMin = 10 * time.Millisecond
retryClient.RetryWaitMax = maxTimeout
retryClient.RetryMax = 10
if noLog {
retryClient.HTTPClient.Timeout = conf.Timeout
retryClient.RetryWaitMin = conf.RetryWaitMin
retryClient.RetryWaitMax = conf.RetryWaitMax
retryClient.RetryMax = conf.RetryMax
if conf.NoLog {
retryClient.Logger = log.New(io.Discard, "", log.LstdFlags)
}

return &RetryableConnection{
Host: host,
Core: core,
Timeout: maxTimeout,
Timeout: conf.Timeout,
httpClient: client,
retryClient: retryClient,
}, nil
Expand Down

0 comments on commit 2bbc576

Please sign in to comment.