Repository: netty/netty
Issue: Add @Sharable annotation to WebSocketClientCompressionHandler
Labels: improvement

Netty version: netty-4.1.0.CR1

Unable to use WebSocketServerCompressionHandler and WebSocketServerCompressionHandler handlers as single instances. Because of absence of @Sharable annotation.


Issue comments:
@mrniko you are right... I will fix it. In the meantime just create a new instance everytime.

@mrniko PTAL https://github.com/netty/netty/pull/4769

@normanmaurer PTAL ? Sorry, what? :)

@mrniko Please take a look ;) 

@mrniko actually only WebSocketClientCompressionHandler can be `@Sharable` as the other is keeping state. Fixed the PR.

@normanmaurer OK, I got you!

Fixed by https://github.com/netty/netty/pull/4769

@normanmaurer @mrniko why WebSocketServerCompressionHandler cannot be be @Sharable . i can't understand. Can you explain it to me？
It's a subclass of `WebSocketServerExtensionHandler` which keeps state per `connection` so it cant be shared. 

Pull request review comments:
What is the need for the `INSTANCE` member if it can't be reused and the constructor is public?

ups sorry missed to remove it ... 


Patch that resolved the issue:
diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/extensions/compression/WebSocketClientCompressionHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/extensions/compression/WebSocketClientCompressionHandler.java
index 3596ab658fa..bfd0375accf 100644
--- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/extensions/compression/WebSocketClientCompressionHandler.java
+++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/extensions/compression/WebSocketClientCompressionHandler.java
@@ -15,6 +15,7 @@
  */
 package io.netty.handler.codec.http.websocketx.extensions.compression;
 
+import io.netty.channel.ChannelHandler;
 import io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandler;
 
 /**
@@ -23,12 +24,12 @@
  *
  * See <tt>io.netty.example.http.websocketx.client.WebSocketClient</tt> for usage.
  */
-public class WebSocketClientCompressionHandler extends WebSocketClientExtensionHandler {
+@ChannelHandler.Sharable
+public final class WebSocketClientCompressionHandler extends WebSocketClientExtensionHandler {
 
-    /**
-     * Constructor with default configuration.
-     */
-    public WebSocketClientCompressionHandler() {
+    public static final WebSocketClientCompressionHandler INSTANCE = new WebSocketClientCompressionHandler();
+
+    private WebSocketClientCompressionHandler() {
         super(new PerMessageDeflateClientExtensionHandshaker(),
                 new DeflateFrameClientExtensionHandshaker(false),
                 new DeflateFrameClientExtensionHandshaker(true));
diff --git a/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClient.java b/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClient.java
index 4c56d6c3ae1..8aa3b5cdbc2 100644
--- a/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClient.java
+++ b/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClient.java
@@ -113,7 +113,7 @@ protected void initChannel(SocketChannel ch) {
                      p.addLast(
                              new HttpClientCodec(),
                              new HttpObjectAggregator(8192),
-                             new WebSocketClientCompressionHandler(),
+                             WebSocketClientCompressionHandler.INSTANCE,
                              handler);
                  }
              });


Test patch: