Offload I/O from ScheduledExecutorService thread in Retransmitter. (#172)

* Offload I/O from ScheduledExecutorService thread in Retransmitter.

* Use single thread executor for timer.

* Use single thread for timer

* Fixed typo
This commit is contained in:
Yury Yaroshevich 2019-02-12 22:50:59 +03:00 committed by bbaldino
parent f9a357ec17
commit 5aec41fc02
2 changed files with 38 additions and 99 deletions

View File

@ -121,7 +121,7 @@ public class Agent
* The ScheduledExecutorService to execute Agent's scheduled tasks * The ScheduledExecutorService to execute Agent's scheduled tasks
*/ */
private static final ScheduledExecutorService agentTasksScheduler private static final ScheduledExecutorService agentTasksScheduler
= ExecutorFactory.createCPUBoundScheduledExecutor( = ExecutorFactory.createSingleThreadScheduledExecutor(
"ice4j.Agent-timer-", 60, TimeUnit.SECONDS); "ice4j.Agent-timer-", 60, TimeUnit.SECONDS);
/** /**

View File

@ -18,6 +18,7 @@
package org.ice4j.stack; package org.ice4j.stack;
import java.io.*; import java.io.*;
import java.time.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.*; import java.util.concurrent.atomic.*;
import java.util.logging.*; import java.util.logging.*;
@ -74,12 +75,21 @@ public class StunClientTransaction
public static final int DEFAULT_ORIGINAL_WAIT_INTERVAL = 100; public static final int DEFAULT_ORIGINAL_WAIT_INTERVAL = 100;
/** /**
* The pool of <tt>Thread</tt>s which retransmit * The pool of <tt>Thread</tt>s which schedules retransmission of
* <tt>StunClientTransaction</tt>s. * <tt>StunClientTransaction</tt>s.
*/ */
private static final ScheduledExecutorService retransmissionThreadPool private static final ScheduledExecutorService retransmissionTimer
= ExecutorFactory.createCPUBoundScheduledExecutor( = ExecutorFactory.createSingleThreadScheduledExecutor(
"ice4j.StunClientTransaction-", 60, TimeUnit.SECONDS); "ice4j.StunClientTransaction-timer-", 60, TimeUnit.SECONDS);
/**
* The pool of <tt>Thread</tt>s which retransmits
* <tt>StunClientTransaction</tt>s.
*/
private static final ExecutorService retransmissionExecutor
= ExecutorFactory.createCachedThreadPool(
"ice4j.StunClientTransaction-executor-");
/** /**
* Maximum number of retransmissions. Once this number is reached and if no * Maximum number of retransmissions. Once this number is reached and if no
@ -415,7 +425,7 @@ public class StunClientTransaction
* If no response is received by 1.6 seconds after the last request has been * If no response is received by 1.6 seconds after the last request has been
* sent, we consider the transaction to have failed. * sent, we consider the transaction to have failed.
*/ */
private final class Retransmitter private final class Retransmitter extends PeriodicRunnable
{ {
/** /**
* Current number of retransmission attempts * Current number of retransmission attempts
@ -427,30 +437,28 @@ public class StunClientTransaction
*/ */
private int nextRetransmissionDelay = originalWaitInterval; private int nextRetransmissionDelay = originalWaitInterval;
/** protected Retransmitter()
* Currently scheduled retransmission task
*/
private ScheduledFuture<?> retransmissionFuture;
/**
* The scheduled runnable that perform retransmit attempt
*/
private final Runnable retransmissionAttempt = new Runnable()
{ {
@Override super(retransmissionTimer, retransmissionExecutor);
public void run() }
@Override
protected Duration getDelayUntilNextRun()
{
return Duration.ofMillis(nextRetransmissionDelay);
}
@Override
protected void run()
{
retransmissionCounter++;
int curWaitInterval = nextRetransmissionDelay;
nextRetransmissionDelay
= Math.min(maxWaitInterval, 2 * nextRetransmissionDelay);
if (retransmissionCounter <= maxRetransmissions)
{ {
if (cancelled.get())
{
return;
}
retransmissionCounter++;
int curWaitInterval = nextRetransmissionDelay;
nextRetransmissionDelay
= Math.min(maxWaitInterval, 2 * nextRetransmissionDelay);
try try
{ {
logger.fine( logger.fine(
@ -470,50 +478,9 @@ public class StunClientTransaction
"A client tran retransmission failed", "A client tran retransmission failed",
ex); ex);
} }
if(!cancelled.get())
{
reschedule();
}
} }
else
private void reschedule()
{ {
if (retransmissionCounter < maxRetransmissions)
{
retransmissionFuture = retransmissionThreadPool.schedule(
retransmissionAttempt,
nextRetransmissionDelay,
TimeUnit.MILLISECONDS);
}
else
{
// before stating that a transaction has timeout-ed we
// should first wait for a reception of the response
nextRetransmissionDelay =
Math.min(maxWaitInterval, 2* nextRetransmissionDelay);
retransmissionFuture = retransmissionThreadPool.schedule(
transactionTimedOut,
nextRetransmissionDelay,
TimeUnit.MILLISECONDS);
}
}
};
/**
* Scheduled runnable to time-out STUN transaction
*/
private final Runnable transactionTimedOut = new Runnable()
{
@Override
public void run()
{
if (cancelled.get())
{
return;
}
stackCallback.removeClientTransaction( stackCallback.removeClientTransaction(
StunClientTransaction.this); StunClientTransaction.this);
@ -521,36 +488,8 @@ public class StunClientTransaction
new StunTimeoutEvent( new StunTimeoutEvent(
stackCallback, stackCallback,
getRequest(), getLocalAddress(), getTransactionID())); getRequest(), getLocalAddress(), getTransactionID()));
}
};
/** nextRetransmissionDelay = -1;
* Schedules STUN transaction retransmission
*/
void schedule()
{
if (retransmissionFuture != null)
{
return;
}
retransmissionFuture = retransmissionThreadPool.schedule(
retransmissionAttempt,
nextRetransmissionDelay,
TimeUnit.MILLISECONDS);
}
/**
* Cancels the transaction. Once this method is called the transaction
* is considered terminated and will stop retransmissions.
*/
void cancel()
{
final ScheduledFuture<?> retransmissionFuture =
this.retransmissionFuture;
if (retransmissionFuture != null)
{
retransmissionFuture.cancel(true);
} }
} }
} }