Skip to content

Commit ef4ce5c

Browse files
committed
Rename Execution.getWaitTime to getDelay
1 parent e8ac452 commit ef4ce5c

File tree

8 files changed

+96
-96
lines changed

8 files changed

+96
-96
lines changed

src/main/java/net/jodah/failsafe/AbstractExecution.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public abstract class AbstractExecution<R> extends ExecutionContext<R> {
3939
volatile boolean attemptStarted;
4040
// Whether the execution attempt has been recorded
4141
volatile boolean attemptRecorded;
42-
// The wait time in nanoseconds
43-
volatile long waitNanos;
42+
// The delay time in nanoseconds
43+
volatile long delayNanos;
4444
// Whether the entire Failsafe execution has been completed
4545
volatile boolean completed;
4646

@@ -125,17 +125,17 @@ synchronized ExecutionResult postExecute(ExecutionResult result) {
125125
allComplete = allComplete && result.isComplete();
126126
}
127127

128-
waitNanos = result.getWaitNanos();
128+
delayNanos = result.getDelay();
129129
completed = allComplete;
130130
return result;
131131
}
132132

133133
/**
134-
* Returns the time to wait before the next execution attempt. Returns {@code 0} if an execution has not yet
134+
* Returns the time to delay before the next execution attempt. Returns {@code 0} if an execution has not yet
135135
* occurred.
136136
*/
137-
public Duration getWaitTime() {
138-
return Duration.ofNanos(waitNanos);
137+
public Duration getDelay() {
138+
return Duration.ofNanos(delayNanos);
139139
}
140140

141141
/**

src/main/java/net/jodah/failsafe/AsyncExecution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void record(R result, Throwable failure) {
9898
// Guard against race with a timeout expiring
9999
synchronized (future) {
100100
if (!attemptRecorded) {
101-
ExecutionResult er = new ExecutionResult(result, failure).withWaitNanos(waitNanos);
101+
ExecutionResult er = new ExecutionResult(result, failure).withDelay(delayNanos);
102102
record(er);
103103
}
104104

src/main/java/net/jodah/failsafe/ExecutionResult.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public final class ExecutionResult {
3838
/** Whether the result represents a non result rather than a {@code null} result */
3939
private final boolean nonResult;
4040
/** The amount of time to wait prior to the next execution, according to the policy */
41-
private final long waitNanos;
41+
private final long delayNanos;
4242
/** Whether a policy has completed handling of the execution */
4343
private final boolean complete;
4444
/** Whether a policy determined the execution to be a success */
@@ -54,12 +54,12 @@ public ExecutionResult(Object result, Throwable failure) {
5454
this(result, failure, false, 0, true, failure == null, failure == null);
5555
}
5656

57-
private ExecutionResult(Object result, Throwable failure, boolean nonResult, long waitNanos, boolean complete,
57+
private ExecutionResult(Object result, Throwable failure, boolean nonResult, long delayNanos, boolean complete,
5858
boolean success, Boolean successAll) {
5959
this.nonResult = nonResult;
6060
this.result = result;
6161
this.failure = failure;
62-
this.waitNanos = waitNanos;
62+
this.delayNanos = delayNanos;
6363
this.complete = complete;
6464
this.success = success;
6565
this.successAll = successAll;
@@ -84,8 +84,8 @@ public Throwable getFailure() {
8484
return failure;
8585
}
8686

87-
public long getWaitNanos() {
88-
return waitNanos;
87+
public long getDelay() {
88+
return delayNanos;
8989
}
9090

9191
public boolean isComplete() {
@@ -107,7 +107,7 @@ public boolean isSuccess() {
107107
ExecutionResult withNonResult() {
108108
return success && this.result == null && nonResult ?
109109
this :
110-
new ExecutionResult(null, null, true, waitNanos, true, true, successAll);
110+
new ExecutionResult(null, null, true, delayNanos, true, true, successAll);
111111
}
112112

113113
/**
@@ -119,7 +119,7 @@ public ExecutionResult withResult(Object result) {
119119
boolean unchangedNotNull = this.result != null && this.result.equals(result);
120120
return success && (unchangedNull || unchangedNotNull) ?
121121
this :
122-
new ExecutionResult(result, null, nonResult, waitNanos, true, true, successAll);
122+
new ExecutionResult(result, null, nonResult, delayNanos, true, true, successAll);
123123
}
124124

125125
/**
@@ -128,14 +128,14 @@ public ExecutionResult withResult(Object result) {
128128
ExecutionResult withNotComplete() {
129129
return !this.complete ?
130130
this :
131-
new ExecutionResult(result, failure, nonResult, waitNanos, false, success, successAll);
131+
new ExecutionResult(result, failure, nonResult, delayNanos, false, success, successAll);
132132
}
133133

134134
/**
135135
* Returns a copy of the ExecutionResult with success value of {code false}.
136136
*/
137137
ExecutionResult withFailure() {
138-
return !this.success ? this : new ExecutionResult(result, failure, nonResult, waitNanos, complete, false, false);
138+
return !this.success ? this : new ExecutionResult(result, failure, nonResult, delayNanos, complete, false, false);
139139
}
140140

141141
/**
@@ -144,25 +144,25 @@ ExecutionResult withFailure() {
144144
ExecutionResult withSuccess() {
145145
return this.complete && this.success ?
146146
this :
147-
new ExecutionResult(result, failure, nonResult, waitNanos, true, true, successAll);
147+
new ExecutionResult(result, failure, nonResult, delayNanos, true, true, successAll);
148148
}
149149

150150
/**
151-
* Returns a copy of the ExecutionResult with the {@code waitNanos} value.
151+
* Returns a copy of the ExecutionResult with the {@code delayNanos} value.
152152
*/
153-
public ExecutionResult withWaitNanos(long waitNanos) {
154-
return this.waitNanos == waitNanos ?
153+
public ExecutionResult withDelay(long delayNanos) {
154+
return this.delayNanos == delayNanos ?
155155
this :
156-
new ExecutionResult(result, failure, nonResult, waitNanos, complete, success, successAll);
156+
new ExecutionResult(result, failure, nonResult, delayNanos, complete, success, successAll);
157157
}
158158

159159
/**
160-
* Returns a copy of the ExecutionResult with the {@code waitNanos}, {@code complete} and {@code success} values.
160+
* Returns a copy of the ExecutionResult with the {@code delayNanos}, {@code complete} and {@code success} values.
161161
*/
162-
public ExecutionResult with(long waitNanos, boolean complete, boolean success) {
163-
return this.waitNanos == waitNanos && this.complete == complete && this.success == success ?
162+
public ExecutionResult with(long delayNanos, boolean complete, boolean success) {
163+
return this.delayNanos == delayNanos && this.complete == complete && this.success == success ?
164164
this :
165-
new ExecutionResult(result, failure, nonResult, waitNanos, complete, success,
165+
new ExecutionResult(result, failure, nonResult, delayNanos, complete, success,
166166
successAll == null ? success : success && successAll);
167167
}
168168

@@ -172,7 +172,7 @@ boolean getSuccessAll() {
172172

173173
@Override
174174
public String toString() {
175-
return "[" + "result=" + result + ", failure=" + failure + ", nonResult=" + nonResult + ", waitNanos=" + waitNanos
175+
return "[" + "result=" + result + ", failure=" + failure + ", nonResult=" + nonResult + ", delayNanos=" + delayNanos
176176
+ ", complete=" + complete + ", success=" + success + ", successAll=" + successAll + ']';
177177
}
178178

src/main/java/net/jodah/failsafe/RetryPolicyExecutor.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class RetryPolicyExecutor<R> extends PolicyExecutor<R, RetryPolicy<R>> {
3939
// Mutable state
4040
private volatile int failedAttempts;
4141
private volatile boolean retriesExceeded;
42-
/** The fixed, backoff, random or computed delay time in nanoseconds. */
43-
private volatile long delayNanos;
42+
/** The last fixed, backoff, random or computed delay time in nanoseconds. */
43+
private volatile long lastDelayNanos;
4444

4545
// Listeners
4646
private final EventListener abortListener;
@@ -80,7 +80,7 @@ protected Function<Execution<R>, ExecutionResult> apply(Function<Execution<R>, E
8080

8181
// Guard against race with Timeout so that sleep can either be skipped or interrupted
8282
execution.interruptState.canInterrupt = true;
83-
Thread.sleep(TimeUnit.NANOSECONDS.toMillis(result.getWaitNanos()));
83+
Thread.sleep(TimeUnit.NANOSECONDS.toMillis(result.getDelay()));
8484
} catch (InterruptedException e) {
8585
// Set interrupt flag if interrupt was not intended
8686
if (!execution.interruptState.interrupted)
@@ -155,7 +155,7 @@ public Object handleAsync(AsyncExecution<R> execution,
155155
future.inject(retryExecution);
156156
Callable<Object> retryFn = () -> handleAsync(retryExecution, innerFn, scheduler, future, promise,
157157
previousResultRef);
158-
Future<?> scheduledRetry = scheduler.schedule(retryFn, postResult.getWaitNanos(),
158+
Future<?> scheduledRetry = scheduler.schedule(retryFn, postResult.getDelay(),
159159
TimeUnit.NANOSECONDS);
160160
retryExecution.innerFuture = scheduledRetry;
161161

@@ -202,22 +202,22 @@ protected ExecutionResult onFailure(AbstractExecution<R> execution, ExecutionRes
202202
failedAttemptListener.handle(result, execution);
203203

204204
failedAttempts++;
205-
long waitNanos = delayNanos;
205+
long delayNanos = lastDelayNanos;
206206

207207
// Determine the computed delay
208208
Duration computedDelay = policy.computeDelay(execution);
209209
if (computedDelay != null) {
210-
waitNanos = computedDelay.toNanos();
210+
delayNanos = computedDelay.toNanos();
211211
} else {
212212
// Determine the fixed or random delay
213-
waitNanos = getFixedOrRandomDelayNanos(waitNanos);
214-
waitNanos = adjustForBackoff(execution, waitNanos);
215-
delayNanos = waitNanos;
213+
delayNanos = getFixedOrRandomDelayNanos(delayNanos);
214+
delayNanos = adjustForBackoff(execution, delayNanos);
215+
lastDelayNanos = delayNanos;
216216
}
217217

218-
waitNanos = adjustForJitter(waitNanos);
218+
delayNanos = adjustForJitter(delayNanos);
219219
long elapsedNanos = execution.getElapsedTime().toNanos();
220-
waitNanos = adjustForMaxDuration(waitNanos, elapsedNanos);
220+
delayNanos = adjustForMaxDuration(delayNanos, elapsedNanos);
221221

222222
// Calculate result
223223
boolean maxRetriesExceeded = policy.getMaxRetries() != -1 && failedAttempts > policy.getMaxRetries();
@@ -234,7 +234,7 @@ protected ExecutionResult onFailure(AbstractExecution<R> execution, ExecutionRes
234234
else if (retriesExceededListener != null && !success && retriesExceeded)
235235
retriesExceededListener.handle(result, execution);
236236

237-
return result.with(waitNanos, completed, success);
237+
return result.with(delayNanos, completed, success);
238238
}
239239

240240
/**
@@ -247,39 +247,39 @@ protected CompletableFuture<ExecutionResult> onFailureAsync(AbstractExecution<R>
247247
return super.onFailureAsync(execution, result.withNotComplete(), scheduler, future);
248248
}
249249

250-
private long getFixedOrRandomDelayNanos(long waitNanos) {
250+
private long getFixedOrRandomDelayNanos(long delayNanos) {
251251
Duration delay = policy.getDelay();
252252
Duration delayMin = policy.getDelayMin();
253253
Duration delayMax = policy.getDelayMax();
254254

255-
if (waitNanos == 0 && delay != null && !delay.equals(Duration.ZERO))
256-
waitNanos = delay.toNanos();
255+
if (delayNanos == 0 && delay != null && !delay.equals(Duration.ZERO))
256+
delayNanos = delay.toNanos();
257257
else if (delayMin != null && delayMax != null)
258-
waitNanos = randomDelayInRange(delayMin.toNanos(), delayMax.toNanos(), Math.random());
259-
return waitNanos;
258+
delayNanos = randomDelayInRange(delayMin.toNanos(), delayMax.toNanos(), Math.random());
259+
return delayNanos;
260260
}
261261

262-
private long adjustForBackoff(AbstractExecution<R> execution, long waitNanos) {
262+
private long adjustForBackoff(AbstractExecution<R> execution, long delayNanos) {
263263
if (execution.getAttemptCount() != 1 && policy.getMaxDelay() != null)
264-
waitNanos = (long) Math.min(waitNanos * policy.getDelayFactor(), policy.getMaxDelay().toNanos());
265-
return waitNanos;
264+
delayNanos = (long) Math.min(delayNanos * policy.getDelayFactor(), policy.getMaxDelay().toNanos());
265+
return delayNanos;
266266
}
267267

268-
private long adjustForJitter(long waitNanos) {
268+
private long adjustForJitter(long delayNanos) {
269269
if (policy.getJitter() != null)
270-
waitNanos = randomDelay(waitNanos, policy.getJitter().toNanos(), Math.random());
270+
delayNanos = randomDelay(delayNanos, policy.getJitter().toNanos(), Math.random());
271271
else if (policy.getJitterFactor() > 0.0)
272-
waitNanos = randomDelay(waitNanos, policy.getJitterFactor(), Math.random());
273-
return waitNanos;
272+
delayNanos = randomDelay(delayNanos, policy.getJitterFactor(), Math.random());
273+
return delayNanos;
274274
}
275275

276-
private long adjustForMaxDuration(long waitNanos, long elapsedNanos) {
276+
private long adjustForMaxDuration(long delayNanos, long elapsedNanos) {
277277
if (policy.getMaxDuration() != null) {
278-
long maxRemainingWaitTime = policy.getMaxDuration().toNanos() - elapsedNanos;
279-
waitNanos = Math.min(waitNanos, maxRemainingWaitTime < 0 ? 0 : maxRemainingWaitTime);
280-
if (waitNanos < 0)
281-
waitNanos = 0;
278+
long maxRemainingDelay = policy.getMaxDuration().toNanos() - elapsedNanos;
279+
delayNanos = Math.min(delayNanos, maxRemainingDelay < 0 ? 0 : maxRemainingDelay);
280+
if (delayNanos < 0)
281+
delayNanos = 0;
282282
}
283-
return waitNanos;
283+
return delayNanos;
284284
}
285285
}

src/main/java/net/jodah/failsafe/internal/EventListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static <R> EventListener ofScheduled(CheckedConsumer<? extends ExecutionSchedule
5858
try {
5959
((CheckedConsumer<ExecutionScheduledEvent<R>>) handler).accept(
6060
new ExecutionScheduledEvent<>(result.getResult(), result.getFailure(),
61-
Duration.ofNanos(result.getWaitNanos()), context));
61+
Duration.ofNanos(result.getDelay()), context));
6262
} catch (Throwable ignore) {
6363
}
6464
};

0 commit comments

Comments
 (0)