Offload I/O from ScheduledExecutorService thread in PaceMaker. (#173)

* Offload I/O from ScheduledExecutorService thread in PaceMaker.
This commit is contained in:
Yury Yaroshevich 2019-02-13 12:08:44 +03:00 committed by bgrozev
parent 5aec41fc02
commit 80471b751a
2 changed files with 63 additions and 104 deletions

View File

@ -379,7 +379,8 @@ public class Agent
SecureRandom random = new SecureRandom(); SecureRandom random = new SecureRandom();
connCheckServer = new ConnectivityCheckServer(this); connCheckServer = new ConnectivityCheckServer(this);
connCheckClient = new ConnectivityCheckClient(this, agentTasksScheduler); connCheckClient = new ConnectivityCheckClient(
this, agentTasksScheduler, agentTasksExecutor);
//add the FINGERPRINT attribute to all messages. //add the FINGERPRINT attribute to all messages.
System.setProperty(StackProperties.ALWAYS_SIGN, "true"); System.setProperty(StackProperties.ALWAYS_SIGN, "true");

View File

@ -18,9 +18,9 @@
package org.ice4j.ice; package org.ice4j.ice;
import java.net.*; import java.net.*;
import java.time.*;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.logging.*; import java.util.logging.*;
import org.ice4j.*; import org.ice4j.*;
@ -28,6 +28,7 @@ import org.ice4j.attribute.*;
import org.ice4j.message.*; import org.ice4j.message.*;
import org.ice4j.socket.*; import org.ice4j.socket.*;
import org.ice4j.stack.*; import org.ice4j.stack.*;
import org.ice4j.util.*;
import org.ice4j.util.Logger; //Disambiguation import org.ice4j.util.Logger; //Disambiguation
/** /**
@ -57,10 +58,15 @@ class ConnectivityCheckClient
private final Agent parentAgent; private final Agent parentAgent;
/** /**
* A scheduled executor service to perform scheduled task of the client * A scheduled executor service to perform periodic tasks of the client
*/ */
private final ScheduledExecutorService scheduledExecutorService; private final ScheduledExecutorService scheduledExecutorService;
/**
* An executor service to perform background tasks of the client
*/
private final ExecutorService executorService;
/** /**
* The <tt>StunStack</tt> that we will use for connectivity checks. * The <tt>StunStack</tt> that we will use for connectivity checks.
*/ */
@ -97,13 +103,17 @@ class ConnectivityCheckClient
* @param parentAgent the <tt>Agent</tt> that is creating this instance. * @param parentAgent the <tt>Agent</tt> that is creating this instance.
* @param scheduledExecutorService the <tt>ScheduledExecutorService</tt> * @param scheduledExecutorService the <tt>ScheduledExecutorService</tt>
* to execute clients tasks * to execute clients tasks
* @param executorService the <tt>ExecutorService</tt> to execute
* background tasks of connectivity check client
*/ */
public ConnectivityCheckClient( public ConnectivityCheckClient(
Agent parentAgent, Agent parentAgent,
ScheduledExecutorService scheduledExecutorService) ScheduledExecutorService scheduledExecutorService,
ExecutorService executorService)
{ {
this.parentAgent = parentAgent; this.parentAgent = parentAgent;
this.scheduledExecutorService = scheduledExecutorService; this.scheduledExecutorService = scheduledExecutorService;
this.executorService = executorService;
logger = new Logger(classLogger, parentAgent.getLogger()); logger = new Logger(classLogger, parentAgent.getLogger());
stunStack = this.parentAgent.getStunStack(); stunStack = this.parentAgent.getStunStack();
@ -869,28 +879,35 @@ class ConnectivityCheckClient
* A class to control periodically scheduled runnable that actually sends * A class to control periodically scheduled runnable that actually sends
* the checks for a particular check list in the pace defined in RFC 5245. * the checks for a particular check list in the pace defined in RFC 5245.
*/ */
private final class PaceMaker private final class PaceMaker extends PeriodicRunnable
{ {
/** /**
* Indicates whether this <tt>Runnable</tt> should continue running. * The {@link CheckList} that this <tt>PaceMaker</tt> will be running
* checks for.
*/ */
private final AtomicBoolean cancelled = new AtomicBoolean(); private final CheckList checkList;
/**
* Creates a new {@link PaceMaker} for this
* <tt>ConnectivityCheckClient</tt>.
*
* @param checkList the {@link CheckList} that we'll be sending checks
* for
*/
public PaceMaker(CheckList checkList)
{
super(scheduledExecutorService, executorService);
this.checkList = checkList;
}
/** /**
* Sends connectivity checks at the pace determined by the {@link * Sends connectivity checks at the pace determined by the {@link
* Agent#calculateTa()} method and using either the trigger check queue * Agent#calculateTa()} method and using either the trigger check queue
* or the regular check lists. * or the regular check lists.
*/ */
private final Runnable connectivityChecker = new Runnable()
{
@Override @Override
public void run() protected void run()
{ {
if (cancelled.get())
{
return;
}
CandidatePair pairToCheck = checkList.popTriggeredCheck(); CandidatePair pairToCheck = checkList.popTriggeredCheck();
//if there are no triggered checks, go for an ordinary one. //if there are no triggered checks, go for an ordinary one.
@ -936,65 +953,6 @@ class ConnectivityCheckClient
logger.finest("will skip a check beat."); logger.finest("will skip a check beat.");
checkList.fireEndOfOrdinaryChecks(); checkList.fireEndOfOrdinaryChecks();
} }
if (!cancelled.get())
{
schedule();
}
}
};
/**
* The {@link CheckList} that this <tt>PaceMaker</tt> will be running
* checks for.
*/
private final CheckList checkList;
/**
* Scheduled instance of {@link #connectivityChecker} runnable
*/
private ScheduledFuture<?> scheduledCheck;
/**
* Creates a new {@link PaceMaker} for this
* <tt>ConnectivityCheckClient</tt>.
*
* @param checkList the {@link CheckList} that we'll be sending checks
* for
*/
public PaceMaker(CheckList checkList)
{
this.checkList = checkList;
}
/**
* Cancel execution of current and further connectivity checks
*/
void cancel()
{
cancelled.set(true);
final ScheduledFuture<?> scheduledCheck = this.scheduledCheck;
if (scheduledCheck != null)
{
scheduledCheck.cancel(true);
}
}
/**
* Schedules execution of checks for check list
*/
void schedule()
{
if (cancelled.get())
{
return;
}
scheduledCheck
= scheduledExecutorService.schedule(
connectivityChecker,
getNextWaitInterval(),
TimeUnit.MILLISECONDS);
} }
/** /**
@ -1004,7 +962,7 @@ class ConnectivityCheckClient
* @return the number milliseconds to wait before we send the next * @return the number milliseconds to wait before we send the next
* check. * check.
*/ */
private long getNextWaitInterval() protected Duration getDelayUntilNextRun()
{ {
int activeCheckLists = parentAgent.getActiveCheckListCount(); int activeCheckLists = parentAgent.getActiveCheckListCount();
@ -1015,7 +973,7 @@ class ConnectivityCheckClient
activeCheckLists = 1; activeCheckLists = 1;
} }
return parentAgent.calculateTa() * activeCheckLists; return Duration.ofMillis(parentAgent.calculateTa() * activeCheckLists);
} }
} }