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 — Release channel when FixedChannelPool acquire is cancelled (#17287) (… · lltopk/netty@a83d62f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • transport/src
    • main/java/io/netty/channel/pool
      • FixedChannelPool.java
    • test/java/io/netty/channel/pool
      • FixedChannelPoolTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎transport/src/main/java/io/netty/channel/pool/FixedChannelPool.java‎

Copy file name to clipboardExpand all lines: transport/src/main/java/io/netty/channel/pool/FixedChannelPool.java
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,11 @@ public void operationComplete(Future<Channel> future) throws Exception {
421421
}
422422

423423
if (future.isSuccess()) {
424-
originalPromise.setSuccess(future.getNow());
424+
Channel channel = future.getNow();
425+
if (!originalPromise.trySuccess(channel)) {
426+
// Promise was completed in the meantime (like cancelled), just release the channel again.
427+
release(channel);
428+
}
425429
} else {
426430
if (acquired) {
427431
decrementAndRunTaskQueue();
Collapse file

‎transport/src/test/java/io/netty/channel/pool/FixedChannelPoolTest.java‎

Copy file name to clipboardExpand all lines: transport/src/test/java/io/netty/channel/pool/FixedChannelPoolTest.java
+175-2Lines changed: 175 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
import org.junit.jupiter.api.Test;
3636
import org.junit.jupiter.api.function.Executable;
3737

38+
import java.util.concurrent.CountDownLatch;
3839
import java.util.concurrent.ExecutionException;
3940
import java.util.concurrent.TimeUnit;
4041
import java.util.concurrent.TimeoutException;
42+
import java.util.concurrent.atomic.AtomicReference;
4143

4244
import static io.netty.channel.pool.ChannelPoolTestUtils.getLocalAddrId;
4345

@@ -92,6 +94,140 @@ public void testAcquire() throws Exception {
9294
pool.close();
9395
}
9496

97+
@Test
98+
public void testCancelledAcquireReleasesChannel() throws Exception {
99+
EventLoopGroup localGroup = new DefaultEventLoopGroup(1);
100+
Tuple t = bootstrap(localGroup);
101+
BlockingChannelPoolHandler handler = new BlockingChannelPoolHandler();
102+
FixedChannelPool pool = null;
103+
Channel sc = null;
104+
try {
105+
sc = t.sb.bind(t.address).syncUninterruptibly().channel();
106+
pool = new FixedChannelPool(t.cb, handler, 1);
107+
108+
Future<Channel> cancelledAcquire = pool.acquire();
109+
assertTrue(handler.awaitChannelAcquired());
110+
assertEquals(1, pool.acquiredChannelCount());
111+
assertTrue(cancelledAcquire.cancel(false));
112+
113+
Future<Channel> nextAcquire = pool.acquire();
114+
handler.continueAcquire();
115+
116+
assertTrue(nextAcquire.await(5, TimeUnit.SECONDS));
117+
assertTrue(nextAcquire.isSuccess());
118+
assertTrue(cancelledAcquire.isCancelled());
119+
assertSame(handler.channel(), nextAcquire.getNow());
120+
assertEquals(1, pool.acquiredChannelCount());
121+
122+
pool.release(nextAcquire.getNow()).syncUninterruptibly();
123+
assertEquals(0, pool.acquiredChannelCount());
124+
pool.close();
125+
assertFalse(handler.channel().isOpen());
126+
} finally {
127+
handler.continueAcquire();
128+
if (pool != null) {
129+
pool.close();
130+
}
131+
if (handler.channel() != null) {
132+
handler.channel().close().syncUninterruptibly();
133+
}
134+
if (sc != null) {
135+
sc.close().syncUninterruptibly();
136+
}
137+
localGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly();
138+
}
139+
}
140+
141+
@Test
142+
public void testCancelledPendingAcquireRunsNextTask() throws Exception {
143+
EventLoopGroup localGroup = new DefaultEventLoopGroup(1);
144+
Tuple t = bootstrap(localGroup);
145+
FixedChannelPool pool = null;
146+
Channel sc = null;
147+
Channel channel = null;
148+
try {
149+
sc = t.sb.bind(t.address).syncUninterruptibly().channel();
150+
pool = new FixedChannelPool(t.cb, new TestChannelPoolHandler(), 1);
151+
channel = pool.acquire().syncUninterruptibly().getNow();
152+
153+
Future<Channel> cancelledAcquire = pool.acquire();
154+
localGroup.submit(new Runnable() {
155+
@Override
156+
public void run() {
157+
// NOOP
158+
}
159+
}).syncUninterruptibly();
160+
assertFalse(cancelledAcquire.isDone());
161+
assertTrue(cancelledAcquire.cancel(false));
162+
163+
Future<Channel> nextAcquire = pool.acquire();
164+
localGroup.submit(new Runnable() {
165+
@Override
166+
public void run() {
167+
// NOOP
168+
}
169+
}).syncUninterruptibly();
170+
assertFalse(nextAcquire.isDone());
171+
172+
pool.release(channel).syncUninterruptibly();
173+
assertTrue(nextAcquire.await(5, TimeUnit.SECONDS));
174+
assertTrue(nextAcquire.isSuccess());
175+
assertSame(channel, nextAcquire.getNow());
176+
assertEquals(1, pool.acquiredChannelCount());
177+
178+
pool.release(nextAcquire.getNow()).syncUninterruptibly();
179+
assertEquals(0, pool.acquiredChannelCount());
180+
} finally {
181+
if (pool != null) {
182+
pool.close();
183+
}
184+
if (channel != null) {
185+
channel.close().syncUninterruptibly();
186+
}
187+
if (sc != null) {
188+
sc.close().syncUninterruptibly();
189+
}
190+
localGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly();
191+
}
192+
}
193+
194+
@Test
195+
public void testCancelledAcquireChannelClosedWhenPoolCloses() throws Exception {
196+
EventLoopGroup localGroup = new DefaultEventLoopGroup(1);
197+
Tuple t = bootstrap(localGroup);
198+
BlockingChannelPoolHandler handler = new BlockingChannelPoolHandler();
199+
FixedChannelPool pool = null;
200+
Channel sc = null;
201+
try {
202+
sc = t.sb.bind(t.address).syncUninterruptibly().channel();
203+
pool = new FixedChannelPool(t.cb, handler, 1);
204+
205+
Future<Channel> cancelledAcquire = pool.acquire();
206+
assertTrue(handler.awaitChannelAcquired());
207+
assertTrue(cancelledAcquire.cancel(false));
208+
Future<Void> closeFuture = pool.closeAsync();
209+
handler.continueAcquire();
210+
211+
assertTrue(closeFuture.await(5, TimeUnit.SECONDS));
212+
assertTrue(closeFuture.isSuccess());
213+
assertTrue(handler.channel().closeFuture().await(5, TimeUnit.SECONDS));
214+
assertFalse(handler.channel().isOpen());
215+
assertEquals(0, pool.acquiredChannelCount());
216+
} finally {
217+
handler.continueAcquire();
218+
if (pool != null) {
219+
pool.close();
220+
}
221+
if (handler.channel() != null) {
222+
handler.channel().close().syncUninterruptibly();
223+
}
224+
if (sc != null) {
225+
sc.close().syncUninterruptibly();
226+
}
227+
localGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly();
228+
}
229+
}
230+
95231
@Test
96232
public void testAcquireTimeout() throws Exception {
97233
testAcquireTimeout(500);
@@ -315,13 +451,17 @@ public void channelCreated(Channel ch) {
315451
}
316452

317453
private Tuple bootstrap() {
454+
return bootstrap(group);
455+
}
456+
457+
private Tuple bootstrap(EventLoopGroup eventLoopGroup) {
318458
LocalAddress addr = new LocalAddress(getLocalAddrId());
319459
Bootstrap cb = new Bootstrap();
320460
cb.remoteAddress(addr);
321-
cb.group(group).channel(LocalChannel.class);
461+
cb.group(eventLoopGroup).channel(LocalChannel.class);
322462

323463
ServerBootstrap sb = new ServerBootstrap();
324-
sb.group(group)
464+
sb.group(eventLoopGroup)
325465
.channel(LocalServerChannel.class)
326466
.childHandler(new ChannelInitializer<LocalChannel>() {
327467
@Override
@@ -333,6 +473,39 @@ public void initChannel(LocalChannel ch) {
333473
return new Tuple(addr, cb, sb);
334474
}
335475

476+
private static final class BlockingChannelPoolHandler extends AbstractChannelPoolHandler {
477+
private final CountDownLatch channelAcquired = new CountDownLatch(1);
478+
private final CountDownLatch continueAcquire = new CountDownLatch(1);
479+
private final AtomicReference<Channel> channel = new AtomicReference<Channel>();
480+
481+
@Override
482+
public void channelCreated(Channel ch) {
483+
// NOOP
484+
}
485+
486+
@Override
487+
public void channelAcquired(Channel ch) throws Exception {
488+
if (channel.compareAndSet(null, ch)) {
489+
channelAcquired.countDown();
490+
if (!continueAcquire.await(5, TimeUnit.SECONDS)) {
491+
throw new TimeoutException("Timed out waiting to continue acquire");
492+
}
493+
}
494+
}
495+
496+
boolean awaitChannelAcquired() throws InterruptedException {
497+
return channelAcquired.await(5, TimeUnit.SECONDS);
498+
}
499+
500+
void continueAcquire() {
501+
continueAcquire.countDown();
502+
}
503+
504+
Channel channel() {
505+
return channel.get();
506+
}
507+
}
508+
336509
private static final class TestChannelPoolHandler extends AbstractChannelPoolHandler {
337510
@Override
338511
public void channelCreated(Channel ch) throws Exception {

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.