From 80471b751a95edca4c5f88a88657c12081fcc895 Mon Sep 17 00:00:00 2001 From: Yury Yaroshevich Date: Wed, 13 Feb 2019 12:08:44 +0300 Subject: [PATCH] Offload I/O from ScheduledExecutorService thread in PaceMaker. (#173) * Offload I/O from ScheduledExecutorService thread in PaceMaker. --- src/main/java/org/ice4j/ice/Agent.java | 3 +- .../ice4j/ice/ConnectivityCheckClient.java | 164 +++++++----------- 2 files changed, 63 insertions(+), 104 deletions(-) diff --git a/src/main/java/org/ice4j/ice/Agent.java b/src/main/java/org/ice4j/ice/Agent.java index bfa31d9..02f83ea 100644 --- a/src/main/java/org/ice4j/ice/Agent.java +++ b/src/main/java/org/ice4j/ice/Agent.java @@ -379,7 +379,8 @@ public class Agent SecureRandom random = new SecureRandom(); connCheckServer = new ConnectivityCheckServer(this); - connCheckClient = new ConnectivityCheckClient(this, agentTasksScheduler); + connCheckClient = new ConnectivityCheckClient( + this, agentTasksScheduler, agentTasksExecutor); //add the FINGERPRINT attribute to all messages. System.setProperty(StackProperties.ALWAYS_SIGN, "true"); diff --git a/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java b/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java index b9f3ab3..a5d60da 100644 --- a/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java +++ b/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java @@ -18,9 +18,9 @@ package org.ice4j.ice; import java.net.*; +import java.time.*; import java.util.*; import java.util.concurrent.*; -import java.util.concurrent.atomic.*; import java.util.logging.*; import org.ice4j.*; @@ -28,6 +28,7 @@ import org.ice4j.attribute.*; import org.ice4j.message.*; import org.ice4j.socket.*; import org.ice4j.stack.*; +import org.ice4j.util.*; import org.ice4j.util.Logger; //Disambiguation /** @@ -57,10 +58,15 @@ class ConnectivityCheckClient 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; + /** + * An executor service to perform background tasks of the client + */ + private final ExecutorService executorService; + /** * The StunStack that we will use for connectivity checks. */ @@ -97,13 +103,17 @@ class ConnectivityCheckClient * @param parentAgent the Agent that is creating this instance. * @param scheduledExecutorService the ScheduledExecutorService * to execute clients tasks + * @param executorService the ExecutorService to execute + * background tasks of connectivity check client */ public ConnectivityCheckClient( Agent parentAgent, - ScheduledExecutorService scheduledExecutorService) + ScheduledExecutorService scheduledExecutorService, + ExecutorService executorService) { this.parentAgent = parentAgent; this.scheduledExecutorService = scheduledExecutorService; + this.executorService = executorService; logger = new Logger(classLogger, parentAgent.getLogger()); stunStack = this.parentAgent.getStunStack(); @@ -869,92 +879,14 @@ class ConnectivityCheckClient * A class to control periodically scheduled runnable that actually sends * 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 Runnable should continue running. - */ - private final AtomicBoolean cancelled = new AtomicBoolean(); - - /** - * Sends connectivity checks at the pace determined by the {@link - * Agent#calculateTa()} method and using either the trigger check queue - * or the regular check lists. - */ - private final Runnable connectivityChecker = new Runnable() - { - @Override - public void run() - { - if (cancelled.get()) - { - return; - } - - CandidatePair pairToCheck = checkList.popTriggeredCheck(); - - //if there are no triggered checks, go for an ordinary one. - if (pairToCheck == null) - { - pairToCheck = checkList.getNextOrdinaryPairToCheck(); - } - - if (pairToCheck != null) - { - /* - * Since we suspect that it is possible to - * startCheckForPair, processSuccessResponse and only - * then setStateInProgress, we'll synchronize. The - * synchronization root is the one of the - * CandidatePair#setState method. - */ - synchronized (pairToCheck) - { - TransactionID transactionID - = startCheckForPair(pairToCheck); - - if (transactionID == null) - { - logger.info( - "Pair failed: " - + pairToCheck.toShortString()); - pairToCheck.setStateFailed(); - } - else - { - pairToCheck.setStateInProgress(transactionID); - } - } - } - else - { - /* - * We are done sending checks for this list. We'll set - * its final state in either the processResponse(), - * processTimeout() or processFailure() method. - */ - logger.finest("will skip a check beat."); - checkList.fireEndOfOrdinaryChecks(); - } - - if (!cancelled.get()) - { - schedule(); - } - } - }; - /** * The {@link CheckList} that this PaceMaker 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 * ConnectivityCheckClient. @@ -964,37 +896,63 @@ class ConnectivityCheckClient */ public PaceMaker(CheckList checkList) { + super(scheduledExecutorService, executorService); this.checkList = checkList; } /** - * Cancel execution of current and further connectivity checks + * Sends connectivity checks at the pace determined by the {@link + * Agent#calculateTa()} method and using either the trigger check queue + * or the regular check lists. */ - void cancel() + @Override + protected void run() { - cancelled.set(true); + CandidatePair pairToCheck = checkList.popTriggeredCheck(); - final ScheduledFuture scheduledCheck = this.scheduledCheck; - if (scheduledCheck != null) + //if there are no triggered checks, go for an ordinary one. + if (pairToCheck == null) { - scheduledCheck.cancel(true); + pairToCheck = checkList.getNextOrdinaryPairToCheck(); } - } - /** - * Schedules execution of checks for check list - */ - void schedule() - { - if (cancelled.get()) + if (pairToCheck != null) { - return; + /* + * Since we suspect that it is possible to + * startCheckForPair, processSuccessResponse and only + * then setStateInProgress, we'll synchronize. The + * synchronization root is the one of the + * CandidatePair#setState method. + */ + synchronized (pairToCheck) + { + TransactionID transactionID + = startCheckForPair(pairToCheck); + + if (transactionID == null) + { + logger.info( + "Pair failed: " + + pairToCheck.toShortString()); + pairToCheck.setStateFailed(); + } + else + { + pairToCheck.setStateInProgress(transactionID); + } + } + } + else + { + /* + * We are done sending checks for this list. We'll set + * its final state in either the processResponse(), + * processTimeout() or processFailure() method. + */ + logger.finest("will skip a check beat."); + checkList.fireEndOfOrdinaryChecks(); } - scheduledCheck - = scheduledExecutorService.schedule( - connectivityChecker, - getNextWaitInterval(), - TimeUnit.MILLISECONDS); } /** @@ -1004,7 +962,7 @@ class ConnectivityCheckClient * @return the number milliseconds to wait before we send the next * check. */ - private long getNextWaitInterval() + protected Duration getDelayUntilNextRun() { int activeCheckLists = parentAgent.getActiveCheckListCount(); @@ -1015,7 +973,7 @@ class ConnectivityCheckClient activeCheckLists = 1; } - return parentAgent.calculateTa() * activeCheckLists; + return Duration.ofMillis(parentAgent.calculateTa() * activeCheckLists); } }