From b1fb40e42d3fe456b20b453d8577f755add5fd1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Br=C3=A9gier?= Date: Tue, 18 Jun 2019 09:34:49 +0200 Subject: [PATCH] Change Scheduled to FixedRate in Traffic Counter (#9245) Motivation: Traffic shaping needs more accurate execution than scheduled one. So the use of FixedRate instead. Moreover the current implementation tends to create as many threads as channels use a ChannelTrafficShapingHandlern, which is unnecessary. Modifications: Change the executor.schedule to executor.scheduleAtFixedRate in the start and remove the reschedule call from run monitor thread since it will be restarted by the Fixed rate executor. Also fix a minor bug where restart was only doing start() without stop() before. Result: Threads are more stable in number of cached and precision of traffic shaping is enhanced. --- .../netty/handler/traffic/GlobalChannelTrafficCounter.java | 4 +--- .../main/java/io/netty/handler/traffic/TrafficCounter.java | 6 +++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/handler/src/main/java/io/netty/handler/traffic/GlobalChannelTrafficCounter.java b/handler/src/main/java/io/netty/handler/traffic/GlobalChannelTrafficCounter.java index 6af7de8d66..9741584a2b 100644 --- a/handler/src/main/java/io/netty/handler/traffic/GlobalChannelTrafficCounter.java +++ b/handler/src/main/java/io/netty/handler/traffic/GlobalChannelTrafficCounter.java @@ -78,8 +78,6 @@ public class GlobalChannelTrafficCounter extends TrafficCounter { perChannel.channelTrafficCounter.resetAccounting(newLastTime); } trafficShapingHandler1.doAccounting(counter); - counter.scheduledFuture = counter.executor.schedule(this, counter.checkInterval.get(), - TimeUnit.MILLISECONDS); } } @@ -97,7 +95,7 @@ public class GlobalChannelTrafficCounter extends TrafficCounter { monitorActive = true; monitor = new MixedTrafficMonitoringTask((GlobalChannelTrafficShapingHandler) trafficShapingHandler, this); scheduledFuture = - executor.schedule(monitor, localCheckInterval, TimeUnit.MILLISECONDS); + executor.scheduleAtFixedRate(monitor, 0, localCheckInterval, TimeUnit.MILLISECONDS); } } diff --git a/handler/src/main/java/io/netty/handler/traffic/TrafficCounter.java b/handler/src/main/java/io/netty/handler/traffic/TrafficCounter.java index 65fc77e00c..b63f550af1 100644 --- a/handler/src/main/java/io/netty/handler/traffic/TrafficCounter.java +++ b/handler/src/main/java/io/netty/handler/traffic/TrafficCounter.java @@ -174,7 +174,6 @@ public class TrafficCounter { if (trafficShapingHandler != null) { trafficShapingHandler.doAccounting(TrafficCounter.this); } - scheduledFuture = executor.schedule(this, checkInterval.get(), TimeUnit.MILLISECONDS); } } @@ -192,7 +191,7 @@ public class TrafficCounter { monitorActive = true; monitor = new TrafficMonitoringTask(); scheduledFuture = - executor.schedule(monitor, localCheckInterval, TimeUnit.MILLISECONDS); + executor.scheduleAtFixedRate(monitor, 0, localCheckInterval, TimeUnit.MILLISECONDS); } } @@ -317,7 +316,8 @@ public class TrafficCounter { // No more active monitoring lastTime.set(milliSecondFromNano()); } else { - // Start if necessary + // Restart + stop(); start(); } }