franz1981 commented on a change in pull request #45: URL: https://github.com/apache/qpid-jms/pull/45#discussion_r739250792
########## File path: qpid-jms-client/src/main/java/org/apache/qpid/jms/transports/netty/NettyEventLoopGroupPool.java ########## @@ -0,0 +1,175 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.qpid.jms.transports.netty; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.util.concurrent.Future; +import org.apache.qpid.jms.transports.TransportOptions; +import org.apache.qpid.jms.util.QpidJMSThreadFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public enum NettyEventLoopGroupPool { + Instance; + + private static final Logger LOG = LoggerFactory.getLogger(NettyEventLoopGroupPool.class); + private static final int SHUTDOWN_TIMEOUT = 50; + private static final AtomicInteger SHARED_NETTY_EVENT_LOOP_GROUP_SEQUENCE = new AtomicInteger(0); + + public interface Lease extends AutoCloseable { + + EventLoopGroup group(); + + @Override + default void close() { + Future<?> fut = group().shutdownGracefully(0, SHUTDOWN_TIMEOUT, TimeUnit.MILLISECONDS); + if (!fut.awaitUninterruptibly(2 * SHUTDOWN_TIMEOUT)) { + LOG.trace("Channel group shutdown failed to complete in allotted time"); + } + } + } + + private static final class DisposableLease implements Lease { + + private final SharedGroupRef sharedGroupRef; + private final AtomicBoolean closed; + + public DisposableLease(SharedGroupRef sharedGroupRef) { + this.sharedGroupRef = sharedGroupRef; + this.closed = new AtomicBoolean(); + } + + @Override + public EventLoopGroup group() { + return sharedGroupRef.group(); + } + + @Override + public void close() { + if (closed.compareAndSet(false, true)) { + sharedGroupRef.release(); + } + } + } + + private final class SharedGroupRef { + + private final EventLoopGroup group; + private final AtomicInteger refs; + + private SharedGroupRef(final EventLoopGroup group) { + this.group = group; + refs = new AtomicInteger(1); + } + + public boolean retain() { + while (true) { + final int currValue = refs.get(); + if (currValue == 0) { + // this has been already disposed! + return false; + } + if (refs.compareAndSet(currValue, currValue + 1)) { + return true; + } + } + } + + public EventLoopGroup group() { + if (refs.get() == 0) { + throw new IllegalStateException("the event loop group cannot be reused"); + } + return group; + } + + public void release() { + while (true) { + final int currValue = refs.get(); + if (currValue == 0) { + return; + } + if (refs.compareAndSet(currValue, currValue - 1)) { + if (currValue == 1) { + // if a racing thread has borrowed this lease, it would try already to set it to a new value: + // this one is a best effort cleanup to help GC + sharedEventLoopGroup.compareAndSet(this, null); Review comment: Nope, just the release process can cause a racing thread interested into borrowing a new instance to fail to `retain` and failback to create a whole new loop group (that's correct): this cleanup can then race with the creation of the new loop group and replace it with a fresh new instance, but we still would like to cleanup (with a null) the shared reference in case the race won't happen. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
