|
61 | 61 |
|
62 | 62 | import java.net.InetSocketAddress;
|
63 | 63 | import java.net.SocketOption;
|
| 64 | +import java.util.ArrayList; |
| 65 | +import java.util.List; |
64 | 66 | import java.util.concurrent.TimeUnit;
|
65 | 67 |
|
66 | 68 | import io.netty.bootstrap.ServerBootstrap;
|
|
75 | 77 | import io.netty.channel.RecvByteBufAllocator;
|
76 | 78 | import io.netty.channel.socket.nio.NioChannelOption;
|
77 | 79 | import io.netty.handler.codec.ByteToMessageDecoder;
|
| 80 | +import io.netty.handler.codec.compression.Brotli; |
| 81 | +import io.netty.handler.codec.compression.CompressionOptions; |
| 82 | +import io.netty.handler.codec.compression.DeflateOptions; |
| 83 | +import io.netty.handler.codec.compression.GzipOptions; |
| 84 | +import io.netty.handler.codec.compression.StandardCompressionOptions; |
| 85 | +import io.netty.handler.codec.compression.ZstdEncoder; |
78 | 86 | import io.netty.handler.codec.http.HttpContentCompressor;
|
79 | 87 | import io.netty.handler.codec.http.HttpContentDecompressor;
|
80 | 88 | import io.netty.handler.codec.http.HttpObjectAggregator;
|
@@ -374,7 +382,11 @@ protected void initChannel(Channel ch) throws Exception {
|
374 | 382 | aggregator.setMaxCumulationBufferComponents(transport.maxCompositeBufferComponents);
|
375 | 383 | ch.pipeline().addLast("aggregator", aggregator);
|
376 | 384 | if (handlingSettings.isCompression()) {
|
377 |
| - ch.pipeline().addLast("encoder_compress", new HttpContentCompressor(handlingSettings.getCompressionLevel())); |
| 385 | + ch.pipeline() |
| 386 | + .addLast( |
| 387 | + "encoder_compress", |
| 388 | + new HttpContentCompressor(defaultCompressionOptions(handlingSettings.getCompressionLevel())) |
| 389 | + ); |
378 | 390 | }
|
379 | 391 | ch.pipeline().addLast("request_creator", requestCreator);
|
380 | 392 | ch.pipeline().addLast("response_creator", responseCreator);
|
@@ -427,4 +439,59 @@ protected ChannelInboundHandlerAdapter createHeaderVerifier() {
|
427 | 439 | protected ChannelInboundHandlerAdapter createDecompressor() {
|
428 | 440 | return new HttpContentDecompressor();
|
429 | 441 | }
|
| 442 | + |
| 443 | + /** |
| 444 | + * Copy of {@link HttpContentCompressor} default compression options with ZSTD excluded: |
| 445 | + * although zstd-jni is on the classpath, {@link ZstdEncoder} requires direct buffers support |
| 446 | + * which by default {@link NettyAllocator} does not provide. |
| 447 | + * |
| 448 | + * @param compressionLevel |
| 449 | + * {@code 1} yields the fastest compression and {@code 9} yields the |
| 450 | + * best compression. {@code 0} means no compression. The default |
| 451 | + * compression level is {@code 6}. |
| 452 | + * |
| 453 | + * @return default compression options |
| 454 | + */ |
| 455 | + private static CompressionOptions[] defaultCompressionOptions(int compressionLevel) { |
| 456 | + return defaultCompressionOptions(compressionLevel, 15, 8); |
| 457 | + } |
| 458 | + |
| 459 | + /** |
| 460 | + * Copy of {@link HttpContentCompressor} default compression options with ZSTD excluded: |
| 461 | + * although zstd-jni is on the classpath, {@link ZstdEncoder} requires direct buffers support |
| 462 | + * which by default {@link NettyAllocator} does not provide. |
| 463 | + * |
| 464 | + * @param compressionLevel |
| 465 | + * {@code 1} yields the fastest compression and {@code 9} yields the |
| 466 | + * best compression. {@code 0} means no compression. The default |
| 467 | + * compression level is {@code 6}. |
| 468 | + * @param windowBits |
| 469 | + * The base two logarithm of the size of the history buffer. The |
| 470 | + * value should be in the range {@code 9} to {@code 15} inclusive. |
| 471 | + * Larger values result in better compression at the expense of |
| 472 | + * memory usage. The default value is {@code 15}. |
| 473 | + * @param memLevel |
| 474 | + * How much memory should be allocated for the internal compression |
| 475 | + * state. {@code 1} uses minimum memory and {@code 9} uses maximum |
| 476 | + * memory. Larger values result in better and faster compression |
| 477 | + * at the expense of memory usage. The default value is {@code 8} |
| 478 | + * |
| 479 | + * @return default compression options |
| 480 | + */ |
| 481 | + private static CompressionOptions[] defaultCompressionOptions(int compressionLevel, int windowBits, int memLevel) { |
| 482 | + final List<CompressionOptions> options = new ArrayList<CompressionOptions>(4); |
| 483 | + final GzipOptions gzipOptions = StandardCompressionOptions.gzip(compressionLevel, windowBits, memLevel); |
| 484 | + final DeflateOptions deflateOptions = StandardCompressionOptions.deflate(compressionLevel, windowBits, memLevel); |
| 485 | + |
| 486 | + options.add(gzipOptions); |
| 487 | + options.add(deflateOptions); |
| 488 | + options.add(StandardCompressionOptions.snappy()); |
| 489 | + |
| 490 | + if (Brotli.isAvailable()) { |
| 491 | + options.add(StandardCompressionOptions.brotli()); |
| 492 | + } |
| 493 | + |
| 494 | + return options.toArray(new CompressionOptions[0]); |
| 495 | + } |
| 496 | + |
430 | 497 | }
|
0 commit comments