1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.traffic;
17
18 import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler.PerChannel;
19
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.TimeUnit;
22
23
24
25
26
27
28
29 public class GlobalChannelTrafficCounter extends TrafficCounter {
30
31
32
33
34
35
36 public GlobalChannelTrafficCounter(GlobalChannelTrafficShapingHandler trafficShapingHandler,
37 ScheduledExecutorService executor, String name, long checkInterval) {
38 super(trafficShapingHandler, executor, name, checkInterval);
39 if (executor == null) {
40 throw new IllegalArgumentException("Executor must not be null");
41 }
42 }
43
44
45
46
47
48 private static class MixedTrafficMonitoringTask implements Runnable {
49
50
51
52 private final GlobalChannelTrafficShapingHandler trafficShapingHandler1;
53
54
55
56
57 private final TrafficCounter counter;
58
59
60
61
62
63 MixedTrafficMonitoringTask(
64 GlobalChannelTrafficShapingHandler trafficShapingHandler,
65 TrafficCounter counter) {
66 trafficShapingHandler1 = trafficShapingHandler;
67 this.counter = counter;
68 }
69
70 @Override
71 public void run() {
72 if (!counter.monitorActive) {
73 return;
74 }
75 long newLastTime = milliSecondFromNano();
76 counter.resetAccounting(newLastTime);
77 for (PerChannel perChannel : trafficShapingHandler1.channelQueues.values()) {
78 perChannel.channelTrafficCounter.resetAccounting(newLastTime);
79 }
80 trafficShapingHandler1.doAccounting(counter);
81 counter.scheduledFuture = counter.executor.schedule(this, counter.checkInterval.get(),
82 TimeUnit.MILLISECONDS);
83 }
84 }
85
86
87
88
89 @Override
90 public synchronized void start() {
91 if (monitorActive) {
92 return;
93 }
94 lastTime.set(milliSecondFromNano());
95 long localCheckInterval = checkInterval.get();
96 if (localCheckInterval > 0) {
97 monitorActive = true;
98 monitor = new MixedTrafficMonitoringTask((GlobalChannelTrafficShapingHandler) trafficShapingHandler, this);
99 scheduledFuture =
100 executor.schedule(monitor, localCheckInterval, TimeUnit.MILLISECONDS);
101 }
102 }
103
104
105
106
107 @Override
108 public synchronized void stop() {
109 if (!monitorActive) {
110 return;
111 }
112 monitorActive = false;
113 resetAccounting(milliSecondFromNano());
114 trafficShapingHandler.doAccounting(this);
115 if (scheduledFuture != null) {
116 scheduledFuture.cancel(true);
117 }
118 }
119
120 @Override
121 public void resetCumulativeTime() {
122 for (PerChannel perChannel :
123 ((GlobalChannelTrafficShapingHandler) trafficShapingHandler).channelQueues.values()) {
124 perChannel.channelTrafficCounter.resetCumulativeTime();
125 }
126 super.resetCumulativeTime();
127 }
128
129 }