Skip to content

Commit

Permalink
Use monotonic clocks for QMUX.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshilliard committed May 27, 2021
1 parent 6c05df8 commit 99b221c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
20 changes: 11 additions & 9 deletions jpos/src/main/java/org/jpos/q2/iso/QMUX.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

import java.io.IOException;
import java.io.PrintStream;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -58,7 +60,7 @@ public class QMUX

List<ISORequestListener> listeners;
private volatile int rx, tx, rxExpired, txExpired, rxPending, rxUnhandled, rxForwarded;
private volatile long lastTxn = 0L;
private volatile Duration lastTxn = Duration.ZERO;
private boolean listenerRegistered;
public QMUX () {
super ();
Expand Down Expand Up @@ -161,7 +163,7 @@ public ISOMsg request (ISOMsg m, long timeout) throws ISOException {
if (resp != null)
{
rx++;
lastTxn = System.currentTimeMillis();
lastTxn = Duration.ofNanos(System.nanoTime());
}else {
rxExpired++;
if (m.getDirection() != ISOMsg.OUTGOING)
Expand Down Expand Up @@ -351,7 +353,7 @@ public boolean removeISORequestListener(ISORequestListener l) {
}
public synchronized void resetCounters() {
rx = tx = rxExpired = txExpired = rxPending = rxUnhandled = rxForwarded = 0;
lastTxn = 0l;
lastTxn = Duration.ZERO;
}
public String getCountersAsString () {
StringBuffer sb = new StringBuffer();
Expand All @@ -366,10 +368,10 @@ public String getCountersAsString () {
sb.append (", connected=");
sb.append (Boolean.toString(isConnected()));
sb.append (", last=");
sb.append (lastTxn);
if (lastTxn > 0) {
sb.append (getLastTxnTimestampInMillis());
if (lastTxn != Duration.ZERO) {
sb.append (", idle=");
sb.append(System.currentTimeMillis() - lastTxn);
sb.append(Duration.ofNanos(System.nanoTime()).minus(lastTxn).toMillis());
sb.append ("ms");
}
return sb.toString();
Expand Down Expand Up @@ -413,10 +415,10 @@ public int getRXForwarded() {
}

public long getLastTxnTimestampInMillis() {
return lastTxn;
return Instant.now().minus(Duration.ofNanos(System.nanoTime()).minus(lastTxn)).toEpochMilli();
}
public long getIdleTimeInMillis() {
return lastTxn > 0L ? System.currentTimeMillis() - lastTxn : -1L;
return lastTxn != Duration.ZERO ? Duration.ofNanos(System.nanoTime()).minus(lastTxn).toMillis() : -1L;
}

protected void processUnhandled (ISOMsg m) {
Expand Down Expand Up @@ -529,7 +531,7 @@ public void responseReceived (ISOMsg response) {
synchronized (QMUX.this) {
rx++;
rxPending--;
lastTxn = System.currentTimeMillis();
lastTxn = Duration.ofNanos(System.nanoTime());
}
long elapsed = chrono.elapsed();
metrics.record("all", elapsed);
Expand Down
1 change: 1 addition & 0 deletions jpos/src/test/java/org/jpos/q2/iso/QMUXTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static void setUp(@TempDir Path deployDir) throws IOException {
});
q2 = new Q2(deployDir.toString());
q2.start();
q2.ready(5000L);
mux = NameRegistrar.get("mux.mux", 2000L);
assertNotNull(mux);
receivedHandback = null;
Expand Down

0 comments on commit 99b221c

Please sign in to comment.