Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit ebc42f4

Browse files
committed
chore: extract backoff instance creation to a method
1 parent 3e8aa26 commit ebc42f4

2 files changed

Lines changed: 23 additions & 28 deletions

File tree

e2e/elasticsearch.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,20 +203,7 @@ func search(stackName string, indexName string, query map[string]interface{}) (s
203203
// if elasticsearch does not get healthy status in a defined number of minutes.
204204
//nolint:unused
205205
func waitForElasticsearch(maxTimeoutMinutes time.Duration, stackName string) (bool, error) {
206-
var (
207-
initialInterval = 500 * time.Millisecond
208-
randomizationFactor = 0.5
209-
multiplier = 2.0
210-
maxInterval = 5 * time.Second
211-
maxElapsedTime = maxTimeoutMinutes * time.Minute
212-
)
213-
214-
exp := backoff.NewExponentialBackOff()
215-
exp.InitialInterval = initialInterval
216-
exp.RandomizationFactor = randomizationFactor
217-
exp.Multiplier = multiplier
218-
exp.MaxInterval = maxInterval
219-
exp.MaxElapsedTime = maxElapsedTime
206+
exp := getExponentialBackOff(maxTimeoutMinutes)
220207

221208
retryCount := 1
222209

e2e/utils.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@ const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
2121
var seededRand *rand.Rand = rand.New(
2222
rand.NewSource(time.Now().UnixNano()))
2323

24+
// getExponentialBackOff returns a preconfigured exponential backoff instance
25+
//nolint:unused
26+
func getExponentialBackOff(elapsedMinutes time.Duration) *backoff.ExponentialBackOff {
27+
var (
28+
initialInterval = 500 * time.Millisecond
29+
randomizationFactor = 0.5
30+
multiplier = 2.0
31+
maxInterval = 5 * time.Second
32+
maxElapsedTime = elapsedMinutes * time.Minute
33+
)
34+
35+
exp := backoff.NewExponentialBackOff()
36+
exp.InitialInterval = initialInterval
37+
exp.RandomizationFactor = randomizationFactor
38+
exp.Multiplier = multiplier
39+
exp.MaxInterval = maxInterval
40+
exp.MaxElapsedTime = maxElapsedTime
41+
42+
return exp
43+
}
44+
2445
// downloadFile will download a url and store it in a temporary path.
2546
// It writes to the destination file as it downloads it, without
2647
// loading the entire file into memory.
@@ -38,20 +59,7 @@ func downloadFile(url string) (string, error) {
3859

3960
filepath := tempFile.Name()
4061

41-
var (
42-
initialInterval = 500 * time.Millisecond
43-
randomizationFactor = 0.5
44-
multiplier = 2.0
45-
maxInterval = 5 * time.Second
46-
maxElapsedTime = 3 * time.Minute
47-
)
48-
49-
exp := backoff.NewExponentialBackOff()
50-
exp.InitialInterval = initialInterval
51-
exp.RandomizationFactor = randomizationFactor
52-
exp.Multiplier = multiplier
53-
exp.MaxInterval = maxInterval
54-
exp.MaxElapsedTime = maxElapsedTime
62+
exp := getExponentialBackOff(3)
5563

5664
retryCount := 1
5765
var fileReader io.ReadCloser

0 commit comments

Comments
 (0)