Skip to content

Commit e2f2199

Browse files
committed
Fixes #14260 - Improve GZIP performance
Signed-off-by: Simone Bordet <[email protected]>
1 parent a11dc40 commit e2f2199

File tree

4 files changed

+28
-30
lines changed

4 files changed

+28
-30
lines changed

jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,9 @@ public boolean handle(Request request, Response response, Callback callback) thr
590590
if (inflatable && tryInflate || etagMatches)
591591
{
592592
// Wrap the request to update the fields and do any inflation
593-
request = new GzipRequest(request, inflatable && tryInflate ? getInflateBufferSize() : -1);
593+
GzipRequest gzipRequest = new GzipRequest(request, _inflaterPool, inflatable && tryInflate ? getInflateBufferSize() : -1);
594+
request = gzipRequest;
595+
callback = Callback.from(callback, gzipRequest::destroy);
594596
}
595597

596598
if (tryDeflate && _vary != null)
@@ -611,9 +613,10 @@ public boolean handle(Request request, Response response, Callback callback) thr
611613
if (next.handle(request, response, callback))
612614
return true;
613615

614-
// If the request was not accepted, destroy any gzipRequest wrapper
616+
// If the request was not handled, destroy GzipRequest.
615617
if (request instanceof GzipRequest gzipRequest)
616618
gzipRequest.destroy();
619+
617620
return false;
618621
}
619622

jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipRequest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,19 @@ public class GzipRequest extends Request.Wrapper
3434
{
3535
private static final HttpField X_CE_GZIP = new PreEncodedHttpField("X-Content-Encoding", "gzip");
3636

37-
// TODO: use InflaterPool from somewhere.
38-
private static final InflaterPool __inflaterPool = new InflaterPool(-1, true);
39-
4037
private final HttpFields _fields;
4138
private Decoder _decoder;
4239
private GzipTransformer _gzipTransformer;
4340

44-
public GzipRequest(Request request, int inflateBufferSize)
41+
public GzipRequest(Request request, InflaterPool inflaterPool, int inflateBufferSize)
4542
{
4643
super(request);
4744
_fields = updateRequestFields(request, inflateBufferSize > 0);
4845

4946
if (inflateBufferSize > 0)
5047
{
5148
Components components = getComponents();
52-
_decoder = new Decoder(__inflaterPool, components.getByteBufferPool(), inflateBufferSize);
49+
_decoder = new Decoder(inflaterPool, components.getByteBufferPool(), inflateBufferSize);
5350
_gzipTransformer = new GzipTransformer(getWrapped(), _decoder);
5451
}
5552
}

jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipResponseAndCallback.java

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,34 +87,18 @@ public GzipResponseAndCallback(GzipHandler handler, Request request, Response re
8787
@Override
8888
public void succeeded()
8989
{
90-
try
91-
{
92-
// We need to write nothing here to intercept the committing of the
93-
// response and possibly change headers in case write is never called.
94-
if (_last)
95-
_callback.succeeded();
96-
else
97-
write(true, null, _callback);
98-
}
99-
finally
100-
{
101-
if (getRequest() instanceof GzipRequest gzipRequest)
102-
gzipRequest.destroy();
103-
}
90+
// We need to write nothing here to intercept the committing of the
91+
// response and possibly change headers in case write is never called.
92+
if (_last)
93+
_callback.succeeded();
94+
else
95+
write(true, null, _callback);
10496
}
10597

10698
@Override
10799
public void failed(Throwable x)
108100
{
109-
try
110-
{
111-
_callback.failed(x);
112-
}
113-
finally
114-
{
115-
if (getRequest() instanceof GzipRequest gzipRequest)
116-
gzipRequest.destroy();
117-
}
101+
_callback.failed(x);
118102
}
119103

120104
@Override

jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/gzip/GzipHandlerTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.concurrent.atomic.AtomicInteger;
3636
import java.util.stream.Collectors;
3737
import java.util.stream.Stream;
38+
import java.util.zip.Deflater;
3839
import java.util.zip.GZIPInputStream;
3940
import java.util.zip.GZIPOutputStream;
4041
import java.util.zip.Inflater;
@@ -71,8 +72,12 @@
7172
import org.eclipse.jetty.util.Callback;
7273
import org.eclipse.jetty.util.Fields;
7374
import org.eclipse.jetty.util.IO;
75+
import org.eclipse.jetty.util.Pool;
7476
import org.eclipse.jetty.util.StringUtil;
7577
import org.eclipse.jetty.util.component.LifeCycle;
78+
import org.eclipse.jetty.util.compression.CompressionPool;
79+
import org.eclipse.jetty.util.compression.DeflaterPool;
80+
import org.eclipse.jetty.util.compression.InflaterPool;
7681
import org.hamcrest.MatcherAssert;
7782
import org.hamcrest.Matchers;
7883
import org.junit.jupiter.api.AfterEach;
@@ -86,6 +91,7 @@
8691
import org.junit.jupiter.params.provider.ValueSource;
8792

8893
import static java.nio.charset.StandardCharsets.UTF_8;
94+
import static org.awaitility.Awaitility.await;
8995
import static org.hamcrest.MatcherAssert.assertThat;
9096
import static org.hamcrest.Matchers.both;
9197
import static org.hamcrest.Matchers.contains;
@@ -944,6 +950,10 @@ public void testGzippedRequestPost() throws Exception
944950

945951
assertThat(response.getStatus(), is(200));
946952
assertThat(response.getContent(), is(data));
953+
954+
InflaterPool inflaterPool = _gzipHandler.getBean(InflaterPool.class);
955+
Pool<CompressionPool<Inflater>.Entry> pool = inflaterPool.getPool();
956+
await().atMost(5, TimeUnit.SECONDS).until(pool::getInUseCount, is(0));
947957
}
948958

949959
@Test
@@ -1307,6 +1317,10 @@ public void testLargeResponse() throws Exception
13071317
{
13081318
assertEquals(CONTENT, new String(Arrays.copyOfRange(bytes, i * CONTENT_BYTES.length, (i + 1) * CONTENT_BYTES.length), StandardCharsets.UTF_8), "chunk " + i);
13091319
}
1320+
1321+
DeflaterPool deflaterPool = _gzipHandler.getBean(DeflaterPool.class);
1322+
Pool<CompressionPool<Deflater>.Entry> pool = deflaterPool.getPool();
1323+
await().atMost(5, TimeUnit.SECONDS).until(pool::getInUseCount, is(0));
13101324
}
13111325

13121326
/**

0 commit comments

Comments
 (0)