-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clone PerMessageDeflateExtension values correctly #1439
Conversation
@@ -330,7 +330,11 @@ public String getProvidedExtensionAsServer() { | |||
|
|||
@Override | |||
public IExtension copyInstance() { | |||
return new PerMessageDeflateExtension(); | |||
PerMessageDeflateExtension clone = new PerMessageDeflateExtension(); | |||
clone.setThreshold(this.getThreshold()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please insert:
- clone.setDeflater(this.getDeflater());
- clone.setInflater(this.getInflater());
and replace the new Inflater() calls in lines 169 and 177 with inflater.reset(), and the deflater.end() & new Deflater() calls in lines 247/248 with deflater.reset().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are not going to reuse the deflater/inflater for multiple clients
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using reset however should be done, I agree
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are not going to reuse the deflater/inflater for multiple clients
Could you elaborate? If the user of this library wants to hand in a different inflater/deflater, why should that not be used?
Specific use case: I want to use BEST_COMPRESSION, not DEFAULT_COMPRESSION, to get the most out of compression (and both my servers and clients are powerful enough to put up that extra effort). That's why I hand in a new Deflater(Deflater.BEST_COMPRESSION, true) instance. I'd like that to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that we create a new instance of a Draft (and therefore the provided extension with the custom deflater/inflater) for every new client connecting to the server.
https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/java/org/java_websocket/WebSocketImpl.java#L272
If we simply use the single instance provided by the user, we will get in trouble for the server implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we simply use the single instance provided by the user, we will get in trouble for the server implementation
Ah, thanks, I see. I was just lucky that I was only using 1 server and client for my WebSocket compression tests.
Indeed, then an API change is needed to set Inflater and Deflater factories. Or, if we think that this flexibility is not needed, a setCompressionLevel(int level); API would do for me. Then this class would always instantiate the Inflater and Deflater objects, always with nowrap=true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking more into the documentation, I think that nowrap will always be true in our case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we simply use the single instance provided by the user, we will get in trouble for the server implementation
Ah, thanks, I see. I was just lucky that I was only using 1 server and client for my WebSocket compression tests.
Indeed, then an API change is needed to set Inflater and Deflater factories. Or, if we think that this flexibility is not needed, a setCompressionLevel(int level); API would do for me. Then this class would always instantiate the Inflater and Deflater objects, always with nowrap=true.
Yeah I think so too. We only need the compression level, nowrap should be true.
@PhilipRoman you got any objections?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, there is no getter for the level/strategy of a Deflater.
Therefore:
new API -> setCompressionLevel()
remove getter/setter for Deflater/Inflater
Looks good, although personally I would add some calls to setDeflaterLevel in the functional tests like testDecodeFrame. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, changes are needed for Java 8 compatibility.
this.deflater = deflater; | ||
/** | ||
* Set the compression level used for the compressor. | ||
* @param level the compression level (0-9) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though the Deflater Javadoc says the same, the implementation actually also accepts DEFAULT_COMPRESSION, which is -1. The getter above would then return -1 as well.
public void setDeflaterLevel(int level) { | ||
this.deflater.setLevel(level); | ||
this.deflaterLevel = level; | ||
//If the compression level is changed, the next invocation of deflate will compress the input available so far with the old level (and may be flushed); the new level will take effect only after that invocation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, this does not work in Java 8:
While this matches the Java 8 Deflater Javadoc, the implementation does not match the documentation:
https://github.com/openjdk/jdk8u/blob/master/jdk/src/share/classes/java/util/zip/Deflater.java
The set level is never picked up as far as I can tell. The "setParams" boolean is never evaluated. It is in Java 11, though, so it works there:
https://github.com/openjdk/jdk11u/blob/master/src/java.base/share/classes/java/util/zip/Deflater.java
So, for Java 8 compatibility, the Deflater instance has to be created with the configured compression level.
How about not having setDeflaterLevel at all, but rather making it an optional constructor argument:
private final int compressionLevel;
private final Inflater inflater;
private final Deflater deflater;
public PerMessageDeflateExtension() {
this(Deflater.DEFAULT_COMPRESSION);
}
public PerMessageDeflateExtension(int compressionLevel) {
this.compressionLevel = compressionLevel;
this.inflater = new Inflater(true);
this.deflater = new Deflater(compressionLevel, true);
}
public int getCompressionLevel() {
return compressionLevel;
}
@OverRide
public IExtension copyInstance() {
PerMessageDeflateExtension clone = new PerMessageDeflateExtension(this.getCompressionLevel());
clone.setThreshold(this.getThreshold());
clone.setClientNoContextTakeover(this.isClientNoContextTakeover());
clone.setServerNoContextTakeover(this.isServerNoContextTakeover());
return clone;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Thanks for your patience!
Description
Related Issue
Fixes #1437
Fixes #1400
Motivation and Context
Issue
How Has This Been Tested?
Additional tests
Types of changes
Checklist: