From c14bf735a2dc0698546a1ff1548d2d3d7a5c8675 Mon Sep 17 00:00:00 2001 From: bkmz Date: Tue, 2 Apr 2019 11:56:46 +0300 Subject: [PATCH 1/8] Compression pool for PermessageDeflate extensions for websockets - Implements Compression Pool (based on apache Commons Pool) - Implements Inflater Pool - Plugs Compression Pools usage in Permessage-Deflate Signed-off-by: bkmz --- .../server/jmh/DeflaterPoolBenchmark.java | 2 +- .../eclipse/jetty/server/DeflaterPool.java | 129 ------------- .../server/handler/gzip/GzipHandler.java | 2 +- jetty-util/pom.xml | 4 + .../eclipse/jetty/util/CompressionPool.java | 170 ++++++++++++++++++ .../org/eclipse/jetty/util/DeflaterPool.java | 79 ++++++++ .../org/eclipse/jetty/util/InflaterPool.java | 76 ++++++++ .../compress/CompressExtension.java | 17 +- pom.xml | 5 + 9 files changed, 348 insertions(+), 136 deletions(-) delete mode 100644 jetty-server/src/main/java/org/eclipse/jetty/server/DeflaterPool.java create mode 100644 jetty-util/src/main/java/org/eclipse/jetty/util/CompressionPool.java create mode 100644 jetty-util/src/main/java/org/eclipse/jetty/util/DeflaterPool.java create mode 100644 jetty-util/src/main/java/org/eclipse/jetty/util/InflaterPool.java diff --git a/jetty-jmh/src/main/java/org/eclipse/jetty/server/jmh/DeflaterPoolBenchmark.java b/jetty-jmh/src/main/java/org/eclipse/jetty/server/jmh/DeflaterPoolBenchmark.java index 784abd908c7..a11bb8df71a 100644 --- a/jetty-jmh/src/main/java/org/eclipse/jetty/server/jmh/DeflaterPoolBenchmark.java +++ b/jetty-jmh/src/main/java/org/eclipse/jetty/server/jmh/DeflaterPoolBenchmark.java @@ -21,7 +21,7 @@ package org.eclipse.jetty.server.jmh; import java.util.concurrent.TimeUnit; import java.util.zip.Deflater; -import org.eclipse.jetty.server.DeflaterPool; +import org.eclipse.jetty.util.DeflaterPool; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Level; diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/DeflaterPool.java b/jetty-server/src/main/java/org/eclipse/jetty/server/DeflaterPool.java deleted file mode 100644 index 27f4070421f..00000000000 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/DeflaterPool.java +++ /dev/null @@ -1,129 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.server; - -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.zip.Deflater; - -public class DeflaterPool -{ - private final Queue _pool; - private final int _compressionLevel; - private final boolean _nowrap; - private final AtomicInteger _numDeflaters = new AtomicInteger(0); - private final int _capacity; - - - /** - * Create a Pool of {@link Deflater} instances. - * - * If given a capacity equal to zero the Deflaters will not be pooled - * and will be created on acquire and ended on release. - * If given a negative capacity equal to zero there will be no size restrictions on the DeflaterPool - * - * @param capacity maximum number of Deflaters which can be contained in the pool - * @param compressionLevel the default compression level for new Deflater objects - * @param nowrap if true then use GZIP compatible compression for all new Deflater objects - */ - public DeflaterPool(int capacity, int compressionLevel, boolean nowrap) - { - _capacity = capacity; - _compressionLevel = compressionLevel; - _nowrap = nowrap; - - if (_capacity != 0) - _pool = new ConcurrentLinkedQueue<>(); - else - _pool = null; - } - - protected Deflater newDeflater() - { - return new Deflater(_compressionLevel, _nowrap); - } - - /** - * @return Deflater taken from the pool if it is not empty or a newly created Deflater - */ - public Deflater acquire() - { - Deflater deflater; - - if (_capacity == 0) - deflater = newDeflater(); - else if (_capacity < 0) - { - deflater = _pool.poll(); - if (deflater == null) - deflater = newDeflater(); - } - else - { - deflater = _pool.poll(); - if (deflater == null) - deflater = newDeflater(); - else - _numDeflaters.decrementAndGet(); - } - - return deflater; - } - - /** - * @param deflater returns this Deflater to the pool or calls deflater.end() if the pool is full. - */ - public void release(Deflater deflater) - { - if (deflater == null) - return; - - if (_capacity == 0) - { - deflater.end(); - return; - } - else if (_capacity < 0) - { - deflater.reset(); - _pool.add(deflater); - } - else - { - while (true) - { - int d = _numDeflaters.get(); - - if (d >= _capacity) - { - deflater.end(); - break; - } - - if (_numDeflaters.compareAndSet(d, d + 1)) - { - deflater.reset(); - _pool.add(deflater); - break; - } - } - } - } -} diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java index 34f555c9c6a..9c9b58c8513 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java @@ -34,7 +34,7 @@ import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.http.*; import org.eclipse.jetty.http.pathmap.PathSpecSet; -import org.eclipse.jetty.server.DeflaterPool; +import org.eclipse.jetty.util.DeflaterPool; import org.eclipse.jetty.server.HttpOutput; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.HandlerWrapper; diff --git a/jetty-util/pom.xml b/jetty-util/pom.xml index d3fe35cd21a..6da82ecbfe2 100644 --- a/jetty-util/pom.xml +++ b/jetty-util/pom.xml @@ -91,6 +91,10 @@ provided true + + commons-pool + commons-pool +