Eliminate BootstrapConnectListener 77/101377/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 31 May 2022 18:29:24 +0000 (20:29 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 31 May 2022 18:34:58 +0000 (20:34 +0200)
Use a simple method and a method handle instead of an explicit
class. This makes things a tad simpler on synchronization side of
things.

Change-Id: Id341b8a3ff124bd95a1e4e14f60721e6b443f72b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/NetconfSessionPromise.java

index f41153445236d47ffb126dfd93fdb8086a74e18a..6cb4ba59c974ea252ac2de6c467727658c446e4e 100644 (file)
@@ -54,7 +54,7 @@ final class NetconfSessionPromise<S extends NetconfSession> extends DefaultPromi
             }
             final ChannelFuture connectFuture = bootstrap.connect(address);
             // Add listener that attempts reconnect by invoking this method again.
-            connectFuture.addListener(new BootstrapConnectListener());
+            connectFuture.addListener((ChannelFutureListener) this::channelConnectComplete);
             pending = connectFuture;
         } catch (final Exception e) {
             LOG.info("Failed to connect to {}", address, e);
@@ -79,44 +79,38 @@ final class NetconfSessionPromise<S extends NetconfSession> extends DefaultPromi
         return super.setSuccess(result);
     }
 
-    private class BootstrapConnectListener implements ChannelFutureListener {
-        @Override
-        public void operationComplete(final ChannelFuture cf) {
-            synchronized (NetconfSessionPromise.this) {
-                LOG.debug("Promise {} connection resolved", NetconfSessionPromise.this);
-
-                // Triggered when a connection attempt is resolved.
-                checkState(pending.equals(cf));
-
-                /*
-                 * The promise we gave out could have been cancelled,
-                 * which cascades to the connect getting cancelled,
-                 * but there is a slight race window, where the connect
-                 * is already resolved, but the listener has not yet
-                 * been notified -- cancellation at that point won't
-                 * stop the notification arriving, so we have to close
-                 * the race here.
-                 */
-                if (isCancelled()) {
-                    if (cf.isSuccess()) {
-                        LOG.debug("Closing channel for cancelled promise {}", NetconfSessionPromise.this);
-                        cf.channel().close();
-                    }
-                    return;
-                }
+    // Triggered when a connection attempt is resolved.
+    private synchronized void channelConnectComplete(final ChannelFuture cf) {
+        LOG.debug("Promise {} connection resolved", this);
+        checkState(pending.equals(cf));
+
+        /*
+         * The promise we gave out could have been cancelled,
+         * which cascades to the connect getting cancelled,
+         * but there is a slight race window, where the connect
+         * is already resolved, but the listener has not yet
+         * been notified -- cancellation at that point won't
+         * stop the notification arriving, so we have to close
+         * the race here.
+         */
+        if (isCancelled()) {
+            if (cf.isSuccess()) {
+                LOG.debug("Closing channel for cancelled promise {}", this);
+                cf.channel().close();
+            }
+            return;
+        }
 
-                if (cf.isSuccess()) {
-                    LOG.debug("Promise {} connection successful", NetconfSessionPromise.this);
-                    return;
-                }
+        if (cf.isSuccess()) {
+            LOG.debug("Promise {} connection successful", this);
+            return;
+        }
 
-                LOG.debug("Attempt to connect to {} failed", address, cf.cause());
+        LOG.debug("Attempt to connect to {} failed", address, cf.cause());
 
-                final Future<Void> rf = strategy.scheduleReconnect(cf.cause());
-                rf.addListener(new ReconnectingStrategyListener());
-                pending = rf;
-            }
-        }
+        final Future<Void> rf = strategy.scheduleReconnect(cf.cause());
+        rf.addListener(new ReconnectingStrategyListener());
+        pending = rf;
     }
 
     private class ReconnectingStrategyListener implements FutureListener<Void> {