Reuse StringEncoders for all connections 46/86146/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 3 Dec 2019 14:27:29 +0000 (15:27 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 3 Dec 2019 17:41:38 +0000 (18:41 +0100)
StringEncoder is stateless and therefore can be shared across multiple
channel pipelines. Create a single instance for use by all connections.

Change-Id: I660cb90ac3de0476f5637d6a53a8c49826c71f1d
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
library/impl/src/main/java/org/opendaylight/ovsdb/lib/impl/OvsdbConnectionService.java

index 0e4ad09dfb883280bd22dfcd65473cf52dd5ead1..77be4ca1e30dc3ed512e5ccd1732e738adf8e9d1 100644 (file)
@@ -30,8 +30,8 @@ import io.netty.handler.logging.LoggingHandler;
 import io.netty.handler.ssl.SslHandler;
 import io.netty.handler.timeout.IdleStateHandler;
 import io.netty.handler.timeout.ReadTimeoutHandler;
-import io.netty.util.CharsetUtil;
 import java.net.InetAddress;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -92,6 +92,8 @@ public class OvsdbConnectionService implements AutoCloseable, OvsdbConnection {
     private static final String USE_SSL = "use-ssl";
     private static final int RETRY_PERIOD = 100; // retry after 100 milliseconds
 
+    private static final StringEncoder UTF8_ENCODER = new StringEncoder(StandardCharsets.UTF_8);
+
     private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newScheduledThreadPool(10,
             new ThreadFactoryBuilder().setNameFormat("OVSDBPassiveConnServ-%d").build());
 
@@ -165,7 +167,7 @@ public class OvsdbConnectionService implements AutoCloseable, OvsdbConnection {
                     channel.pipeline().addLast(
                             //new LoggingHandler(LogLevel.INFO),
                             new JsonRpcDecoder(jsonRpcDecoderMaxFrameLength),
-                            new StringEncoder(CharsetUtil.UTF_8),
+                            UTF8_ENCODER,
                             new IdleStateHandler(IDLE_READER_TIMEOUT, 0, 0),
                             new ReadTimeoutHandler(READ_TIMEOUT),
                             new ExceptionHandler(OvsdbConnectionService.this));
@@ -348,7 +350,7 @@ public class OvsdbConnectionService implements AutoCloseable, OvsdbConnection {
 
                             channel.pipeline().addLast(
                                  new JsonRpcDecoder(jsonRpcDecoderMaxFrameLength),
-                                 new StringEncoder(CharsetUtil.UTF_8),
+                                 UTF8_ENCODER,
                                  new IdleStateHandler(IDLE_READER_TIMEOUT, 0, 0),
                                  new ReadTimeoutHandler(READ_TIMEOUT),
                                  new ExceptionHandler(OvsdbConnectionService.this));