Register NioSocketChannel 79/107179/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 31 Jul 2023 22:20:41 +0000 (00:20 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 1 Aug 2023 11:30:20 +0000 (13:30 +0200)
NioSocketChannel must be bound to an Eventloop during close(). This
patch adds the smarts to associate it.

JIRA: NETCONF-1030
Change-Id: I0fcb2d130f38f4f44c4c9979d81ea8c2e611b0a6
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 14538c723ef4888de48625e55c9cfa09c2410c50)

transport/transport-tcp/pom.xml
transport/transport-tcp/src/main/java/org/opendaylight/netconf/transport/tcp/NettyTransportSupport.java
transport/transport-tcp/src/main/java/org/opendaylight/netconf/transport/tcp/NioNettyImpl.java

index 3100b233c968287b325460e34ee0fff8e5dab7f4..be8b13c3d4eee982f36792710ea8279f27c69db3 100644 (file)
             <groupId>org.opendaylight.netconf</groupId>
             <artifactId>transport-api</artifactId>
         </dependency>
-
-        <dependency>
-            <groupId>io.netty</groupId>
-            <artifactId>netty-transport-native-epoll</artifactId>
-            <classifier>linux-x86_64</classifier>
-            <scope>test</scope>
-        </dependency>
     </dependencies>
 
     <build>
index e89c700ef72372bc266665a14a79581cd9ffcb6f..ed12a12227e174394a381de7e5169f04f350646d 100644 (file)
@@ -30,7 +30,7 @@ import org.slf4j.LoggerFactory;
 @NonNullByDefault
 public final class NettyTransportSupport {
     private static final Logger LOG = LoggerFactory.getLogger(NettyTransportSupport.class);
-    private static final AbstractNettyImpl IMPL = Epoll.isAvailable() ? new EpollNettyImpl() : new NioNettyImpl();
+    private static final AbstractNettyImpl IMPL = Epoll.isAvailable() ? new EpollNettyImpl() : NioNettyImpl.INSTANCE;
 
     static {
         LOG.info("Netty transport backed by {}", IMPL);
index 3e1f93cf4513962204778f1bd102edff9108f216..c25f9b7db42dc8ef6062f7b28ffcd8ff0919c448 100644 (file)
@@ -98,17 +98,37 @@ final class NioNettyImpl extends AbstractNettyImpl {
         SUPPORT = support;
     }
 
-    private final boolean supportsKeepalives;
+    static final NioNettyImpl INSTANCE;
 
-    NioNettyImpl() {
-        final var ch = new NioSocketChannel();
+    static {
+        final var grp = new NioEventLoopGroup();
         try {
-            supportsKeepalives = SUPPORT.configureKeepalives(ch.config());
-        } finally {
-            ch.close();
+            try {
+                final var ch = new NioSocketChannel();
+                grp.register(ch).sync();
+
+                final boolean supportsKeepalives;
+                try {
+                    supportsKeepalives = SUPPORT.configureKeepalives(ch.config());
+                } finally {
+                    ch.close().sync();
+                }
+                INSTANCE = new NioNettyImpl(supportsKeepalives);
+            } finally {
+                grp.shutdownGracefully().sync();
+            }
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new ExceptionInInitializerError(e);
         }
     }
 
+    private final boolean supportsKeepalives;
+
+    private NioNettyImpl(final boolean supportsKeepalives) {
+        this.supportsKeepalives = supportsKeepalives;
+    }
+
     @Override
     Class<NioSocketChannel> channelClass() {
         return NioSocketChannel.class;