@@ -5,31 +5,37 @@ import (
55)
66
77type (
8- // Options contains configuration parameters for resource testing behavior.
8+ // options contains configuration parameters for resource testing behavior.
99 // These options control retry intervals, maximum wait times, and the number
1010 // of attempts made when testing resource availability.
11- Options struct {
12- interval time.Duration // Initial retry interval between attempts
13- maxInterval time.Duration // Maximum interval for exponential backoff
14- attempts uint64 // Maximum number of retry attempts
11+ options struct {
12+ interval time.Duration // Initial retry interval between attempts
13+ maxInterval time.Duration // Maximum interval for exponential backoff
14+ attempts uint64 // Maximum number of retry attempts
15+ multiplier float64 // Multiplier for exponential backoff
16+ randomizationFactor float64 // Randomization factor for backoff intervals
1517 }
1618
17- // Option is a function type used to configure Options through the functional
19+ // Option is a function type used to configure options through the functional
1820 // options pattern. This allows flexible and extensible configuration of
1921 // resource testing behavior.
20- Option func (opts * Options )
22+ Option func (opts * options )
2123)
2224
23- // newOptions creates a new Options instance with default values and applies
25+ // newOptions creates a new options instance with default values and applies
2426// the provided option setters. Default values are:
2527// - interval: 5 seconds
26- // - maxInterval: 60 seconds
28+ // - maxInterval: 60 seconds
2729// - attempts: 5.
28- func newOptions (setters []Option ) * Options {
29- opts := & Options {
30- interval : time .Duration (5 ) * time .Second ,
31- maxInterval : time .Duration (60 ) * time .Second ,
32- attempts : 5 ,
30+ // - multiplier: 1.5
31+ // - randomizationFactor: 0.5
32+ func newOptions (setters []Option ) * options {
33+ opts := & options {
34+ interval : time .Duration (5 ) * time .Second ,
35+ maxInterval : time .Duration (60 ) * time .Second ,
36+ attempts : 5 ,
37+ multiplier : 1.5 ,
38+ randomizationFactor : 0.5 ,
3339 }
3440
3541 for _ , setter := range setters {
@@ -47,7 +53,7 @@ func newOptions(setters []Option) *Options {
4753//
4854// runner.Test(ctx, resources, waitfor.WithInterval(2)) // Start with 2 second intervals
4955func WithInterval (interval uint64 ) Option {
50- return func (opts * Options ) {
56+ return func (opts * options ) {
5157 opts .interval = time .Duration (interval ) * time .Second
5258 }
5359}
@@ -60,7 +66,7 @@ func WithInterval(interval uint64) Option {
6066//
6167// runner.Test(ctx, resources, waitfor.WithMaxInterval(30)) // Cap at 30 seconds
6268func WithMaxInterval (interval uint64 ) Option {
63- return func (opts * Options ) {
69+ return func (opts * options ) {
6470 opts .maxInterval = time .Duration (interval ) * time .Second
6571 }
6672}
@@ -73,7 +79,34 @@ func WithMaxInterval(interval uint64) Option {
7379//
7480// runner.Test(ctx, resources, waitfor.WithAttempts(10)) // Try up to 10 times
7581func WithAttempts (attempts uint64 ) Option {
76- return func (opts * Options ) {
82+ return func (opts * options ) {
7783 opts .attempts = attempts
7884 }
7985}
86+
87+ // WithMultiplier creates an Option that sets the multiplier for exponential backoff.
88+ // This value determines how quickly the retry interval increases after each attempt.
89+ // A higher multiplier results in faster growth of the interval.
90+ //
91+ // Example:
92+ //
93+ // runner.Test(ctx, resources, waitfor.WithMultiplier(2.0)) // Double the interval each time
94+ func WithMultiplier (multiplier float64 ) Option {
95+ return func (opts * options ) {
96+ opts .multiplier = multiplier
97+ }
98+ }
99+
100+ // WithRandomizationFactor creates an Option that sets the randomization factor for
101+ // exponential backoff. This factor introduces jitter to the retry intervals,
102+ // helping to prevent thundering herd problems when multiple clients are retrying
103+ // simultaneously.
104+ //
105+ // Example:
106+ //
107+ // runner.Test(ctx, resources, waitfor.WithRandomizationFactor(0.5)) // 50% jitter
108+ func WithRandomizationFactor (factor float64 ) Option {
109+ return func (opts * options ) {
110+ opts .randomizationFactor = factor
111+ }
112+ }
0 commit comments