Skip to content

Commit 0169715

Browse files
committed
Change ExecutionContext.getStartTime to return Instant.
Fixes #347
1 parent eed3ed6 commit 0169715

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 3.3.0
2+
3+
### API Changes
4+
5+
- `ExecutionContext.getStartTime` now returns an `Instant` rather than a `Duration` object.
6+
17
# 3.2.4
28

39
### Improvements

core/src/main/java/dev/failsafe/ExecutionContext.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import dev.failsafe.function.CheckedRunnable;
1919

2020
import java.time.Duration;
21+
import java.time.Instant;
2122
import java.util.concurrent.CompletableFuture;
2223

2324
/**
@@ -78,9 +79,9 @@ public interface ExecutionContext<R> {
7879
R getLastResult(R defaultValue);
7980

8081
/**
81-
* Returns the time that the initial execution started.
82+
* Returns the time that the initial execution started, else {code null} if an execution has not started yet.
8283
*/
83-
Duration getStartTime();
84+
Instant getStartTime();
8485

8586
/**
8687
* Returns whether the execution has been cancelled. In this case the implementor should attempt to stop execution.

core/src/main/java/dev/failsafe/ExecutionImpl.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import dev.failsafe.spi.PolicyExecutor;
2323

2424
import java.time.Duration;
25+
import java.time.Instant;
2526
import java.util.ArrayList;
2627
import java.util.List;
2728
import java.util.ListIterator;
@@ -39,7 +40,7 @@ class ExecutionImpl<R> implements ExecutionInternal<R> {
3940

4041
final List<PolicyExecutor<R>> policyExecutors;
4142
// When the first execution attempt was started
42-
private volatile Duration startTime;
43+
private volatile Instant startTime;
4344
// Number of execution attempts
4445
private final AtomicInteger attempts;
4546
// Number of completed executions
@@ -54,7 +55,7 @@ class ExecutionImpl<R> implements ExecutionInternal<R> {
5455
// The result of the current execution attempt;
5556
volatile ExecutionResult<R> result;
5657
// When the most recent execution attempt was started
57-
volatile Duration attemptStartTime;
58+
private volatile Instant attemptStartTime;
5859
// The index of a PolicyExecutor that cancelled the execution. Integer.MIN_VALUE represents non-cancelled.
5960
volatile int cancelledIndex = Integer.MIN_VALUE;
6061
// The user-provided callback to be called when an execution is cancelled
@@ -71,8 +72,6 @@ class ExecutionImpl<R> implements ExecutionInternal<R> {
7172
*/
7273
ExecutionImpl(List<? extends Policy<R>> policies) {
7374
policyExecutors = new ArrayList<>(policies.size());
74-
startTime = Duration.ZERO;
75-
attemptStartTime = Duration.ZERO;
7675
attempts = new AtomicInteger();
7776
executions = new AtomicInteger();
7877
latest = new AtomicReference<>(this);
@@ -122,8 +121,8 @@ public void onCancel(CheckedRunnable cancelCallback) {
122121
@Override
123122
public synchronized void preExecute() {
124123
if (!preExecuted) {
125-
attemptStartTime = Duration.ofNanos(System.nanoTime());
126-
if (startTime == Duration.ZERO)
124+
attemptStartTime = Instant.now();
125+
if (startTime == null)
127126
startTime = attemptStartTime;
128127
preExecuted = true;
129128
}
@@ -220,12 +219,12 @@ public ExecutionInternal<R> getLatest() {
220219

221220
@Override
222221
public Duration getElapsedTime() {
223-
return Duration.ofNanos(System.nanoTime() - startTime.toNanos());
222+
return startTime == null ? Duration.ZERO : Duration.between(startTime, Instant.now());
224223
}
225224

226225
@Override
227226
public Duration getElapsedAttemptTime() {
228-
return Duration.ofNanos(System.nanoTime() - attemptStartTime.toNanos());
227+
return attemptStartTime == null ? Duration.ZERO : Duration.between(attemptStartTime, Instant.now());
229228
}
230229

231230
@Override
@@ -264,7 +263,7 @@ public R getLastResult(R defaultValue) {
264263
}
265264

266265
@Override
267-
public Duration getStartTime() {
266+
public Instant getStartTime() {
268267
return startTime;
269268
}
270269

core/src/main/java/dev/failsafe/Functions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
final class Functions {
3232
/**
33-
* Returns a Supplier that for synchronous executions, that pre-executes the {@code execution}, applies the {@code
33+
* Returns a Supplier for synchronous executions that pre-executes the {@code execution}, applies the {@code
3434
* supplier}, records the result and returns the result. This implementation also handles Thread interrupts.
3535
*
3636
* @param <R> result type
@@ -90,7 +90,7 @@ static <R> Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult
9090
}
9191

9292
/**
93-
* Returns a Function for asynchronous executions, that pre-executes the {@code execution}, runs the {@code runnable},
93+
* Returns a Function for asynchronous executions that pre-executes the {@code execution}, runs the {@code runnable},
9494
* and attempts to complete the {@code execution} if a failure occurs. Locks to ensure the resulting supplier cannot
9595
* be applied multiple times concurrently.
9696
*
@@ -117,7 +117,7 @@ public synchronized CompletableFuture<ExecutionResult<R>> apply(AsyncExecutionIn
117117
}
118118

119119
/**
120-
* Returns a Function that for asynchronous executions, that pre-executes the {@code execution}, applies the {@code
120+
* Returns a Function that for asynchronous executions that pre-executes the {@code execution}, applies the {@code
121121
* supplier}, records the result and returns a promise containing the result.
122122
*
123123
* @param <R> result type

core/src/main/java/dev/failsafe/event/ExecutionEvent.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import dev.failsafe.ExecutionContext;
2020

2121
import java.time.Duration;
22+
import java.time.Instant;
2223

2324
/**
2425
* Encapsulates information about a Failsafe execution.
@@ -58,9 +59,9 @@ public int getExecutionCount() {
5859
}
5960

6061
/**
61-
* Returns the time that the initial execution started.
62+
* Returns the time that the initial execution started, else {code null} if an execution has not started yet.
6263
*/
63-
public Duration getStartTime() {
64+
public Instant getStartTime() {
6465
return context.getStartTime();
6566
}
6667

0 commit comments

Comments
 (0)