Skip to content
Open
Changes from 3 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
33 changes: 27 additions & 6 deletions src/main/java/hudson/remoting/PingThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
* @since 1.170
*/
public abstract class PingThread extends Thread {
private final static int DEFAULT_MAX_TIMEOUTS = 3;

private final Channel channel;

/**
Expand All @@ -61,11 +63,14 @@ public abstract class PingThread extends Thread {
*/
private final long interval;

public PingThread(Channel channel, long timeout, long interval) {
private final int maxTimeouts;

public PingThread(Channel channel, long timeout, long interval, int maxTimeouts) {
super("Ping thread for channel "+channel);
this.channel = channel;
this.timeout = timeout;
this.interval = interval;
this.maxTimeouts = maxTimeouts;
setDaemon(true);
setUncaughtExceptionHandler((t, e) -> {
LOGGER.log(Level.SEVERE, "Uncaught exception in PingThread " + t, e);
Expand All @@ -74,7 +79,7 @@ public PingThread(Channel channel, long timeout, long interval) {
}

public PingThread(Channel channel, long interval) {
this(channel, TimeUnit.MINUTES.toMillis(4), interval);
this(channel, TimeUnit.MINUTES.toMillis(4), interval, DEFAULT_MAX_TIMEOUTS);
}

public PingThread(Channel channel) {
Expand All @@ -83,10 +88,26 @@ public PingThread(Channel channel) {

public void run() {
try {
int timeouts = 0;

while(true) {
long nextCheck = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(interval);

ping();
try {
ping();
timeouts = 0;
} catch (TimeoutException e) {
if (++timeouts >= this.maxTimeouts) {
onDead(e);
} else {
LOGGER.log( Level.WARNING,
"timeout {0}/{1} pinging {2}",
new Object[]
{timeouts, this.maxTimeouts, channel.getName()} );
}
} catch (ExecutionException e) {
onDead(e);
}

// wait until the next check
long diff;
Expand Down Expand Up @@ -121,16 +142,16 @@ private void ping() throws IOException, InterruptedException {
} catch (ExecutionException e) {
if (e.getCause() instanceof RequestAbortedException)
return; // connection has shut down orderly.
onDead(e);
return;
throw e;
} catch (TimeoutException e) {
// get method waits "at most the amount specified in the timeout",
// so let's make sure that it really waited enough
}
remaining = end - System.nanoTime();
} while(remaining>0);

onDead(new TimeoutException("Ping started at "+start+" hasn't completed by "+System.currentTimeMillis()));//.initCause(e)
throw new TimeoutException( "Ping started at " + start +
" hasn't completed by " + System.currentTimeMillis() );
}

/**
Expand Down