From 1e510e9a12c06f34202c81b42844517528dfc09f Mon Sep 17 00:00:00 2001 From: Oleg Date: Mon, 23 Oct 2023 10:55:11 +0400 Subject: [PATCH] Migrate to atomics for holding current state --- .../java/com/exactpro/th2/FixHandler.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/exactpro/th2/FixHandler.java b/src/main/java/com/exactpro/th2/FixHandler.java index 6344683..7242581 100644 --- a/src/main/java/com/exactpro/th2/FixHandler.java +++ b/src/main/java/com/exactpro/th2/FixHandler.java @@ -55,6 +55,7 @@ import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; @@ -68,6 +69,7 @@ import org.slf4j.LoggerFactory; import javax.annotation.concurrent.NotThreadSafe; +import javax.annotation.concurrent.ThreadSafe; import static com.exactpro.th2.conn.dirty.fix.FixByteBufUtilKt.findField; import static com.exactpro.th2.conn.dirty.fix.FixByteBufUtilKt.findLastField; @@ -1177,13 +1179,13 @@ private void debug(String message, Object... args) { } } - @NotThreadSafe + @ThreadSafe private static class TimeoutSendHandler { private final Consumer eventConsumer; private final long maxTimeout; private final long minTimeout; - private long currentTimeout; - private int attempts; + private final AtomicLong currentTimeout; + private final AtomicInteger attempts = new AtomicInteger(); TimeoutSendHandler(long maxTimeout, long minTimeout, Consumer eventConsumer) { if (maxTimeout < minTimeout) { @@ -1198,20 +1200,20 @@ private static class TimeoutSendHandler { this.maxTimeout = maxTimeout; this.minTimeout = minTimeout; this.eventConsumer = requireNonNull(eventConsumer, "event consumer"); - currentTimeout = maxTimeout; + currentTimeout = new AtomicLong(maxTimeout); } public void getWithTimeout(Future future) throws ExecutionException, InterruptedException, TimeoutException { try { - future.get(currentTimeout, TimeUnit.MILLISECONDS); - if (attempts > 0) { - generateEvent(attempts); + future.get(currentTimeout.get(), TimeUnit.MILLISECONDS); + int spentAttempts = attempts.getAndSet(0); + if (spentAttempts > 0) { + generateEvent(spentAttempts); } - currentTimeout = maxTimeout; - attempts = 0; + currentTimeout.set(maxTimeout); } catch (ExecutionException | InterruptedException | TimeoutException ex) { - currentTimeout = Math.max(minTimeout, currentTimeout / 2); - attempts += 1; + attempts.incrementAndGet(); + currentTimeout.updateAndGet(cur -> Math.max(minTimeout, cur / 2)); throw ex; } } @@ -1226,11 +1228,11 @@ private void generateEvent(int attempts) { } public long getCurrentTimeout() { - return currentTimeout; + return currentTimeout.get(); } public long getDeadline() { - return System.currentTimeMillis() + currentTimeout; + return System.currentTimeMillis() + currentTimeout.get(); } } } \ No newline at end of file