allow override of shared CompressionPools in WebSocketServerComponents

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-10-14 17:47:50 +11:00
parent 0e3cfe8fc2
commit 4690aa51ba
3 changed files with 20 additions and 12 deletions

View File

@ -20,7 +20,7 @@ package org.eclipse.jetty.util.compression;
import java.util.zip.Deflater;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.component.Container;
import org.eclipse.jetty.util.thread.ThreadPool;
public class DeflaterPool extends CompressionPool<Deflater>
@ -64,19 +64,19 @@ public class DeflaterPool extends CompressionPool<Deflater>
deflater.reset();
}
public static DeflaterPool ensurePool(ContainerLifeCycle containerLifeCycle)
public static DeflaterPool ensurePool(Container container)
{
DeflaterPool pool = containerLifeCycle.getBean(DeflaterPool.class);
DeflaterPool pool = container.getBean(DeflaterPool.class);
if (pool != null)
return pool;
int capacity = CompressionPool.DEFAULT_CAPACITY;
ThreadPool.SizedThreadPool threadPool = containerLifeCycle.getBean(ThreadPool.SizedThreadPool.class);
ThreadPool.SizedThreadPool threadPool = container.getBean(ThreadPool.SizedThreadPool.class);
if (threadPool != null)
capacity = threadPool.getMaxThreads();
pool = new DeflaterPool(capacity, Deflater.DEFAULT_COMPRESSION, true);
containerLifeCycle.addBean(pool);
container.addBean(pool);
return pool;
}
}

View File

@ -20,7 +20,7 @@ package org.eclipse.jetty.util.compression;
import java.util.zip.Inflater;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.component.Container;
import org.eclipse.jetty.util.thread.ThreadPool;
public class InflaterPool extends CompressionPool<Inflater>
@ -61,19 +61,19 @@ public class InflaterPool extends CompressionPool<Inflater>
inflater.reset();
}
public static InflaterPool ensurePool(ContainerLifeCycle containerLifeCycle)
public static InflaterPool ensurePool(Container container)
{
InflaterPool pool = containerLifeCycle.getBean(InflaterPool.class);
InflaterPool pool = container.getBean(InflaterPool.class);
if (pool != null)
return pool;
int capacity = CompressionPool.DEFAULT_CAPACITY;
ThreadPool.SizedThreadPool threadPool = containerLifeCycle.getBean(ThreadPool.SizedThreadPool.class);
ThreadPool.SizedThreadPool threadPool = container.getBean(ThreadPool.SizedThreadPool.class);
if (threadPool != null)
capacity = threadPool.getMaxThreads();
pool = new InflaterPool(capacity, true);
containerLifeCycle.addBean(pool);
container.addBean(pool);
return pool;
}
}

View File

@ -38,6 +38,8 @@ import org.eclipse.jetty.websocket.core.WebSocketExtensionRegistry;
public class WebSocketServerComponents extends WebSocketComponents
{
public static final String WEBSOCKET_COMPONENTS_ATTRIBUTE = WebSocketComponents.class.getName();
public static final String WEBSOCKET_INFLATER_POOL_ATTRIBUTE = "jetty.websocket.inflater";
public static final String WEBSOCKET_DEFLATER_POOL_ATTRIBUTE = "jetty.websocket.deflater";
WebSocketServerComponents(InflaterPool inflaterPool, DeflaterPool deflaterPool)
{
@ -49,8 +51,14 @@ public class WebSocketServerComponents extends WebSocketComponents
WebSocketComponents components = server.getBean(WebSocketComponents.class);
if (components == null)
{
InflaterPool inflaterPool = InflaterPool.ensurePool(server);
DeflaterPool deflaterPool = DeflaterPool.ensurePool(server);
InflaterPool inflaterPool = (InflaterPool)servletContext.getAttribute(WEBSOCKET_INFLATER_POOL_ATTRIBUTE);
if (inflaterPool == null)
inflaterPool = InflaterPool.ensurePool(server);
DeflaterPool deflaterPool = (DeflaterPool)servletContext.getAttribute(WEBSOCKET_DEFLATER_POOL_ATTRIBUTE);
if (deflaterPool == null)
deflaterPool = DeflaterPool.ensurePool(server);
components = new WebSocketServerComponents(inflaterPool, deflaterPool);
server.addBean(components);
}