Skip to content

Commit

Permalink
Fix issue jetty#5499
Browse files Browse the repository at this point in the history
this PR let the ByteAccumulator recyclable. after invoke ByteAccumulator.transferTo method
we can invoke ByteAccumulator.recycle method to reuse byte[] via ByteAccumulator.newByteArray method

Signed-off-by: Baoyi Chen <chen.bao.yi@qq.com>
  • Loading branch information
leonchen83 committed Oct 29, 2020
1 parent a98265c commit 1fc4f56
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
Expand Up @@ -54,6 +54,11 @@ public int getLength()
return length;
}

int getMaxSize()
{
return maxSize;
}

ByteBuffer newByteBuffer(int size)
{
ByteBuffer buf;
Expand Down
Expand Up @@ -48,9 +48,9 @@ public abstract class CompressExtension extends AbstractExtension
private static final Logger LOG = Log.getLogger(CompressExtension.class);

/**
* thread local accumulator
* Accumulator
*/
protected ThreadLocal<ByteAccumulator> accumulator = ThreadLocal.withInitial(() -> newByteAccumulator());
protected ByteAccumulator accumulator;

/**
* Never drop tail bytes 0000FFFF, from any frame type
Expand Down Expand Up @@ -181,7 +181,11 @@ protected void forwardIncoming(Frame frame, ByteAccumulator accumulator)
protected ByteAccumulator newByteAccumulator()
{
int maxSize = Math.max(getPolicy().getMaxTextMessageSize(), getPolicy().getMaxBinaryMessageSize());
return new ByteAccumulator(maxSize);
if (accumulator == null || accumulator.getMaxSize() != maxSize)
{
accumulator = new ByteAccumulator(maxSize);
}
return accumulator;
}

protected void decompress(ByteAccumulator accumulator, ByteBuffer buf) throws DataFormatException
Expand Down
Expand Up @@ -63,7 +63,7 @@ public void incomingFrame(Frame frame)

try
{
ByteAccumulator accumulator = this.accumulator.get();
accumulator = newByteAccumulator();
decompress(accumulator, frame.getPayload());
decompress(accumulator, TAIL_BYTES_BUF.slice());
forwardIncoming(frame, accumulator);
Expand Down
Expand Up @@ -78,10 +78,9 @@ public void incomingFrame(Frame frame)
throw new ProtocolException("Invalid RSV1 set on permessage-deflate CONTINUATION frame");
}

ByteAccumulator accumulator = this.accumulator.get();

try
{
accumulator = newByteAccumulator();
ByteBuffer payload = frame.getPayload();
decompress(accumulator, payload);
if (frame.isFin())
Expand Down

0 comments on commit 1fc4f56

Please sign in to comment.