GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Correctly release and fail queued traffic-shaping writes on close (#1… · netty/netty@ec4efdb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/traffic
      • AbstractTrafficShapingHandler.java
      • ChannelTrafficShapingHandler.java
      • GlobalChannelTrafficShapingHandler.java
      • GlobalTrafficShapingHandler.java
    • test/java/io/netty/handler/traffic
      • TrafficShapingHandlerTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.netty.channel.FileRegion;
2929
import io.netty.util.Attribute;
3030
import io.netty.util.AttributeKey;
31+
import io.netty.util.ReferenceCountUtil;
3132
import io.netty.util.internal.logging.InternalLogger;
3233
import io.netty.util.internal.logging.InternalLoggerFactory;
3334

@@ -579,6 +580,14 @@ protected void submitWrite(final ChannelHandlerContext ctx, final Object msg,
579580
abstract void submitWrite(
580581
ChannelHandlerContext ctx, Object msg, long size, long delay, long now, ChannelPromise promise);
581582

583+
/**
584+
* Releases the given {@code msg} and fails the given {@code promise} with the supplied {@code cause}.
585+
*/
586+
static void releaseAndFailQueuedWrite(Object msg, ChannelPromise promise, Throwable cause) {
587+
ReferenceCountUtil.safeRelease(msg);
588+
promise.tryFailure(cause);
589+
}
590+
582591
@Override
583592
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
584593
setUserDefinedWritability(ctx, true);
Collapse file

‎handler/src/main/java/io/netty/handler/traffic/ChannelTrafficShapingHandler.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/traffic/ChannelTrafficShapingHandler.java
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
*/
1616
package io.netty.handler.traffic;
1717

18-
import io.netty.buffer.ByteBuf;
1918
import io.netty.channel.ChannelHandlerContext;
2019
import io.netty.channel.ChannelPromise;
2120

21+
import java.nio.channels.ClosedChannelException;
2222
import java.util.ArrayDeque;
2323
import java.util.concurrent.TimeUnit;
2424

@@ -148,12 +148,12 @@ public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
148148
queueSize -= size;
149149
ctx.write(toSend.toSend, toSend.promise);
150150
}
151-
} else {
151+
} else if (!messagesQueue.isEmpty()) {
152+
ClosedChannelException cause = new ClosedChannelException();
152153
for (ToSend toSend : messagesQueue) {
153-
if (toSend.toSend instanceof ByteBuf) {
154-
((ByteBuf) toSend.toSend).release();
155-
}
154+
releaseAndFailQueuedWrite(toSend.toSend, toSend.promise, cause);
156155
}
156+
queueSize = 0;
157157
}
158158
messagesQueue.clear();
159159
}
Collapse file

‎handler/src/main/java/io/netty/handler/traffic/GlobalChannelTrafficShapingHandler.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/traffic/GlobalChannelTrafficShapingHandler.java
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static io.netty.util.internal.ObjectUtil.checkPositive;
2020
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
2121

22-
import io.netty.buffer.ByteBuf;
2322
import io.netty.channel.Channel;
2423
import io.netty.channel.ChannelHandler.Sharable;
2524
import io.netty.channel.ChannelConfig;
@@ -30,6 +29,7 @@
3029
import io.netty.util.internal.logging.InternalLogger;
3130
import io.netty.util.internal.logging.InternalLoggerFactory;
3231

32+
import java.nio.channels.ClosedChannelException;
3333
import java.util.AbstractCollection;
3434
import java.util.ArrayDeque;
3535
import java.util.Collection;
@@ -495,13 +495,13 @@ public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
495495
queuesSize.addAndGet(-size);
496496
ctx.write(toSend.toSend, toSend.promise);
497497
}
498-
} else {
498+
} else if (!perChannel.messagesQueue.isEmpty()) {
499499
queuesSize.addAndGet(-perChannel.queueSize);
500+
ClosedChannelException cause = new ClosedChannelException();
500501
for (ToSend toSend : perChannel.messagesQueue) {
501-
if (toSend.toSend instanceof ByteBuf) {
502-
((ByteBuf) toSend.toSend).release();
503-
}
502+
releaseAndFailQueuedWrite(toSend.toSend, toSend.promise, cause);
504503
}
504+
perChannel.queueSize = 0;
505505
}
506506
perChannel.messagesQueue.clear();
507507
}
Collapse file

‎handler/src/main/java/io/netty/handler/traffic/GlobalTrafficShapingHandler.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/traffic/GlobalTrafficShapingHandler.java
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
*/
1616
package io.netty.handler.traffic;
1717

18-
import io.netty.buffer.ByteBuf;
1918
import io.netty.channel.ChannelHandler.Sharable;
2019
import io.netty.channel.Channel;
2120
import io.netty.channel.ChannelHandlerContext;
2221
import io.netty.channel.ChannelPromise;
2322
import io.netty.util.concurrent.EventExecutor;
2423
import io.netty.util.internal.ObjectUtil;
2524

25+
import java.nio.channels.ClosedChannelException;
2626
import java.util.ArrayDeque;
2727
import java.util.concurrent.ConcurrentHashMap;
2828
import java.util.concurrent.ConcurrentMap;
@@ -276,13 +276,13 @@ public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
276276
queuesSize.addAndGet(-size);
277277
ctx.write(toSend.toSend, toSend.promise);
278278
}
279-
} else {
279+
} else if (!perChannel.messagesQueue.isEmpty()) {
280280
queuesSize.addAndGet(-perChannel.queueSize);
281+
ClosedChannelException cause = new ClosedChannelException();
281282
for (ToSend toSend : perChannel.messagesQueue) {
282-
if (toSend.toSend instanceof ByteBuf) {
283-
((ByteBuf) toSend.toSend).release();
284-
}
283+
releaseAndFailQueuedWrite(toSend.toSend, toSend.promise, cause);
285284
}
285+
perChannel.queueSize = 0;
286286
}
287287
perChannel.messagesQueue.clear();
288288
}
Collapse file

‎handler/src/test/java/io/netty/handler/traffic/TrafficShapingHandlerTest.java‎

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/traffic/TrafficShapingHandlerTest.java
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,23 @@
1616

1717
package io.netty.handler.traffic;
1818

19+
import java.nio.channels.ClosedChannelException;
1920
import java.util.concurrent.Executors;
2021
import java.util.concurrent.ScheduledExecutorService;
2122

2223
import io.netty.bootstrap.Bootstrap;
2324
import io.netty.bootstrap.ServerBootstrap;
25+
import io.netty.buffer.ByteBufHolder;
26+
import io.netty.buffer.DefaultByteBufHolder;
2427
import io.netty.buffer.Unpooled;
2528
import io.netty.channel.Channel;
2629
import io.netty.channel.ChannelHandlerContext;
2730
import io.netty.channel.ChannelInboundHandlerAdapter;
2831
import io.netty.channel.ChannelInitializer;
32+
import io.netty.channel.ChannelPromise;
2933
import io.netty.channel.EventLoopGroup;
3034
import io.netty.channel.MultiThreadIoEventLoopGroup;
35+
import io.netty.channel.embedded.EmbeddedChannel;
3136
import io.netty.channel.local.LocalAddress;
3237
import io.netty.channel.local.LocalChannel;
3338
import io.netty.channel.local.LocalIoHandler;
@@ -37,12 +42,16 @@
3742
import org.junit.jupiter.api.AfterAll;
3843
import org.junit.jupiter.api.Test;
3944

45+
import static org.junit.jupiter.api.Assertions.assertEquals;
46+
import static org.junit.jupiter.api.Assertions.assertFalse;
4047
import static org.junit.jupiter.api.Assertions.assertNotNull;
4148
import static org.junit.jupiter.api.Assertions.assertNull;
49+
import static org.junit.jupiter.api.Assertions.assertTrue;
4250

4351
public class TrafficShapingHandlerTest {
4452

4553
private static final long READ_LIMIT_BYTES_PER_SECOND = 1;
54+
private static final long WRITE_LIMIT_BYTES_PER_SECOND = 1;
4655
private static final ScheduledExecutorService SES = Executors.newSingleThreadScheduledExecutor();
4756
private static final EventLoopGroup GROUP = new MultiThreadIoEventLoopGroup(1, LocalIoHandler.newFactory());
4857

@@ -72,6 +81,46 @@ public void testHandlerRemove() throws Exception {
7281
}
7382
}
7483

84+
@Test
85+
public void testQueuedWritesReleasedAndFailedOnClose() {
86+
testQueuedWritesReleasedAndFailedOnClose0(new ChannelTrafficShapingHandler(
87+
WRITE_LIMIT_BYTES_PER_SECOND, 0, 0));
88+
89+
GlobalTrafficShapingHandler trafficHandler1 =
90+
new GlobalTrafficShapingHandler(SES, WRITE_LIMIT_BYTES_PER_SECOND, 0, 0);
91+
try {
92+
testQueuedWritesReleasedAndFailedOnClose0(trafficHandler1);
93+
} finally {
94+
trafficHandler1.release();
95+
}
96+
97+
GlobalChannelTrafficShapingHandler trafficHandler2 =
98+
new GlobalChannelTrafficShapingHandler(SES, WRITE_LIMIT_BYTES_PER_SECOND, 0,
99+
WRITE_LIMIT_BYTES_PER_SECOND, 0, 0);
100+
try {
101+
testQueuedWritesReleasedAndFailedOnClose0(trafficHandler2);
102+
} finally {
103+
trafficHandler2.release();
104+
}
105+
}
106+
107+
private static void testQueuedWritesReleasedAndFailedOnClose0(AbstractTrafficShapingHandler trafficHandler) {
108+
EmbeddedChannel ch = new EmbeddedChannel(trafficHandler);
109+
ByteBufHolder holder = new DefaultByteBufHolder(Unpooled.buffer(64).writeZero(64));
110+
ChannelPromise promise = ch.newPromise();
111+
112+
ch.writeOneOutbound(holder, promise);
113+
assertFalse(promise.isDone());
114+
assertNull(ch.readOutbound());
115+
assertEquals(1, holder.refCnt());
116+
117+
ch.close().syncUninterruptibly();
118+
assertEquals(0, holder.refCnt());
119+
assertTrue(promise.isDone());
120+
assertTrue(promise.cause() instanceof ClosedChannelException);
121+
assertFalse(ch.finishAndReleaseAll());
122+
}
123+
75124
private void testHandlerRemove0(final AbstractTrafficShapingHandler trafficHandler)
76125
throws Exception {
77126
Channel svrChannel = null;

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.