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 · netty/netty@76fae38

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
@@ -417,7 +417,11 @@ public void operationComplete(Future<Channel> future) throws Exception {
417417
}
418418

419419
if (future.isSuccess()) {
420-
originalPromise.setSuccess(future.getNow());
420+
Channel channel = future.getNow();
421+
if (!originalPromise.trySuccess(channel)) {
422+
// Promise was completed in the meantime (like cancelled), just release the channel again.
423+
release(channel);
424+
}
421425
} else {
422426
if (acquired) {
423427
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
+165-2Lines changed: 165 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@
3939

4040
import java.util.ArrayList;
4141
import java.util.List;
42+
import java.util.concurrent.CountDownLatch;
4243
import java.util.concurrent.ExecutionException;
4344
import java.util.concurrent.TimeUnit;
4445
import java.util.concurrent.TimeoutException;
46+
import java.util.concurrent.atomic.AtomicReference;
4547

4648
import static io.netty.channel.pool.ChannelPoolTestUtils.getLocalAddrId;
4749

@@ -96,6 +98,130 @@ public void testAcquire() throws Exception {
9698
pool.close();
9799
}
98100

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

408534
private Tuple bootstrap() {
535+
return bootstrap(group);
536+
}
537+
538+
private Tuple bootstrap(EventLoopGroup eventLoopGroup) {
409539
LocalAddress addr = new LocalAddress(getLocalAddrId());
410540
Bootstrap cb = new Bootstrap();
411541
cb.remoteAddress(addr);
412-
cb.group(group).channel(LocalChannel.class);
542+
cb.group(eventLoopGroup).channel(LocalChannel.class);
413543

414544
ServerBootstrap sb = new ServerBootstrap();
415-
sb.group(group)
545+
sb.group(eventLoopGroup)
416546
.channel(LocalServerChannel.class)
417547
.childHandler(new ChannelInitializer<LocalChannel>() {
418548
@Override
@@ -424,6 +554,39 @@ public void initChannel(LocalChannel ch) {
424554
return new Tuple(addr, cb, sb);
425555
}
426556

557+
private static final class BlockingChannelPoolHandler extends AbstractChannelPoolHandler {
558+
private final CountDownLatch channelAcquired = new CountDownLatch(1);
559+
private final CountDownLatch continueAcquire = new CountDownLatch(1);
560+
private final AtomicReference<Channel> channel = new AtomicReference<>();
561+
562+
@Override
563+
public void channelCreated(Channel ch) {
564+
// NOOP
565+
}
566+
567+
@Override
568+
public void channelAcquired(Channel ch) throws Exception {
569+
if (channel.compareAndSet(null, ch)) {
570+
channelAcquired.countDown();
571+
if (!continueAcquire.await(5, TimeUnit.SECONDS)) {
572+
throw new TimeoutException("Timed out waiting to continue acquire");
573+
}
574+
}
575+
}
576+
577+
boolean awaitChannelAcquired() throws InterruptedException {
578+
return channelAcquired.await(5, TimeUnit.SECONDS);
579+
}
580+
581+
void continueAcquire() {
582+
continueAcquire.countDown();
583+
}
584+
585+
Channel channel() {
586+
return channel.get();
587+
}
588+
}
589+
427590
private static final class TestChannelPoolHandler extends AbstractChannelPoolHandler {
428591
@Override
429592
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.