Skip to content

Commit 61fa1c6

Browse files
committed
Make RetryContext available to Callable of Promises.poll method
1 parent 2628dd8 commit 61fa1c6

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/main/java/net/tascalate/concurrent/Promises.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,13 @@ private static <T> void pollOnce(Callable<Optional<? extends T>> codeBlock,
566566
Runnable doCall = () -> {
567567
long startTime = System.currentTimeMillis();
568568
try {
569-
Optional<? extends T> result = codeBlock.call();
569+
Optional<? extends T> result;
570+
ctx.enter();
571+
try {
572+
result = codeBlock.call();
573+
} finally {
574+
ctx.exit();
575+
}
570576
if (result.isPresent()) {
571577
resultPromise.onSuccess(result.get());
572578
} else {

src/main/java/net/tascalate/concurrent/RetryContext.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,21 @@ public RetryContext asPrevRetry() {
6969
}
7070
return new RetryContext(policy, retry - 1, lastCallDuration, lastThrowable);
7171
}
72+
73+
public static RetryContext current() {
74+
return CURRENT_CONTEXT.get();
75+
}
76+
77+
void enter() {
78+
CURRENT_CONTEXT.set(this);
79+
}
80+
81+
void exit() {
82+
if (CURRENT_CONTEXT.get() == this) {
83+
CURRENT_CONTEXT.remove();
84+
}
85+
}
86+
87+
private static final ThreadLocal<RetryContext> CURRENT_CONTEXT = new ThreadLocal<>();
7288

7389
}

src/main/java/net/tascalate/concurrent/RetryException.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Copyright 2015-2017 Valery Silaev (http://vsilaev.com)
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+
*/
116
package net.tascalate.concurrent;
217

318
public class RetryException extends Exception {

0 commit comments

Comments
 (0)