|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 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 | + * http://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 | + |
| 17 | +package com.google.adk.agents; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableSet; |
| 20 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
| 21 | +import java.time.Duration; |
| 22 | +import java.util.Objects; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +/** Configuration for retrying transient failures at the LLM provider-call boundary. */ |
| 26 | +public final class RetryConfig { |
| 27 | + |
| 28 | + private static final ImmutableSet<Integer> DEFAULT_RETRYABLE_STATUS_CODES = |
| 29 | + ImmutableSet.of(408, 429, 500, 502, 503, 504); |
| 30 | + |
| 31 | + private final int maxAttempts; |
| 32 | + private final Duration initialBackoff; |
| 33 | + private final Duration maxBackoff; |
| 34 | + private final double multiplier; |
| 35 | + private final double jitterRatio; |
| 36 | + private final ImmutableSet<Integer> retryableStatusCodes; |
| 37 | + |
| 38 | + private RetryConfig(Builder builder) { |
| 39 | + this.maxAttempts = builder.maxAttempts; |
| 40 | + this.initialBackoff = builder.initialBackoff; |
| 41 | + this.maxBackoff = builder.maxBackoff; |
| 42 | + this.multiplier = builder.multiplier; |
| 43 | + this.jitterRatio = builder.jitterRatio; |
| 44 | + this.retryableStatusCodes = ImmutableSet.copyOf(builder.retryableStatusCodes); |
| 45 | + } |
| 46 | + |
| 47 | + /** Returns a retry configuration that performs exactly one provider attempt. */ |
| 48 | + public static RetryConfig disabled() { |
| 49 | + return builder().build(); |
| 50 | + } |
| 51 | + |
| 52 | + /** Returns a builder whose defaults keep retries disabled. */ |
| 53 | + public static Builder builder() { |
| 54 | + return new Builder(); |
| 55 | + } |
| 56 | + |
| 57 | + public int maxAttempts() { |
| 58 | + return maxAttempts; |
| 59 | + } |
| 60 | + |
| 61 | + public Duration initialBackoff() { |
| 62 | + return initialBackoff; |
| 63 | + } |
| 64 | + |
| 65 | + public Duration maxBackoff() { |
| 66 | + return maxBackoff; |
| 67 | + } |
| 68 | + |
| 69 | + public double multiplier() { |
| 70 | + return multiplier; |
| 71 | + } |
| 72 | + |
| 73 | + public double jitterRatio() { |
| 74 | + return jitterRatio; |
| 75 | + } |
| 76 | + |
| 77 | + public ImmutableSet<Integer> retryableStatusCodes() { |
| 78 | + return retryableStatusCodes; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public boolean equals(Object other) { |
| 83 | + if (this == other) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + if (!(other instanceof RetryConfig that)) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + return maxAttempts == that.maxAttempts |
| 90 | + && Double.compare(multiplier, that.multiplier) == 0 |
| 91 | + && Double.compare(jitterRatio, that.jitterRatio) == 0 |
| 92 | + && initialBackoff.equals(that.initialBackoff) |
| 93 | + && maxBackoff.equals(that.maxBackoff) |
| 94 | + && retryableStatusCodes.equals(that.retryableStatusCodes); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public int hashCode() { |
| 99 | + return Objects.hash( |
| 100 | + maxAttempts, initialBackoff, maxBackoff, multiplier, jitterRatio, retryableStatusCodes); |
| 101 | + } |
| 102 | + |
| 103 | + /** Builder for {@link RetryConfig}. */ |
| 104 | + public static final class Builder { |
| 105 | + private int maxAttempts = 1; |
| 106 | + private Duration initialBackoff = Duration.ofMillis(500); |
| 107 | + private Duration maxBackoff = Duration.ofSeconds(8); |
| 108 | + private double multiplier = 2.0; |
| 109 | + private double jitterRatio = 0.0; |
| 110 | + private Set<Integer> retryableStatusCodes = DEFAULT_RETRYABLE_STATUS_CODES; |
| 111 | + |
| 112 | + private Builder() {} |
| 113 | + |
| 114 | + @CanIgnoreReturnValue |
| 115 | + public Builder maxAttempts(int maxAttempts) { |
| 116 | + this.maxAttempts = maxAttempts; |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + @CanIgnoreReturnValue |
| 121 | + public Builder initialBackoff(Duration initialBackoff) { |
| 122 | + this.initialBackoff = Objects.requireNonNull(initialBackoff, "initialBackoff cannot be null"); |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + @CanIgnoreReturnValue |
| 127 | + public Builder maxBackoff(Duration maxBackoff) { |
| 128 | + this.maxBackoff = Objects.requireNonNull(maxBackoff, "maxBackoff cannot be null"); |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + @CanIgnoreReturnValue |
| 133 | + public Builder multiplier(double multiplier) { |
| 134 | + this.multiplier = multiplier; |
| 135 | + return this; |
| 136 | + } |
| 137 | + |
| 138 | + @CanIgnoreReturnValue |
| 139 | + public Builder jitterRatio(double jitterRatio) { |
| 140 | + this.jitterRatio = jitterRatio; |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + @CanIgnoreReturnValue |
| 145 | + public Builder retryableStatusCodes(Set<Integer> retryableStatusCodes) { |
| 146 | + this.retryableStatusCodes = |
| 147 | + Objects.requireNonNull(retryableStatusCodes, "retryableStatusCodes cannot be null"); |
| 148 | + return this; |
| 149 | + } |
| 150 | + |
| 151 | + public RetryConfig build() { |
| 152 | + if (maxAttempts < 1) { |
| 153 | + throw new IllegalArgumentException("maxAttempts must be at least 1."); |
| 154 | + } |
| 155 | + if (initialBackoff.isNegative()) { |
| 156 | + throw new IllegalArgumentException("initialBackoff cannot be negative."); |
| 157 | + } |
| 158 | + if (maxBackoff.isNegative()) { |
| 159 | + throw new IllegalArgumentException("maxBackoff cannot be negative."); |
| 160 | + } |
| 161 | + if (maxBackoff.compareTo(initialBackoff) < 0) { |
| 162 | + throw new IllegalArgumentException("maxBackoff cannot be less than initialBackoff."); |
| 163 | + } |
| 164 | + if (!Double.isFinite(multiplier) || multiplier < 1.0) { |
| 165 | + throw new IllegalArgumentException("multiplier must be finite and at least 1.0."); |
| 166 | + } |
| 167 | + if (!Double.isFinite(jitterRatio) || jitterRatio < 0.0 || jitterRatio > 1.0) { |
| 168 | + throw new IllegalArgumentException("jitterRatio must be between 0.0 and 1.0."); |
| 169 | + } |
| 170 | + for (int statusCode : retryableStatusCodes) { |
| 171 | + if (statusCode < 100 || statusCode > 599) { |
| 172 | + throw new IllegalArgumentException( |
| 173 | + "retryableStatusCodes must contain valid HTTP status codes."); |
| 174 | + } |
| 175 | + } |
| 176 | + return new RetryConfig(this); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments