Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use monotonic clocks for ChannelAdaptor. #417

Open
wants to merge 1 commit into
base: tail
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions jpos/src/main/java/org/jpos/q2/iso/ChannelAdaptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.io.IOException;
import java.io.PrintStream;
import java.net.SocketTimeoutException;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;

/**
Expand All @@ -49,13 +51,13 @@ public class ChannelAdaptor
{
protected Space sp;
private ISOChannel channel;
private Duration lastTxn = null;
String in, out, ready, reconnect;
long delay;
boolean keepAlive = false;
boolean ignoreISOExceptions = false;
boolean writeOnly = false;
int rx, tx, connects;
long lastTxn = 0l;
long timeout = 0l;
boolean waitForWorkersOnStop;
private Thread receiver;
Expand Down Expand Up @@ -332,7 +334,7 @@ public void run () {
continue;
ISOMsg m = channel.receive ();
rx++;
lastTxn = System.currentTimeMillis();
lastTxn = Duration.ofNanos(System.nanoTime());
if (timeout > 0)
sp.out (out, m, timeout);
else
Expand Down Expand Up @@ -430,20 +432,23 @@ public synchronized void setSocketFactory (String sFac) {
setModified(true);
}

void updateLast() {
lastTxn = Duration.ofNanos(System.nanoTime());
}
public void resetCounters () {
rx = tx = connects = 0;
lastTxn = 0l;
lastTxn = null;
}
public String getCountersAsString () {
StringBuffer sb = new StringBuffer();
append (sb, "tx=", tx);
append (sb, ", rx=", rx);
append (sb, ", connects=", connects);
sb.append (", last=");
sb.append(lastTxn);
if (lastTxn > 0) {
sb.append(getLastTxnTimestampInMillis());
if (lastTxn != null) {
sb.append (", idle=");
sb.append(System.currentTimeMillis() - lastTxn);
sb.append(Duration.ofNanos(System.nanoTime()).minus(lastTxn).toMillis());
sb.append ("ms");
}
return sb.toString();
Expand All @@ -458,10 +463,12 @@ public int getConnectsCounter () {
return connects;
}
public long getLastTxnTimestampInMillis() {
return lastTxn;
if (lastTxn == null)
return 0L;
return Instant.now().minus(Duration.ofNanos(System.nanoTime()).minus(lastTxn)).toEpochMilli();
}
public long getIdleTimeInMillis() {
return lastTxn > 0L ? System.currentTimeMillis() - lastTxn : -1L;
return lastTxn != null ? Duration.ofNanos(System.nanoTime()).minus(lastTxn).toMillis() : -1L;
}
public String getSocketFactory() {
return getProperty(getProperties ("channel"), "socketFactory");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void run () {
}
ISOMsg m = channel.receive ();
rx++;
lastTxn = System.currentTimeMillis();
updateLast();
if (timeout > 0)
sp.out (out, m, timeout);
else
Expand Down