Issue #300 - use inflater/deflater pools in CompressExtension

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2019-07-02 16:42:50 +10:00
parent 64eb3217cd
commit 705e1cafe0
2 changed files with 20 additions and 12 deletions

View File

@ -83,15 +83,13 @@ public abstract class CompressExtension extends AbstractExtension
*/ */
private static final int DECOMPRESS_BUF_SIZE = 8 * 1024; private static final int DECOMPRESS_BUF_SIZE = 8 * 1024;
private static final boolean NOWRAP = true;
private final Queue<FrameEntry> entries = new ArrayDeque<>(); private final Queue<FrameEntry> entries = new ArrayDeque<>();
private final IteratingCallback flusher = new Flusher(); private final IteratingCallback flusher = new Flusher();
private Deflater deflaterImpl; private Deflater deflaterImpl;
private Inflater inflaterImpl; private Inflater inflaterImpl;
protected AtomicInteger decompressCount = new AtomicInteger(0); protected AtomicInteger decompressCount = new AtomicInteger(0);
private int tailDrop = TAIL_DROP_NEVER; private int tailDrop;
private int rsvUse = RSV_USE_ALWAYS; private int rsvUse;
protected CompressExtension() protected CompressExtension()
{ {
@ -102,21 +100,29 @@ public abstract class CompressExtension extends AbstractExtension
public Deflater getDeflater() public Deflater getDeflater()
{ {
if (deflaterImpl == null) if (deflaterImpl == null)
{ deflaterImpl = getDeflaterPool().acquire();
deflaterImpl = new Deflater(Deflater.DEFAULT_COMPRESSION, NOWRAP);
}
return deflaterImpl; return deflaterImpl;
} }
public Inflater getInflater() public Inflater getInflater()
{ {
if (inflaterImpl == null) if (inflaterImpl == null)
{ inflaterImpl = getInflaterPool().acquire();
inflaterImpl = new Inflater(NOWRAP);
}
return inflaterImpl; return inflaterImpl;
} }
public void releaseInflater()
{
getInflaterPool().release(inflaterImpl);
inflaterImpl = null;
}
public void releaseDeflater()
{
getInflaterPool().release(inflaterImpl);
inflaterImpl = null;
}
/** /**
* Indicates use of RSV1 flag for indicating deflation is in use. * Indicates use of RSV1 flag for indicating deflation is in use.
*/ */
@ -409,6 +415,8 @@ public abstract class CompressExtension extends AbstractExtension
@Override @Override
public void failed(Throwable cause) public void failed(Throwable cause)
{ {
releaseInflater();
releaseDeflater();
notifyCallbackFailure(current.callback, cause); notifyCallbackFailure(current.callback, cause);
// If something went wrong, very likely the compression context // If something went wrong, very likely the compression context
// will be invalid, so we need to fail this IteratingCallback. // will be invalid, so we need to fail this IteratingCallback.

View File

@ -115,7 +115,7 @@ public class PerMessageDeflateExtension extends CompressExtension
{ {
LOG.debug("Incoming Context Reset"); LOG.debug("Incoming Context Reset");
decompressCount.set(0); decompressCount.set(0);
getInflater().reset(); releaseInflater();
} }
super.nextIncomingFrame(frame, callback); super.nextIncomingFrame(frame, callback);
} }
@ -126,7 +126,7 @@ public class PerMessageDeflateExtension extends CompressExtension
if (frame.isFin() && !outgoingContextTakeover) if (frame.isFin() && !outgoingContextTakeover)
{ {
LOG.debug("Outgoing Context Reset"); LOG.debug("Outgoing Context Reset");
getDeflater().reset(); releaseDeflater();
} }
super.nextOutgoingFrame(frame, callback, batch); super.nextOutgoingFrame(frame, callback, batch);
} }