@@ -149,43 +149,22 @@ else if (postResult != null) {
149149 @ SuppressWarnings ("unchecked" )
150150 protected ExecutionResult onFailure (ExecutionResult result ) {
151151 failedAttempts ++;
152+ long waitNanos = delayNanos ;
152153
153154 // Determine the computed delay
154155 Duration computedDelay = policy .computeDelay (execution );
155-
156- // Determine the non-computed delay
157- if (computedDelay == null ) {
158- Duration delay = policy .getDelay ();
159- Duration delayMin = policy .getDelayMin ();
160- Duration delayMax = policy .getDelayMax ();
161-
162- if (delayNanos == -1 && delay != null && !delay .equals (Duration .ZERO ))
163- delayNanos = delay .toNanos ();
164- else if (delayMin != null && delayMax != null )
165- delayNanos = randomDelayInRange (delayMin .toNanos (), delayMax .toNanos (), Math .random ());
166-
167- // Adjust for backoff
168- if (execution .getAttemptCount () != 1 && policy .getMaxDelay () != null )
169- delayNanos = (long ) Math .min (delayNanos * policy .getDelayFactor (), policy .getMaxDelay ().toNanos ());
156+ if (computedDelay != null ) {
157+ waitNanos = computedDelay .toNanos ();
158+ } else {
159+ // Determine the fixed or random delay
160+ waitNanos = getFixedOrRandomDelayNanos (waitNanos );
161+ waitNanos = adjustForBackoff (waitNanos );
162+ delayNanos = waitNanos ;
170163 }
171164
172- // The wait time, which is the delay time adjusted for jitter and max duration, in nanoseconds
173- long waitNanos = computedDelay != null ? computedDelay .toNanos () : delayNanos ;
174-
175- // Adjust the wait time for jitter
176- if (policy .getJitter () != null )
177- waitNanos = randomDelay (waitNanos , policy .getJitter ().toNanos (), Math .random ());
178- else if (policy .getJitterFactor () > 0.0 )
179- waitNanos = randomDelay (waitNanos , policy .getJitterFactor (), Math .random ());
180-
181- // Adjust the wait time for max duration
165+ waitNanos = adjustForJitter (waitNanos );
182166 long elapsedNanos = execution .getElapsedTime ().toNanos ();
183- if (policy .getMaxDuration () != null ) {
184- long maxRemainingWaitTime = policy .getMaxDuration ().toNanos () - elapsedNanos ;
185- waitNanos = Math .min (waitNanos , maxRemainingWaitTime < 0 ? 0 : maxRemainingWaitTime );
186- if (waitNanos < 0 )
187- waitNanos = 0 ;
188- }
167+ waitNanos = adjustForMaxDuration (waitNanos , elapsedNanos );
189168
190169 // Calculate result
191170 boolean maxRetriesExceeded = policy .getMaxRetries () != -1 && failedAttempts > policy .getMaxRetries ();
@@ -208,4 +187,40 @@ else if (retriesExceededListener != null && !success && retriesExceeded)
208187
209188 return result .with (waitNanos , completed , success );
210189 }
190+
191+ private long getFixedOrRandomDelayNanos (long waitNanos ) {
192+ Duration delay = policy .getDelay ();
193+ Duration delayMin = policy .getDelayMin ();
194+ Duration delayMax = policy .getDelayMax ();
195+
196+ if (waitNanos == -1 && delay != null && !delay .equals (Duration .ZERO ))
197+ waitNanos = delay .toNanos ();
198+ else if (delayMin != null && delayMax != null )
199+ waitNanos = randomDelayInRange (delayMin .toNanos (), delayMax .toNanos (), Math .random ());
200+ return waitNanos ;
201+ }
202+
203+ private long adjustForBackoff (long waitNanos ) {
204+ if (execution .getAttemptCount () != 1 && policy .getMaxDelay () != null )
205+ waitNanos = (long ) Math .min (waitNanos * policy .getDelayFactor (), policy .getMaxDelay ().toNanos ());
206+ return waitNanos ;
207+ }
208+
209+ private long adjustForJitter (long waitNanos ) {
210+ if (policy .getJitter () != null )
211+ waitNanos = randomDelay (waitNanos , policy .getJitter ().toNanos (), Math .random ());
212+ else if (policy .getJitterFactor () > 0.0 )
213+ waitNanos = randomDelay (waitNanos , policy .getJitterFactor (), Math .random ());
214+ return waitNanos ;
215+ }
216+
217+ private long adjustForMaxDuration (long waitNanos , long elapsedNanos ) {
218+ if (policy .getMaxDuration () != null ) {
219+ long maxRemainingWaitTime = policy .getMaxDuration ().toNanos () - elapsedNanos ;
220+ waitNanos = Math .min (waitNanos , maxRemainingWaitTime < 0 ? 0 : maxRemainingWaitTime );
221+ if (waitNanos < 0 )
222+ waitNanos = 0 ;
223+ }
224+ return waitNanos ;
225+ }
211226}
0 commit comments