/** * Create a simple {@link LoopResources} to provide automatically for {@link * EventLoopGroup} and {@link Channel} factories * * @param prefix the event loop thread name prefix * * @return a new {@link LoopResources} to provide automatically for {@link * EventLoopGroup} and {@link Channel} factories */ static LoopResources create(String prefix) { return new DefaultLoopResources(prefix, DEFAULT_IO_SELECT_COUNT, DEFAULT_IO_WORKER_COUNT, true); }
@Override public EventLoopGroup onServer(boolean useNative) { if (useNative && preferNative()) { return cacheNativeServerLoops(); } return cacheNioServerLoops(); }
@Override public EventLoopGroup onClient(boolean useNative) { if (useNative && preferNative()) { return cacheNativeClientLoops(); } return cacheNioClientLoops(); }
@Override public EventLoopGroup onServerSelect(boolean useNative) { if (useNative && preferNative()) { return cacheNativeSelectLoops(); } return cacheNioSelectLoops(); }
@Test public void disposeLaterSubsequentIsQuick() { DefaultLoopResources loopResources = new DefaultLoopResources( "test", 0, false); loopResources.onServer(true); assertThat(loopResources.isDisposed()).isFalse(); Duration firstInvocation = StepVerifier.create(loopResources.disposeLater()) .verifyComplete(); assertThat(loopResources.isDisposed()).isTrue(); if (!loopResources.preferNative()) { assertThat(loopResources.serverLoops.get().isTerminated()).isTrue(); } else { assertThat(loopResources.cacheNativeServerLoops.get().isTerminated()).isTrue(); } Duration secondInvocation = StepVerifier.create(loopResources.disposeLater()) .verifyComplete(); assertThat(secondInvocation).isLessThan(firstInvocation); }
EventLoopGroup cacheNioSelectLoops() { if (serverSelectLoops == serverLoops) { return cacheNioServerLoops(); } EventLoopGroup eventLoopGroup = serverSelectLoops.get(); if (null == eventLoopGroup) { EventLoopGroup newEventLoopGroup = new NioEventLoopGroup(selectCount, threadFactory(this, "select-nio")); if (!serverSelectLoops.compareAndSet(null, newEventLoopGroup)) { newEventLoopGroup.shutdownGracefully(); } eventLoopGroup = cacheNioSelectLoops(); } return eventLoopGroup; }
EventLoopGroup cacheNativeSelectLoops() { if (cacheNativeSelectLoops == cacheNativeServerLoops) { return cacheNativeServerLoops(); } EventLoopGroup eventLoopGroup = cacheNativeSelectLoops.get(); if (null == eventLoopGroup) { DefaultLoop defaultLoop = DefaultLoopNativeDetector.getInstance(); EventLoopGroup newEventLoopGroup = defaultLoop.newEventLoopGroup( selectCount, threadFactory(this, "select-" + defaultLoop.getName())); if (!cacheNativeSelectLoops.compareAndSet(null, newEventLoopGroup)) { newEventLoopGroup.shutdownGracefully(); } eventLoopGroup = cacheNativeSelectLoops(); } return eventLoopGroup; }
@Test public void disposeLaterDefers() { DefaultLoopResources loopResources = new DefaultLoopResources( "test", 0, false); Mono<Void> disposer = loopResources.disposeLater(); assertThat(loopResources.isDisposed()).isFalse(); disposer.subscribe(); assertThat(loopResources.isDisposed()).isTrue(); }
EventLoopGroup cacheNioClientLoops() { EventLoopGroup eventLoopGroup = clientLoops.get(); if (null == eventLoopGroup) { EventLoopGroup newEventLoopGroup = LoopResources.colocate(cacheNioServerLoops()); if (!clientLoops.compareAndSet(null, newEventLoopGroup)) { // Do not shutdown newEventLoopGroup as this will shutdown the server loops } eventLoopGroup = cacheNioClientLoops(); } return eventLoopGroup; }
EventLoopGroup cacheNativeClientLoops() { EventLoopGroup eventLoopGroup = cacheNativeClientLoops.get(); if (null == eventLoopGroup) { EventLoopGroup newEventLoopGroup = LoopResources.colocate(cacheNativeServerLoops()); if (!cacheNativeClientLoops.compareAndSet(null, newEventLoopGroup)) { // Do not shutdown newEventLoopGroup as this will shutdown the server loops } eventLoopGroup = cacheNativeClientLoops(); } return eventLoopGroup; }
EventLoopGroup cacheNioServerLoops() { EventLoopGroup eventLoopGroup = serverLoops.get(); if (null == eventLoopGroup) { EventLoopGroup newEventLoopGroup = new NioEventLoopGroup(workerCount, threadFactory(this, "nio")); if (!serverLoops.compareAndSet(null, newEventLoopGroup)) { newEventLoopGroup.shutdownGracefully(); } eventLoopGroup = cacheNioServerLoops(); } return eventLoopGroup; }
EventLoopGroup cacheNativeServerLoops() { EventLoopGroup eventLoopGroup = cacheNativeServerLoops.get(); if (null == eventLoopGroup) { DefaultLoop defaultLoop = DefaultLoopNativeDetector.getInstance(); EventLoopGroup newEventLoopGroup = defaultLoop.newEventLoopGroup( workerCount, threadFactory(this, defaultLoop.getName())); if (!cacheNativeServerLoops.compareAndSet(null, newEventLoopGroup)) { newEventLoopGroup.shutdownGracefully(); } eventLoopGroup = cacheNativeServerLoops(); } return eventLoopGroup; }
@Override public EventLoopGroup onServerSelect(boolean useNative) { if (useNative && preferNative()) { return cacheNativeSelectLoops(); } return cacheNioSelectLoops(); }
EventLoopGroup cacheNioSelectLoops() { if (serverSelectLoops == serverLoops) { return cacheNioServerLoops(); } EventLoopGroup eventLoopGroup = serverSelectLoops.get(); if (null == eventLoopGroup) { EventLoopGroup newEventLoopGroup = new NioEventLoopGroup(selectCount, threadFactory(this, "select-nio")); if (!serverSelectLoops.compareAndSet(null, newEventLoopGroup)) { newEventLoopGroup.shutdownGracefully(); } eventLoopGroup = cacheNioSelectLoops(); } return eventLoopGroup; }
EventLoopGroup cacheNativeSelectLoops() { if (cacheNativeSelectLoops == cacheNativeServerLoops) { return cacheNativeServerLoops(); } EventLoopGroup eventLoopGroup = cacheNativeSelectLoops.get(); if (null == eventLoopGroup) { DefaultLoop defaultLoop = DefaultLoopNativeDetector.getInstance(); EventLoopGroup newEventLoopGroup = defaultLoop.newEventLoopGroup( selectCount, threadFactory(this, "select-" + defaultLoop.getName())); if (!cacheNativeSelectLoops.compareAndSet(null, newEventLoopGroup)) { newEventLoopGroup.shutdownGracefully(); } eventLoopGroup = cacheNativeSelectLoops(); } return eventLoopGroup; }
EventLoopGroup cacheNioClientLoops() { EventLoopGroup eventLoopGroup = clientLoops.get(); if (null == eventLoopGroup) { EventLoopGroup newEventLoopGroup = LoopResources.colocate(cacheNioServerLoops()); if (!clientLoops.compareAndSet(null, newEventLoopGroup)) { // Do not shutdown newEventLoopGroup as this will shutdown the server loops } eventLoopGroup = cacheNioClientLoops(); } return eventLoopGroup; }
EventLoopGroup cacheNativeClientLoops() { EventLoopGroup eventLoopGroup = cacheNativeClientLoops.get(); if (null == eventLoopGroup) { EventLoopGroup newEventLoopGroup = LoopResources.colocate(cacheNativeServerLoops()); if (!cacheNativeClientLoops.compareAndSet(null, newEventLoopGroup)) { // Do not shutdown newEventLoopGroup as this will shutdown the server loops } eventLoopGroup = cacheNativeClientLoops(); } return eventLoopGroup; }
EventLoopGroup cacheNioServerLoops() { EventLoopGroup eventLoopGroup = serverLoops.get(); if (null == eventLoopGroup) { EventLoopGroup newEventLoopGroup = new NioEventLoopGroup(workerCount, threadFactory(this, "nio")); if (!serverLoops.compareAndSet(null, newEventLoopGroup)) { newEventLoopGroup.shutdownGracefully(); } eventLoopGroup = cacheNioServerLoops(); } return eventLoopGroup; }
EventLoopGroup cacheNativeServerLoops() { EventLoopGroup eventLoopGroup = cacheNativeServerLoops.get(); if (null == eventLoopGroup) { DefaultLoop defaultLoop = DefaultLoopNativeDetector.getInstance(); EventLoopGroup newEventLoopGroup = defaultLoop.newEventLoopGroup( workerCount, threadFactory(this, defaultLoop.getName())); if (!cacheNativeServerLoops.compareAndSet(null, newEventLoopGroup)) { newEventLoopGroup.shutdownGracefully(); } eventLoopGroup = cacheNativeServerLoops(); } return eventLoopGroup; }
@Override public EventLoopGroup onServer(boolean useNative) { if (useNative && preferNative()) { return cacheNativeServerLoops(); } return cacheNioServerLoops(); }