1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.handler.traffic;
17
18 import io.netty5.channel.Channel;
19 import io.netty5.channel.ChannelHandlerContext;
20 import io.netty5.util.Resource;
21 import io.netty5.util.concurrent.EventExecutor;
22 import io.netty5.util.concurrent.EventExecutorGroup;
23 import io.netty5.util.concurrent.Promise;
24
25 import java.util.ArrayDeque;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.concurrent.ConcurrentMap;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.atomic.AtomicLong;
30
31 import static java.util.Objects.requireNonNull;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public class GlobalTrafficShapingHandler extends AbstractTrafficShapingHandler {
79
80
81
82 private final ConcurrentMap<Integer, PerChannel> channelQueues = new ConcurrentHashMap<>();
83
84
85
86
87 private final AtomicLong queuesSize = new AtomicLong();
88
89
90
91
92
93 long maxGlobalWriteSize = DEFAULT_MAX_SIZE * 100;
94
95 private static final class PerChannel {
96 ArrayDeque<ToSend> messagesQueue;
97 long queueSize;
98 long lastWriteTimestamp;
99 long lastReadTimestamp;
100 }
101
102
103
104
105 void createGlobalTrafficCounter(EventExecutorGroup executor) {
106 requireNonNull(executor, "executor");
107 TrafficCounter tc = new TrafficCounter(this, executor, "GlobalTC", checkInterval);
108 setTrafficCounter(tc);
109 tc.start();
110 }
111
112 @Override
113 public boolean isSharable() {
114 return true;
115 }
116
117 @Override
118 protected int userDefinedWritabilityIndex() {
119 return AbstractTrafficShapingHandler.GLOBAL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX;
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137 public GlobalTrafficShapingHandler(EventExecutorGroup executor, long writeLimit, long readLimit,
138 long checkInterval, long maxTime) {
139 super(writeLimit, readLimit, checkInterval, maxTime);
140 createGlobalTrafficCounter(executor);
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 public GlobalTrafficShapingHandler(EventExecutorGroup executor, long writeLimit,
158 long readLimit, long checkInterval) {
159 super(writeLimit, readLimit, checkInterval);
160 createGlobalTrafficCounter(executor);
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174 public GlobalTrafficShapingHandler(EventExecutorGroup executor, long writeLimit,
175 long readLimit) {
176 super(writeLimit, readLimit);
177 createGlobalTrafficCounter(executor);
178 }
179
180
181
182
183
184
185
186
187
188
189
190 public GlobalTrafficShapingHandler(EventExecutorGroup executor, long checkInterval) {
191 super(checkInterval);
192 createGlobalTrafficCounter(executor);
193 }
194
195
196
197
198
199
200
201
202 public GlobalTrafficShapingHandler(EventExecutor executor) {
203 createGlobalTrafficCounter(executor);
204 }
205
206
207
208
209 public long getMaxGlobalWriteSize() {
210 return maxGlobalWriteSize;
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224 public void setMaxGlobalWriteSize(long maxGlobalWriteSize) {
225 this.maxGlobalWriteSize = maxGlobalWriteSize;
226 }
227
228
229
230
231 public long queuesSize() {
232 return queuesSize.get();
233 }
234
235
236
237
238 public final void release() {
239 trafficCounter.stop();
240 }
241
242 private PerChannel getOrSetPerChannel(ChannelHandlerContext ctx) {
243
244 Channel channel = ctx.channel();
245 Integer key = channel.hashCode();
246 PerChannel perChannel = channelQueues.get(key);
247 if (perChannel == null) {
248 perChannel = new PerChannel();
249 perChannel.messagesQueue = new ArrayDeque<>();
250 perChannel.queueSize = 0L;
251 perChannel.lastReadTimestamp = TrafficCounter.milliSecondFromNano();
252 perChannel.lastWriteTimestamp = perChannel.lastReadTimestamp;
253 channelQueues.put(key, perChannel);
254 }
255 return perChannel;
256 }
257
258 @Override
259 public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
260 getOrSetPerChannel(ctx);
261 super.handlerAdded(ctx);
262 }
263
264 @Override
265 public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
266 Channel channel = ctx.channel();
267 Integer key = channel.hashCode();
268 PerChannel perChannel = channelQueues.remove(key);
269 if (perChannel != null) {
270
271 synchronized (perChannel) {
272 if (channel.isActive()) {
273 for (ToSend toSend : perChannel.messagesQueue) {
274 long size = calculateSize(toSend.toSend);
275 trafficCounter.bytesRealWriteFlowControl(size);
276 perChannel.queueSize -= size;
277 queuesSize.addAndGet(-size);
278 ctx.write(toSend.toSend).cascadeTo(toSend.promise);
279 }
280 } else {
281 queuesSize.addAndGet(-perChannel.queueSize);
282 for (ToSend toSend : perChannel.messagesQueue) {
283 if (Resource.isAccessible(toSend.toSend, false)) {
284 Resource.dispose(toSend.toSend);
285 }
286 }
287 }
288 perChannel.messagesQueue.clear();
289 }
290 }
291 releaseWriteSuspended(ctx);
292 releaseReadSuspended(ctx);
293 super.handlerRemoved(ctx);
294 }
295
296 @Override
297 long checkWaitReadTime(final ChannelHandlerContext ctx, long wait, final long now) {
298 Integer key = ctx.channel().hashCode();
299 PerChannel perChannel = channelQueues.get(key);
300 if (perChannel != null) {
301 if (wait > maxTime && now + wait - perChannel.lastReadTimestamp > maxTime) {
302 wait = maxTime;
303 }
304 }
305 return wait;
306 }
307
308 @Override
309 void informReadOperation(final ChannelHandlerContext ctx, final long now) {
310 Integer key = ctx.channel().hashCode();
311 PerChannel perChannel = channelQueues.get(key);
312 if (perChannel != null) {
313 perChannel.lastReadTimestamp = now;
314 }
315 }
316
317 private static final class ToSend {
318 final long relativeTimeAction;
319 final Object toSend;
320 final long size;
321 final Promise<Void> promise;
322
323 private ToSend(final long delay, final Object toSend, final long size, final Promise<Void> promise) {
324 relativeTimeAction = delay;
325 this.toSend = toSend;
326 this.size = size;
327 this.promise = promise;
328 }
329 }
330
331 @Override
332 void submitWrite(final ChannelHandlerContext ctx, final Object msg,
333 final long size, final long writedelay, final long now,
334 final Promise<Void> promise) {
335 Channel channel = ctx.channel();
336 Integer key = channel.hashCode();
337 PerChannel perChannel = channelQueues.get(key);
338 if (perChannel == null) {
339
340
341 perChannel = getOrSetPerChannel(ctx);
342 }
343 final ToSend newToSend;
344 long delay = writedelay;
345 boolean globalSizeExceeded = false;
346
347 synchronized (perChannel) {
348 if (writedelay == 0 && perChannel.messagesQueue.isEmpty()) {
349 trafficCounter.bytesRealWriteFlowControl(size);
350 ctx.write(msg).cascadeTo(promise);
351 perChannel.lastWriteTimestamp = now;
352 return;
353 }
354 if (delay > maxTime && now + delay - perChannel.lastWriteTimestamp > maxTime) {
355 delay = maxTime;
356 }
357 newToSend = new ToSend(delay + now, msg, size, promise);
358 perChannel.messagesQueue.addLast(newToSend);
359 perChannel.queueSize += size;
360 queuesSize.addAndGet(size);
361 checkWriteSuspend(ctx, delay, perChannel.queueSize);
362 if (queuesSize.get() > maxGlobalWriteSize) {
363 globalSizeExceeded = true;
364 }
365 }
366 if (globalSizeExceeded) {
367 setUserDefinedWritability(ctx, false);
368 }
369 final long futureNow = newToSend.relativeTimeAction;
370 final PerChannel forSchedule = perChannel;
371 ctx.executor().schedule(() -> sendAllValid(ctx, forSchedule, futureNow), delay, TimeUnit.MILLISECONDS);
372 }
373
374 private void sendAllValid(final ChannelHandlerContext ctx, final PerChannel perChannel, final long now) {
375
376 synchronized (perChannel) {
377 ToSend newToSend = perChannel.messagesQueue.pollFirst();
378 for (; newToSend != null; newToSend = perChannel.messagesQueue.pollFirst()) {
379 if (newToSend.relativeTimeAction <= now) {
380 long size = newToSend.size;
381 trafficCounter.bytesRealWriteFlowControl(size);
382 perChannel.queueSize -= size;
383 queuesSize.addAndGet(-size);
384 ctx.write(newToSend.toSend).cascadeTo(newToSend.promise);
385 perChannel.lastWriteTimestamp = now;
386 } else {
387 perChannel.messagesQueue.addFirst(newToSend);
388 break;
389 }
390 }
391 if (perChannel.messagesQueue.isEmpty()) {
392 releaseWriteSuspended(ctx);
393 }
394 }
395 ctx.flush();
396 }
397 }