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 — Auto-port 4.1: POOL: Release Channel when SimpleChannelPool acquire i… · netty/netty@ec1de88

File tree

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

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

Copy file name to clipboardExpand all lines: transport/src/main/java/io/netty/channel/pool/SimpleChannelPool.java
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,10 @@ private void notifyHealthCheck(Future<Boolean> future, Channel channel, Promise<
247247
if (future.isSuccess() && future.getNow()) {
248248
channel.attr(POOL_KEY).set(this);
249249
handler.channelAcquired(channel);
250-
promise.setSuccess(channel);
250+
if (!promise.trySuccess(channel)) {
251+
// Promise was completed in the meantime (like cancelled), just release the channel again
252+
release(channel);
253+
}
251254
} else {
252255
closeChannel(channel);
253256
acquireHealthyFromPoolOrNew(promise);
Collapse file

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

Copy file name to clipboardExpand all lines: transport/src/test/java/io/netty/channel/pool/SimpleChannelPoolTest.java
+85Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424
import io.netty.channel.EventLoopGroup;
2525
import io.netty.channel.local.LocalAddress;
2626
import io.netty.channel.local.LocalChannel;
27+
import io.netty.channel.local.LocalEventLoopGroup;
2728
import io.netty.channel.local.LocalServerChannel;
2829
import io.netty.util.concurrent.Future;
30+
import io.netty.util.concurrent.Promise;
2931
import org.junit.jupiter.api.Test;
3032
import org.junit.jupiter.api.function.Executable;
3133

3234
import java.util.Queue;
35+
import java.util.concurrent.BlockingQueue;
3336
import java.util.concurrent.LinkedBlockingQueue;
3437
import java.util.concurrent.TimeUnit;
3538
import java.util.concurrent.atomic.AtomicBoolean;
@@ -457,4 +460,86 @@ public void channelCreated(Channel ch) {
457460
pool.close();
458461
group.shutdownGracefully();
459462
}
463+
464+
@Test
465+
public void testAcquireCancelledDuringHealthCheckOffersChannelBack() throws Exception {
466+
EventLoopGroup group = new LocalEventLoopGroup(1);
467+
LocalAddress addr = new LocalAddress(getLocalAddrId());
468+
Bootstrap cb = new Bootstrap()
469+
.group(group)
470+
.channel(LocalChannel.class)
471+
.remoteAddress(addr);
472+
ServerBootstrap sb = new ServerBootstrap()
473+
.group(group)
474+
.channel(LocalServerChannel.class)
475+
.childHandler(new ChannelInitializer<LocalChannel>() {
476+
@Override
477+
public void initChannel(LocalChannel ch) throws Exception {
478+
ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
479+
}
480+
});
481+
Channel sc = sb.bind(addr).sync().channel();
482+
CountingChannelPoolHandler handler = new CountingChannelPoolHandler();
483+
484+
// Hand out health checks that we complete ourselves, so the acquire Promise can be cancelled while the
485+
// health check of an already pooled Channel is still pending.
486+
final BlockingQueue<Promise<Boolean>> healthChecks = new LinkedBlockingQueue<Promise<Boolean>>();
487+
ChannelHealthChecker healthChecker = new ChannelHealthChecker() {
488+
@Override
489+
public Future<Boolean> isHealthy(Channel channel) {
490+
Promise<Boolean> healthCheck = channel.eventLoop().newPromise();
491+
healthChecks.add(healthCheck);
492+
return healthCheck;
493+
}
494+
};
495+
// Health checks on release are turned off, so releasing does not need a hand-completed health check as well.
496+
SimpleChannelPool pool = new SimpleChannelPool(cb, handler, healthChecker, false);
497+
498+
Channel channel = null;
499+
try {
500+
// Nothing is pooled yet, so this connects a new Channel and does not run a health check at all.
501+
channel = pool.acquire().sync().getNow();
502+
pool.release(channel).sync();
503+
assertTrue(healthChecks.isEmpty());
504+
assertEquals(1, handler.acquiredCount());
505+
assertEquals(1, handler.releasedCount());
506+
507+
// This acquire reuses the pooled Channel, so it has to pass the health check first.
508+
Promise<Channel> acquirePromise = group.next().newPromise();
509+
pool.acquire(acquirePromise);
510+
Promise<Boolean> healthCheck = healthChecks.poll(5, TimeUnit.SECONDS);
511+
assertNotNull(healthCheck);
512+
513+
assertTrue(acquirePromise.cancel(false));
514+
healthCheck.setSuccess(Boolean.TRUE);
515+
516+
// Let the health check listener run on the EventLoop of the Channel.
517+
channel.eventLoop().submit(new Runnable() {
518+
@Override
519+
public void run() {
520+
// NOOP.
521+
}
522+
}).sync();
523+
524+
assertTrue(acquirePromise.isCancelled());
525+
assertEquals(2, handler.acquiredCount());
526+
assertEquals(2, handler.releasedCount());
527+
assertTrue(channel.isActive());
528+
529+
// The Channel went back into the pool, so the next acquire hands out the very same Channel.
530+
Promise<Channel> secondAcquirePromise = group.next().newPromise();
531+
pool.acquire(secondAcquirePromise);
532+
Promise<Boolean> secondHealthCheck = healthChecks.poll(5, TimeUnit.SECONDS);
533+
assertNotNull(secondHealthCheck);
534+
secondHealthCheck.setSuccess(Boolean.TRUE);
535+
assertSame(channel, secondAcquirePromise.sync().getNow());
536+
} finally {
537+
if (channel != null) {
538+
channel.close().sync();
539+
}
540+
sc.close().sync();
541+
pool.close();
542+
group.shutdownGracefully();
543+
}
544+
}
460545
}

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.