Merge pull request #3771 from eclipse/jetty-9.4.x-300-CompressionPool
Issue #300 - Deflater / Inflater Object Pool
This commit is contained in:
commit
4fb596cf1c
|
@ -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.compression.DeflaterPool;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Level;
|
||||
|
|
|
@ -1,128 +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<Deflater> _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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -26,6 +26,7 @@ import java.util.ListIterator;
|
|||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
import javax.servlet.DispatcherType;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
|
@ -40,7 +41,6 @@ import org.eclipse.jetty.http.HttpMethod;
|
|||
import org.eclipse.jetty.http.MimeTypes;
|
||||
import org.eclipse.jetty.http.PreEncodedHttpField;
|
||||
import org.eclipse.jetty.http.pathmap.PathSpecSet;
|
||||
import org.eclipse.jetty.server.DeflaterPool;
|
||||
import org.eclipse.jetty.server.HttpOutput;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.server.handler.HandlerWrapper;
|
||||
|
@ -48,6 +48,7 @@ import org.eclipse.jetty.util.IncludeExclude;
|
|||
import org.eclipse.jetty.util.RegexSet;
|
||||
import org.eclipse.jetty.util.StringUtil;
|
||||
import org.eclipse.jetty.util.URIUtil;
|
||||
import org.eclipse.jetty.util.compression.DeflaterPool;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
|
||||
|
|
|
@ -0,0 +1,128 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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.util.compression;
|
||||
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.eclipse.jetty.util.component.AbstractLifeCycle;
|
||||
|
||||
public abstract class CompressionPool<T> extends AbstractLifeCycle
|
||||
{
|
||||
public static final int INFINITE_CAPACITY = -1;
|
||||
|
||||
private final Queue<T> _pool;
|
||||
private final AtomicInteger _numObjects = new AtomicInteger(0);
|
||||
private final int _capacity;
|
||||
|
||||
/**
|
||||
* Create a Pool of {@link T} instances.
|
||||
*
|
||||
* If given a capacity equal to zero the Objects 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 Pool
|
||||
*
|
||||
* @param capacity maximum number of Objects which can be contained in the pool
|
||||
*/
|
||||
public CompressionPool(int capacity)
|
||||
{
|
||||
_capacity = capacity;
|
||||
_pool = (_capacity == 0) ? null : new ConcurrentLinkedQueue<>();
|
||||
}
|
||||
|
||||
abstract protected T newObject();
|
||||
|
||||
abstract protected void end(T object);
|
||||
|
||||
abstract protected void reset(T object);
|
||||
|
||||
/**
|
||||
* @return Object taken from the pool if it is not empty or a newly created Object
|
||||
*/
|
||||
public T acquire()
|
||||
{
|
||||
T object;
|
||||
|
||||
if (_capacity == 0)
|
||||
object = newObject();
|
||||
else
|
||||
{
|
||||
object = _pool.poll();
|
||||
if (object == null)
|
||||
object = newObject();
|
||||
else if (_capacity > 0)
|
||||
_numObjects.decrementAndGet();
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param object returns this Object to the pool or calls {@link #end(Object)} if the pool is full.
|
||||
*/
|
||||
public void release(T object)
|
||||
{
|
||||
if (object == null)
|
||||
return;
|
||||
|
||||
if (_capacity == 0 || !isRunning())
|
||||
{
|
||||
end(object);
|
||||
return;
|
||||
}
|
||||
else if (_capacity < 0)
|
||||
{
|
||||
reset(object);
|
||||
_pool.add(object);
|
||||
}
|
||||
else
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
int d = _numObjects.get();
|
||||
|
||||
if (d >= _capacity)
|
||||
{
|
||||
end(object);
|
||||
break;
|
||||
}
|
||||
|
||||
if (_numObjects.compareAndSet(d, d + 1))
|
||||
{
|
||||
reset(object);
|
||||
_pool.add(object);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doStop()
|
||||
{
|
||||
T t = _pool.poll();
|
||||
while (t != null)
|
||||
{
|
||||
end(t);
|
||||
t = _pool.poll();
|
||||
}
|
||||
_numObjects.set(0);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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.util.compression;
|
||||
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
public class DeflaterPool extends CompressionPool<Deflater>
|
||||
{
|
||||
private final int compressionLevel;
|
||||
private final boolean nowrap;
|
||||
|
||||
/**
|
||||
* Create a Pool of {@link Deflater} instances.
|
||||
* <p>
|
||||
* 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)
|
||||
{
|
||||
super(capacity);
|
||||
this.compressionLevel = compressionLevel;
|
||||
this.nowrap = nowrap;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Deflater newObject()
|
||||
{
|
||||
return new Deflater(compressionLevel, nowrap);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void end(Deflater deflater)
|
||||
{
|
||||
deflater.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reset(Deflater deflater)
|
||||
{
|
||||
deflater.reset();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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.util.compression;
|
||||
|
||||
import java.util.zip.Inflater;
|
||||
|
||||
public class InflaterPool extends CompressionPool<Inflater>
|
||||
{
|
||||
private final boolean nowrap;
|
||||
|
||||
/**
|
||||
* Create a Pool of {@link Inflater} instances.
|
||||
* <p>
|
||||
* If given a capacity equal to zero the Inflaters 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 InflaterPool
|
||||
*
|
||||
* @param capacity maximum number of Inflaters which can be contained in the pool
|
||||
* @param nowrap if true then use GZIP compatible compression for all new Inflater objects
|
||||
*/
|
||||
public InflaterPool(int capacity, boolean nowrap)
|
||||
{
|
||||
super(capacity);
|
||||
this.nowrap = nowrap;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Inflater newObject()
|
||||
{
|
||||
return new Inflater(nowrap);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void end(Inflater inflater)
|
||||
{
|
||||
inflater.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reset(Inflater inflater)
|
||||
{
|
||||
inflater.reset();
|
||||
}
|
||||
}
|
|
@ -19,6 +19,7 @@
|
|||
package org.eclipse.jetty.websocket.tests;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.channels.ClosedChannelException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
|
@ -108,7 +109,7 @@ public class WriteAfterStopTest
|
|||
|
||||
try (StacklessLogging stacklessLogging = new StacklessLogging(CompressExtension.class))
|
||||
{
|
||||
assertThrows(NullPointerException.class,
|
||||
assertThrows(ClosedChannelException.class,
|
||||
() -> session.getRemote().sendString("hello world"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,64 +18,22 @@
|
|||
|
||||
package org.eclipse.jetty.websocket.api.extensions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class ExtensionFactory implements Iterable<Class<? extends Extension>>
|
||||
public interface ExtensionFactory extends Iterable<Class<? extends Extension>>
|
||||
{
|
||||
private ServiceLoader<Extension> extensionLoader = ServiceLoader.load(Extension.class);
|
||||
private Map<String, Class<? extends Extension>> availableExtensions;
|
||||
Map<String, Class<? extends Extension>> getAvailableExtensions();
|
||||
|
||||
public ExtensionFactory()
|
||||
{
|
||||
availableExtensions = new HashMap<>();
|
||||
for (Extension ext : extensionLoader)
|
||||
{
|
||||
if (ext != null)
|
||||
{
|
||||
availableExtensions.put(ext.getName(), ext.getClass());
|
||||
}
|
||||
}
|
||||
}
|
||||
Class<? extends Extension> getExtension(String name);
|
||||
|
||||
public Map<String, Class<? extends Extension>> getAvailableExtensions()
|
||||
{
|
||||
return availableExtensions;
|
||||
}
|
||||
Set<String> getExtensionNames();
|
||||
|
||||
public Class<? extends Extension> getExtension(String name)
|
||||
{
|
||||
return availableExtensions.get(name);
|
||||
}
|
||||
boolean isAvailable(String name);
|
||||
|
||||
public Set<String> getExtensionNames()
|
||||
{
|
||||
return availableExtensions.keySet();
|
||||
}
|
||||
Extension newInstance(ExtensionConfig config);
|
||||
|
||||
public boolean isAvailable(String name)
|
||||
{
|
||||
return availableExtensions.containsKey(name);
|
||||
}
|
||||
void register(String name, Class<? extends Extension> extension);
|
||||
|
||||
@Override
|
||||
public Iterator<Class<? extends Extension>> iterator()
|
||||
{
|
||||
return availableExtensions.values().iterator();
|
||||
}
|
||||
|
||||
public abstract Extension newInstance(ExtensionConfig config);
|
||||
|
||||
public void register(String name, Class<? extends Extension> extension)
|
||||
{
|
||||
availableExtensions.put(name, extension);
|
||||
}
|
||||
|
||||
public void unregister(String name)
|
||||
{
|
||||
availableExtensions.remove(name);
|
||||
}
|
||||
void unregister(String name);
|
||||
}
|
||||
|
|
|
@ -249,6 +249,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
|
|||
// Support Late Binding of Object Factory (for CDI)
|
||||
this.objectFactorySupplier = () -> scope.getObjectFactory();
|
||||
this.extensionRegistry = new WebSocketExtensionFactory(this);
|
||||
addBean(extensionRegistry);
|
||||
|
||||
this.eventDriverFactory = eventDriverFactory == null ? new EventDriverFactory(this) : eventDriverFactory;
|
||||
this.sessionFactory = sessionFactory == null ? new WebSocketSessionFactory(this) : sessionFactory;
|
||||
|
@ -365,6 +366,8 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
|
|||
|
||||
ShutdownThread.deregister(this);
|
||||
|
||||
sessionTracker.stop();
|
||||
extensionRegistry.stop();
|
||||
super.doStop();
|
||||
|
||||
if (LOG.isDebugEnabled())
|
||||
|
|
|
@ -18,21 +18,69 @@
|
|||
|
||||
package org.eclipse.jetty.websocket.common.extensions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.Set;
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
import org.eclipse.jetty.util.StringUtil;
|
||||
import org.eclipse.jetty.util.component.ContainerLifeCycle;
|
||||
import org.eclipse.jetty.util.compression.CompressionPool;
|
||||
import org.eclipse.jetty.util.compression.DeflaterPool;
|
||||
import org.eclipse.jetty.util.compression.InflaterPool;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketException;
|
||||
import org.eclipse.jetty.websocket.api.extensions.Extension;
|
||||
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
|
||||
import org.eclipse.jetty.websocket.api.extensions.ExtensionFactory;
|
||||
import org.eclipse.jetty.websocket.common.extensions.compress.CompressExtension;
|
||||
import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
|
||||
|
||||
public class WebSocketExtensionFactory extends ExtensionFactory
|
||||
public class WebSocketExtensionFactory extends ContainerLifeCycle implements ExtensionFactory
|
||||
{
|
||||
private WebSocketContainerScope container;
|
||||
private ServiceLoader<Extension> extensionLoader = ServiceLoader.load(Extension.class);
|
||||
private Map<String, Class<? extends Extension>> availableExtensions;
|
||||
private final InflaterPool inflaterPool = new InflaterPool(CompressionPool.INFINITE_CAPACITY, true);
|
||||
private final DeflaterPool deflaterPool = new DeflaterPool(CompressionPool.INFINITE_CAPACITY, Deflater.DEFAULT_COMPRESSION, true);
|
||||
|
||||
public WebSocketExtensionFactory(WebSocketContainerScope container)
|
||||
{
|
||||
super();
|
||||
availableExtensions = new HashMap<>();
|
||||
for (Extension ext : extensionLoader)
|
||||
{
|
||||
if (ext != null)
|
||||
availableExtensions.put(ext.getName(),ext.getClass());
|
||||
}
|
||||
|
||||
this.container = container;
|
||||
addBean(inflaterPool);
|
||||
addBean(deflaterPool);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Class<? extends Extension>> getAvailableExtensions()
|
||||
{
|
||||
return availableExtensions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends Extension> getExtension(String name)
|
||||
{
|
||||
return availableExtensions.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getExtensionNames()
|
||||
{
|
||||
return availableExtensions.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(String name)
|
||||
{
|
||||
return availableExtensions.containsKey(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -64,6 +112,13 @@ public class WebSocketExtensionFactory extends ExtensionFactory
|
|||
aext.init(container);
|
||||
aext.setConfig(config);
|
||||
}
|
||||
if (ext instanceof CompressExtension)
|
||||
{
|
||||
CompressExtension cext = (CompressExtension)ext;
|
||||
cext.setInflaterPool(inflaterPool);
|
||||
cext.setDeflaterPool(deflaterPool);
|
||||
}
|
||||
|
||||
return ext;
|
||||
}
|
||||
catch (Exception e)
|
||||
|
@ -71,4 +126,22 @@ public class WebSocketExtensionFactory extends ExtensionFactory
|
|||
throw new WebSocketException("Cannot instantiate extension: " + extClass, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(String name, Class<? extends Extension> extension)
|
||||
{
|
||||
availableExtensions.put(name,extension);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregister(String name)
|
||||
{
|
||||
availableExtensions.remove(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Class<? extends Extension>> iterator()
|
||||
{
|
||||
return availableExtensions.values().iterator();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,6 +30,8 @@ import java.util.zip.ZipException;
|
|||
|
||||
import org.eclipse.jetty.util.BufferUtil;
|
||||
import org.eclipse.jetty.util.IteratingCallback;
|
||||
import org.eclipse.jetty.util.compression.DeflaterPool;
|
||||
import org.eclipse.jetty.util.compression.InflaterPool;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
import org.eclipse.jetty.websocket.api.BatchMode;
|
||||
|
@ -84,10 +86,10 @@ public abstract class CompressExtension extends AbstractExtension
|
|||
*/
|
||||
private static final int DECOMPRESS_BUF_SIZE = 8 * 1024;
|
||||
|
||||
private static final boolean NOWRAP = true;
|
||||
|
||||
private final Queue<FrameEntry> entries = new ArrayDeque<>();
|
||||
private final IteratingCallback flusher = new Flusher();
|
||||
private DeflaterPool deflaterPool;
|
||||
private InflaterPool inflaterPool;
|
||||
private Deflater deflaterImpl;
|
||||
private Inflater inflaterImpl;
|
||||
protected AtomicInteger decompressCount = new AtomicInteger(0);
|
||||
|
@ -100,11 +102,21 @@ public abstract class CompressExtension extends AbstractExtension
|
|||
rsvUse = getRsvUseMode();
|
||||
}
|
||||
|
||||
public void setInflaterPool(InflaterPool inflaterPool)
|
||||
{
|
||||
this.inflaterPool = inflaterPool;
|
||||
}
|
||||
|
||||
public void setDeflaterPool(DeflaterPool deflaterPool)
|
||||
{
|
||||
this.deflaterPool = deflaterPool;
|
||||
}
|
||||
|
||||
public Deflater getDeflater()
|
||||
{
|
||||
if (deflaterImpl == null)
|
||||
{
|
||||
deflaterImpl = new Deflater(Deflater.DEFAULT_COMPRESSION, NOWRAP);
|
||||
deflaterImpl = deflaterPool.acquire();
|
||||
}
|
||||
return deflaterImpl;
|
||||
}
|
||||
|
@ -113,7 +125,7 @@ public abstract class CompressExtension extends AbstractExtension
|
|||
{
|
||||
if (inflaterImpl == null)
|
||||
{
|
||||
inflaterImpl = new Inflater(NOWRAP);
|
||||
inflaterImpl = inflaterPool.acquire();
|
||||
}
|
||||
return inflaterImpl;
|
||||
}
|
||||
|
@ -389,9 +401,17 @@ public abstract class CompressExtension extends AbstractExtension
|
|||
protected void doStop() throws Exception
|
||||
{
|
||||
if (deflaterImpl != null)
|
||||
deflaterImpl.end();
|
||||
{
|
||||
deflaterPool.release(deflaterImpl);
|
||||
deflaterImpl = null;
|
||||
}
|
||||
|
||||
if (inflaterImpl != null)
|
||||
inflaterImpl.end();
|
||||
{
|
||||
inflaterPool.release(inflaterImpl);
|
||||
inflaterImpl = null;
|
||||
}
|
||||
|
||||
super.doStop();
|
||||
}
|
||||
|
||||
|
|
|
@ -34,6 +34,9 @@ import org.eclipse.jetty.io.RuntimeIOException;
|
|||
import org.eclipse.jetty.util.BufferUtil;
|
||||
import org.eclipse.jetty.util.StringUtil;
|
||||
import org.eclipse.jetty.util.TypeUtil;
|
||||
import org.eclipse.jetty.util.compression.CompressionPool;
|
||||
import org.eclipse.jetty.util.compression.DeflaterPool;
|
||||
import org.eclipse.jetty.util.compression.InflaterPool;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
import org.eclipse.jetty.websocket.api.BatchMode;
|
||||
|
@ -68,6 +71,8 @@ public class DeflateFrameExtensionTest extends AbstractExtensionTest
|
|||
private static final Logger LOG = Log.getLogger(DeflateFrameExtensionTest.class);
|
||||
|
||||
public ByteBufferPool bufferPool = new MappedByteBufferPool();
|
||||
public DeflaterPool deflaterPool = new DeflaterPool(CompressionPool.INFINITE_CAPACITY, Deflater.DEFAULT_COMPRESSION, true);
|
||||
public InflaterPool inflaterPool = new InflaterPool(CompressionPool.INFINITE_CAPACITY, true);
|
||||
|
||||
private void assertIncoming(byte[] raw, String... expectedTextDatas)
|
||||
{
|
||||
|
@ -75,6 +80,8 @@ public class DeflateFrameExtensionTest extends AbstractExtensionTest
|
|||
|
||||
DeflateFrameExtension ext = new DeflateFrameExtension();
|
||||
ext.setBufferPool(bufferPool);
|
||||
ext.setDeflaterPool(deflaterPool);
|
||||
ext.setInflaterPool(inflaterPool);
|
||||
ext.setPolicy(policy);
|
||||
|
||||
ExtensionConfig config = ExtensionConfig.parse("deflate-frame");
|
||||
|
@ -119,6 +126,8 @@ public class DeflateFrameExtensionTest extends AbstractExtensionTest
|
|||
|
||||
DeflateFrameExtension ext = new DeflateFrameExtension();
|
||||
ext.setBufferPool(bufferPool);
|
||||
ext.setDeflaterPool(deflaterPool);
|
||||
ext.setInflaterPool(inflaterPool);
|
||||
ext.setPolicy(policy);
|
||||
|
||||
ExtensionConfig config = ExtensionConfig.parse("deflate-frame");
|
||||
|
@ -251,6 +260,8 @@ public class DeflateFrameExtensionTest extends AbstractExtensionTest
|
|||
{
|
||||
ext.setConfig(new ExtensionConfig(ext.getName()));
|
||||
ext.setBufferPool(bufferPool);
|
||||
ext.setDeflaterPool(deflaterPool);
|
||||
ext.setInflaterPool(inflaterPool);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -303,6 +314,8 @@ public class DeflateFrameExtensionTest extends AbstractExtensionTest
|
|||
|
||||
DeflateFrameExtension ext = new DeflateFrameExtension();
|
||||
ext.setBufferPool(bufferPool);
|
||||
ext.setDeflaterPool(deflaterPool);
|
||||
ext.setInflaterPool(inflaterPool);
|
||||
ext.setPolicy(policy);
|
||||
ext.setConfig(new ExtensionConfig(ext.getName()));
|
||||
|
||||
|
@ -397,6 +410,8 @@ public class DeflateFrameExtensionTest extends AbstractExtensionTest
|
|||
|
||||
DeflateFrameExtension clientExtension = new DeflateFrameExtension();
|
||||
clientExtension.setBufferPool(bufferPool);
|
||||
clientExtension.setDeflaterPool(deflaterPool);
|
||||
clientExtension.setInflaterPool(inflaterPool);
|
||||
clientExtension.setPolicy(WebSocketPolicy.newClientPolicy());
|
||||
clientExtension.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
|
||||
clientExtension.getPolicy().setMaxBinaryMessageBufferSize(maxMessageSize);
|
||||
|
@ -404,6 +419,8 @@ public class DeflateFrameExtensionTest extends AbstractExtensionTest
|
|||
|
||||
final DeflateFrameExtension serverExtension = new DeflateFrameExtension();
|
||||
serverExtension.setBufferPool(bufferPool);
|
||||
serverExtension.setDeflaterPool(deflaterPool);
|
||||
serverExtension.setInflaterPool(inflaterPool);
|
||||
serverExtension.setPolicy(WebSocketPolicy.newServerPolicy());
|
||||
serverExtension.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
|
||||
serverExtension.getPolicy().setMaxBinaryMessageBufferSize(maxMessageSize);
|
||||
|
|
|
@ -24,12 +24,16 @@ import java.nio.charset.StandardCharsets;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
import org.eclipse.jetty.io.ByteBufferPool;
|
||||
import org.eclipse.jetty.io.MappedByteBufferPool;
|
||||
import org.eclipse.jetty.toolchain.test.ByteBufferAssert;
|
||||
import org.eclipse.jetty.util.BufferUtil;
|
||||
import org.eclipse.jetty.util.TypeUtil;
|
||||
import org.eclipse.jetty.util.compression.CompressionPool;
|
||||
import org.eclipse.jetty.util.compression.DeflaterPool;
|
||||
import org.eclipse.jetty.util.compression.InflaterPool;
|
||||
import org.eclipse.jetty.websocket.api.BatchMode;
|
||||
import org.eclipse.jetty.websocket.api.ProtocolException;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
|
||||
|
@ -58,6 +62,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
|||
public class PerMessageDeflateExtensionTest extends AbstractExtensionTest
|
||||
{
|
||||
public ByteBufferPool bufferPool = new MappedByteBufferPool();
|
||||
public DeflaterPool deflaterPool = new DeflaterPool(CompressionPool.INFINITE_CAPACITY, Deflater.DEFAULT_COMPRESSION, true);
|
||||
public InflaterPool inflaterPool = new InflaterPool(CompressionPool.INFINITE_CAPACITY, true);
|
||||
|
||||
private void assertEndsWithTail(String hexStr, boolean expectedResult)
|
||||
{
|
||||
|
@ -284,6 +290,8 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest
|
|||
{
|
||||
PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
|
||||
ext.setBufferPool(bufferPool);
|
||||
ext.setDeflaterPool(deflaterPool);
|
||||
ext.setInflaterPool(inflaterPool);
|
||||
ext.setPolicy(WebSocketPolicy.newServerPolicy());
|
||||
ExtensionConfig config = ExtensionConfig.parse("permessage-deflate");
|
||||
ext.setConfig(config);
|
||||
|
@ -321,6 +329,8 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest
|
|||
{
|
||||
PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
|
||||
ext.setBufferPool(bufferPool);
|
||||
ext.setDeflaterPool(deflaterPool);
|
||||
ext.setInflaterPool(inflaterPool);
|
||||
ext.setPolicy(WebSocketPolicy.newServerPolicy());
|
||||
ExtensionConfig config = ExtensionConfig.parse("permessage-deflate");
|
||||
ext.setConfig(config);
|
||||
|
@ -358,6 +368,8 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest
|
|||
{
|
||||
PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
|
||||
ext.setBufferPool(bufferPool);
|
||||
ext.setDeflaterPool(deflaterPool);
|
||||
ext.setInflaterPool(inflaterPool);
|
||||
ext.setPolicy(WebSocketPolicy.newServerPolicy());
|
||||
ExtensionConfig config = ExtensionConfig.parse("permessage-deflate");
|
||||
ext.setConfig(config);
|
||||
|
@ -415,6 +427,8 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest
|
|||
{
|
||||
PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
|
||||
ext.setBufferPool(bufferPool);
|
||||
ext.setDeflaterPool(deflaterPool);
|
||||
ext.setInflaterPool(inflaterPool);
|
||||
ext.setPolicy(WebSocketPolicy.newServerPolicy());
|
||||
ExtensionConfig config = ExtensionConfig.parse("permessage-deflate");
|
||||
ext.setConfig(config);
|
||||
|
@ -456,6 +470,8 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest
|
|||
{
|
||||
PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
|
||||
ext.setBufferPool(bufferPool);
|
||||
ext.setDeflaterPool(deflaterPool);
|
||||
ext.setInflaterPool(inflaterPool);
|
||||
ext.setPolicy(WebSocketPolicy.newServerPolicy());
|
||||
ExtensionConfig config = ExtensionConfig.parse("permessage-deflate");
|
||||
ext.setConfig(config);
|
||||
|
|
|
@ -186,6 +186,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
addBean(scheduler);
|
||||
addBean(bufferPool);
|
||||
addBean(sessionTracker);
|
||||
addBean(extensionFactory);
|
||||
listeners.add(this.sessionTracker);
|
||||
}
|
||||
|
||||
|
@ -337,6 +338,14 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
super.doStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doStop() throws Exception
|
||||
{
|
||||
sessionTracker.stop();
|
||||
extensionFactory.stop();
|
||||
super.doStop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to find the DecoratedObjectFactory that should be used.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue