|
| 1 | +/** |
| 2 | + * Copyright 2025, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.optimizely.ab.cmab.client; |
| 17 | +/** |
| 18 | + * Configuration for retry behavior in CMAB client operations. |
| 19 | + */ |
| 20 | +public class RetryConfig { |
| 21 | + private final int maxRetries; |
| 22 | + private final long backoffBaseMs; |
| 23 | + private final double backoffMultiplier; |
| 24 | + private final int maxTimeoutMs; |
| 25 | + |
| 26 | + /** |
| 27 | + * Creates a RetryConfig with custom retry and backoff settings. |
| 28 | + * |
| 29 | + * @param maxRetries Maximum number of retry attempts |
| 30 | + * @param backoffBaseMs Base delay in milliseconds for the first retry |
| 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 |
| 33 | + */ |
| 34 | + public RetryConfig(int maxRetries, long backoffBaseMs, double backoffMultiplier, int maxTimeoutMs) { |
| 35 | + if (maxRetries < 0) { |
| 36 | + throw new IllegalArgumentException("maxRetries cannot be negative"); |
| 37 | + } |
| 38 | + if (backoffBaseMs < 0) { |
| 39 | + throw new IllegalArgumentException("backoffBaseMs cannot be negative"); |
| 40 | + } |
| 41 | + if (backoffMultiplier < 1.0) { |
| 42 | + throw new IllegalArgumentException("backoffMultiplier must be >= 1.0"); |
| 43 | + } |
| 44 | + if (maxTimeoutMs < 0) { |
| 45 | + throw new IllegalArgumentException("maxTimeoutMs cannot be negative"); |
| 46 | + } |
| 47 | + |
| 48 | + this.maxRetries = maxRetries; |
| 49 | + this.backoffBaseMs = backoffBaseMs; |
| 50 | + this.backoffMultiplier = backoffMultiplier; |
| 51 | + this.maxTimeoutMs = maxTimeoutMs; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Creates a RetryConfig with default backoff settings and timeout (1 second base, 2x multiplier, 10 second timeout). |
| 56 | + * |
| 57 | + * @param maxRetries Maximum number of retry attempts |
| 58 | + */ |
| 59 | + public RetryConfig(int maxRetries) { |
| 60 | + this(maxRetries, 1000, 2.0, 10000); // Default: 1 second base, exponential backoff, 10 second timeout |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Creates a default RetryConfig with 3 retries and exponential backoff. |
| 65 | + */ |
| 66 | + public static RetryConfig defaultConfig() { |
| 67 | + return new RetryConfig(3); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Creates a RetryConfig with no retries (single attempt only). |
| 72 | + */ |
| 73 | + public static RetryConfig noRetry() { |
| 74 | + return new RetryConfig(0, 0, 1.0, 0); |
| 75 | + } |
| 76 | + |
| 77 | + public int getMaxRetries() { |
| 78 | + return maxRetries; |
| 79 | + } |
| 80 | + |
| 81 | + public long getBackoffBaseMs() { |
| 82 | + return backoffBaseMs; |
| 83 | + } |
| 84 | + |
| 85 | + public double getBackoffMultiplier() { |
| 86 | + return backoffMultiplier; |
| 87 | + } |
| 88 | + |
| 89 | + public int getMaxTimeoutMs() { |
| 90 | + return maxTimeoutMs; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Calculates the delay for a specific retry attempt. |
| 95 | + * |
| 96 | + * @param attemptNumber The attempt number (0-based, so 0 = first retry) |
| 97 | + * @return Delay in milliseconds |
| 98 | + */ |
| 99 | + public long calculateDelay(int attemptNumber) { |
| 100 | + if (attemptNumber < 0) { |
| 101 | + return 0; |
| 102 | + } |
| 103 | + return (long) (backoffBaseMs * Math.pow(backoffMultiplier, attemptNumber)); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public String toString() { |
| 108 | + return String.format("RetryConfig{maxRetries=%d, backoffBaseMs=%d, backoffMultiplier=%.1f, maxTimeoutMs=%d}", |
| 109 | + maxRetries, backoffBaseMs, backoffMultiplier, maxTimeoutMs); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public boolean equals(Object obj) { |
| 114 | + if (this == obj) return true; |
| 115 | + if (obj == null || getClass() != obj.getClass()) return false; |
| 116 | + |
| 117 | + RetryConfig that = (RetryConfig) obj; |
| 118 | + return maxRetries == that.maxRetries && |
| 119 | + backoffBaseMs == that.backoffBaseMs && |
| 120 | + maxTimeoutMs == that.maxTimeoutMs && |
| 121 | + Double.compare(that.backoffMultiplier, backoffMultiplier) == 0; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public int hashCode() { |
| 126 | + int result = maxRetries; |
| 127 | + result = 31 * result + Long.hashCode(backoffBaseMs); |
| 128 | + result = 31 * result + Double.hashCode(backoffMultiplier); |
| 129 | + result = 31 * result + Integer.hashCode(maxTimeoutMs); |
| 130 | + return result; |
| 131 | + } |
| 132 | +} |
0 commit comments