13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- package com .optimizely .ab .cmab ;
16
+ package com .optimizely .ab .cmab . client ;
17
17
/**
18
18
* Configuration for retry behavior in CMAB client operations.
19
19
*/
20
20
public class RetryConfig {
21
21
private final int maxRetries ;
22
22
private final long backoffBaseMs ;
23
23
private final double backoffMultiplier ;
24
-
24
+ private final int maxTimeoutMs ;
25
+
25
26
/**
26
27
* Creates a RetryConfig with custom retry and backoff settings.
27
28
*
28
29
* @param maxRetries Maximum number of retry attempts
29
30
* @param backoffBaseMs Base delay in milliseconds for the first retry
30
31
* @param backoffMultiplier Multiplier for exponential backoff (e.g., 2.0 for doubling)
32
+ * @param maxTimeoutMs Maximum total timeout in milliseconds for all retry attempts
31
33
*/
32
- public RetryConfig (int maxRetries , long backoffBaseMs , double backoffMultiplier ) {
34
+ public RetryConfig (int maxRetries , long backoffBaseMs , double backoffMultiplier , int maxTimeoutMs ) {
33
35
if (maxRetries < 0 ) {
34
36
throw new IllegalArgumentException ("maxRetries cannot be negative" );
35
37
}
@@ -39,19 +41,23 @@ public RetryConfig(int maxRetries, long backoffBaseMs, double backoffMultiplier)
39
41
if (backoffMultiplier < 1.0 ) {
40
42
throw new IllegalArgumentException ("backoffMultiplier must be >= 1.0" );
41
43
}
44
+ if (maxTimeoutMs < 0 ) {
45
+ throw new IllegalArgumentException ("maxTimeoutMs cannot be negative" );
46
+ }
42
47
43
48
this .maxRetries = maxRetries ;
44
49
this .backoffBaseMs = backoffBaseMs ;
45
50
this .backoffMultiplier = backoffMultiplier ;
51
+ this .maxTimeoutMs = maxTimeoutMs ;
46
52
}
47
53
48
54
/**
49
- * Creates a RetryConfig with default backoff settings (1 second base, 2x multiplier).
55
+ * Creates a RetryConfig with default backoff settings and timeout (1 second base, 2x multiplier, 10 second timeout ).
50
56
*
51
57
* @param maxRetries Maximum number of retry attempts
52
58
*/
53
59
public RetryConfig (int maxRetries ) {
54
- this (maxRetries , 1000 , 2.0 ); // Default: 1 second base, exponential backoff
60
+ this (maxRetries , 1000 , 2.0 , 10000 ); // Default: 1 second base, exponential backoff, 10 second timeout
55
61
}
56
62
57
63
/**
@@ -65,7 +71,7 @@ public static RetryConfig defaultConfig() {
65
71
* Creates a RetryConfig with no retries (single attempt only).
66
72
*/
67
73
public static RetryConfig noRetry () {
68
- return new RetryConfig (0 );
74
+ return new RetryConfig (0 , 0 , 1.0 , 0 );
69
75
}
70
76
71
77
public int getMaxRetries () {
@@ -80,6 +86,10 @@ public double getBackoffMultiplier() {
80
86
return backoffMultiplier ;
81
87
}
82
88
89
+ public int getMaxTimeoutMs () {
90
+ return maxTimeoutMs ;
91
+ }
92
+
83
93
/**
84
94
* Calculates the delay for a specific retry attempt.
85
95
*
@@ -95,8 +105,8 @@ public long calculateDelay(int attemptNumber) {
95
105
96
106
@ Override
97
107
public String toString () {
98
- return String .format ("RetryConfig{maxRetries=%d, backoffBaseMs=%d, backoffMultiplier=%.1f}" ,
99
- maxRetries , backoffBaseMs , backoffMultiplier );
108
+ return String .format ("RetryConfig{maxRetries=%d, backoffBaseMs=%d, backoffMultiplier=%.1f, maxTimeoutMs=%d }" ,
109
+ maxRetries , backoffBaseMs , backoffMultiplier , maxTimeoutMs );
100
110
}
101
111
102
112
@ Override
@@ -107,6 +117,7 @@ public boolean equals(Object obj) {
107
117
RetryConfig that = (RetryConfig ) obj ;
108
118
return maxRetries == that .maxRetries &&
109
119
backoffBaseMs == that .backoffBaseMs &&
120
+ maxTimeoutMs == that .maxTimeoutMs &&
110
121
Double .compare (that .backoffMultiplier , backoffMultiplier ) == 0 ;
111
122
}
112
123
@@ -115,6 +126,7 @@ public int hashCode() {
115
126
int result = maxRetries ;
116
127
result = 31 * result + Long .hashCode (backoffBaseMs );
117
128
result = 31 * result + Double .hashCode (backoffMultiplier );
129
+ result = 31 * result + Integer .hashCode (maxTimeoutMs );
118
130
return result ;
119
131
}
120
132
}
0 commit comments