Mercurial Hosting > luan
changeset 1011:4e7208df7741
remove PooledBuffers
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 23 Oct 2016 23:59:08 -0600 |
parents | 2712133d5bce |
children | 8d0bdd357e6e |
files | src/org/eclipse/jetty/io/Buffers.java src/org/eclipse/jetty/io/PooledBuffers.java src/org/eclipse/jetty/io/SimpleBuffers.java src/org/eclipse/jetty/server/Connector.java src/org/eclipse/jetty/server/ssl/SslSelectChannelConnector.java |
diffstat | 5 files changed, 52 insertions(+), 341 deletions(-) [+] |
line wrap: on
line diff
--- a/src/org/eclipse/jetty/io/Buffers.java Sun Oct 23 22:23:50 2016 -0600 +++ b/src/org/eclipse/jetty/io/Buffers.java Sun Oct 23 23:59:08 2016 -0600 @@ -28,11 +28,9 @@ */ public interface Buffers { - enum Type { DIRECT, INDIRECT } ; - - Buffer getHeader(); - Buffer getBuffer(); - Buffer getBuffer(int size); - - void returnBuffer(Buffer buffer); + Buffer getHeader(); + Buffer getBuffer(); + Buffer getBuffer(int size); + + void returnBuffer(Buffer buffer); }
--- a/src/org/eclipse/jetty/io/PooledBuffers.java Sun Oct 23 22:23:50 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,182 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 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.io; - -import org.eclipse.jetty.io.nio.DirectNIOBuffer; -import org.eclipse.jetty.io.nio.IndirectNIOBuffer; -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.atomic.AtomicInteger; - - -public final class PooledBuffers implements Buffers -{ - private final Buffers.Type _headerType; - private final int _headerSize; - private final Buffers.Type _bufferType; - private final int _bufferSize; - - private final Queue<Buffer> _headers = new ConcurrentLinkedQueue<Buffer>(); - private final Queue<Buffer> _buffers = new ConcurrentLinkedQueue<Buffer>(); - private final Queue<Buffer> _others = new ConcurrentLinkedQueue<Buffer>(); - private final AtomicInteger _size = new AtomicInteger(); - private final int _maxSize; - private final boolean _otherHeaders; - - public PooledBuffers(Buffers.Type headerType, int headerSize, Buffers.Type bufferType, int bufferSize,int maxSize) - { - _headerType = headerType; - _headerSize = headerSize; - _bufferType = bufferType; - _bufferSize = bufferSize; - - _otherHeaders = headerType==bufferType; - _maxSize = maxSize; - } - - public Buffer getHeader() - { - Buffer buffer = _headers.poll(); - if (buffer==null) - buffer = newHeader(); - else - _size.decrementAndGet(); - return buffer; - } - - public Buffer getBuffer() - { - Buffer buffer = _buffers.poll(); - if (buffer==null) - buffer = newBuffer(_bufferSize); - else - _size.decrementAndGet(); - return buffer; - } - - public Buffer getBuffer(int size) - { - if (_otherHeaders && size==_headerSize) - return getHeader(); - if (size==_bufferSize) - return getBuffer(); - - // Look for an other buffer - Buffer buffer = _others.poll(); - - // consume all other buffers until one of the right size is found - while (buffer!=null && buffer.capacity()!=size) - { - _size.decrementAndGet(); - buffer = _others.poll(); - } - - if (buffer==null) - buffer = newBuffer(size); - else - _size.decrementAndGet(); - return buffer; - } - - public void returnBuffer(Buffer buffer) - { - buffer.clear(); - if (buffer.isVolatile() || buffer.isImmutable()) - return; - - if (_size.incrementAndGet() > _maxSize) - _size.decrementAndGet(); - else - { - if (isHeader(buffer)) - _headers.add(buffer); - else if (isBuffer(buffer)) - _buffers.add(buffer); - else - _others.add(buffer); - } - } - - @Override - public String toString() - { - return String.format("%s [%d/%d@%d,%d/%d@%d,%d/%d@-]", - getClass().getSimpleName(), - _headers.size(),_maxSize,_headerSize, - _buffers.size(),_maxSize,_bufferSize, - _others.size(),_maxSize); - } - - // from AbstractBuffers - - private Buffer newHeader() - { - switch(_headerType) - { - case DIRECT: - return new DirectNIOBuffer(_headerSize); - case INDIRECT: - return new IndirectNIOBuffer(_headerSize); - } - throw new IllegalStateException(); - } - - private Buffer newBuffer(int size) - { - switch(_bufferType) - { - case DIRECT: - return new DirectNIOBuffer(size); - case INDIRECT: - return new IndirectNIOBuffer(size); - } - throw new IllegalStateException(); - } - - private boolean isHeader(Buffer buffer) - { - if (buffer.capacity()==_headerSize) - { - switch(_headerType) - { - case DIRECT: - return buffer instanceof DirectNIOBuffer; - case INDIRECT: - return buffer instanceof IndirectNIOBuffer; - } - } - return false; - } - - private boolean isBuffer(Buffer buffer) - { - if (buffer.capacity()==_bufferSize) - { - switch(_bufferType) - { - case DIRECT: - return buffer instanceof DirectNIOBuffer; - case INDIRECT: - return buffer instanceof IndirectNIOBuffer; - } - } - return false; - } - -}
--- a/src/org/eclipse/jetty/io/SimpleBuffers.java Sun Oct 23 22:23:50 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,117 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 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.io; - -/* ------------------------------------------------------------ */ -/** SimpleBuffers. - * Simple implementation of Buffers holder. - * - * - */ -public class SimpleBuffers implements Buffers -{ - final Buffer _header; - final Buffer _buffer; - boolean _headerOut; - boolean _bufferOut; - - /* ------------------------------------------------------------ */ - /** - * - */ - public SimpleBuffers(Buffer header, Buffer buffer) - { - _header=header; - _buffer=buffer; - } - - /* ------------------------------------------------------------ */ - public Buffer getBuffer() - { - synchronized(this) - { - if (_buffer!=null && !_bufferOut) - { - _bufferOut=true; - return _buffer; - } - - if (_buffer!=null && _header!=null && _header.capacity()==_buffer.capacity() && !_headerOut) - { - _headerOut=true; - return _header; - } - - if (_buffer!=null) - return new ByteArrayBuffer(_buffer.capacity()); - return new ByteArrayBuffer(4096); - } - } - - /* ------------------------------------------------------------ */ - public Buffer getHeader() - { - synchronized(this) - { - if (_header!=null && !_headerOut) - { - _headerOut=true; - return _header; - } - - if (_buffer!=null && _header!=null && _header.capacity()==_buffer.capacity() && !_bufferOut) - { - _bufferOut=true; - return _buffer; - } - - if (_header!=null) - return new ByteArrayBuffer(_header.capacity()); - return new ByteArrayBuffer(4096); - } - } - - /* ------------------------------------------------------------ */ - public Buffer getBuffer(int size) - { - synchronized(this) - { - if (_header!=null && _header.capacity()==size) - return getHeader(); - if (_buffer!=null && _buffer.capacity()==size) - return getBuffer(); - return null; - } - } - - /* ------------------------------------------------------------ */ - public void returnBuffer(Buffer buffer) - { - synchronized(this) - { - buffer.clear(); - if (buffer==_header) - _headerOut=false; - if (buffer==_buffer) - _bufferOut=false; - } - } - - -}
--- a/src/org/eclipse/jetty/server/Connector.java Sun Oct 23 22:23:50 2016 -0600 +++ b/src/org/eclipse/jetty/server/Connector.java Sun Oct 23 23:59:08 2016 -0600 @@ -28,10 +28,12 @@ import javax.servlet.ServletRequest; +import org.eclipse.jetty.io.Buffer; import org.eclipse.jetty.io.Buffers; -import org.eclipse.jetty.io.PooledBuffers; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.EofException; +import org.eclipse.jetty.io.nio.DirectNIOBuffer; +import org.eclipse.jetty.io.nio.IndirectNIOBuffer; import org.eclipse.jetty.util.component.AggregateLifeCycle; import org.eclipse.jetty.util.component.Dumpable; import org.slf4j.Logger; @@ -132,8 +134,6 @@ if (server.isLowOnThreads()) LOG.warn("insufficient threads configured for {}",this); - startBuffers(); - LOG.info("Started {}",this); } @@ -151,8 +151,6 @@ LOG.warn("",e); } - stopBuffers(); - super.doStop(); } @@ -236,41 +234,51 @@ // from HttpBuffersImpl + protected int _requestHeaderSize = 6*1024; protected int _requestBufferSize = 16*1024; - protected int _requestHeaderSize = 6*1024; - private final int _responseBufferSize = 32*1024; private final int _responseHeaderSize = 6*1024; - private final int _maxBuffers = 1024; - - protected Buffers.Type _requestBufferType = Buffers.Type.DIRECT; - protected Buffers.Type _responseBufferType = Buffers.Type.DIRECT; + private final int _responseBufferSize = 32*1024; - private Buffers _requestBuffers; - private Buffers _responseBuffers; - - private void startBuffers() - throws Exception - { - _requestBuffers = new PooledBuffers(Buffers.Type.INDIRECT,_requestHeaderSize,_requestBufferType,_requestBufferSize,_maxBuffers); - _responseBuffers = new PooledBuffers(Buffers.Type.INDIRECT,_responseHeaderSize,_responseBufferType,_responseBufferSize,_maxBuffers); - super.doStart(); - } - - private void stopBuffers() - throws Exception - { - _requestBuffers = null; - _responseBuffers = null; - } - public final Buffers getRequestBuffers() { - return _requestBuffers; + return new MyBuffers(_requestHeaderSize,_requestBufferSize); } public final Buffers getResponseBuffers() { - return _responseBuffers; + return new MyBuffers(_responseHeaderSize,_responseBufferSize); + } + + + // my own buffers + + protected Buffer newBuffer(int size) { + return new DirectNIOBuffer(size); } + private class MyBuffers implements Buffers { + private final int headerSize; + private final int bufferSize; + + MyBuffers(int headerSize,int bufferSize) { + this.headerSize = headerSize; + this.bufferSize = bufferSize; + } + + public Buffer getHeader() { + return new IndirectNIOBuffer(headerSize); + } + + public Buffer getBuffer() { + return newBuffer(bufferSize); + } + + public Buffer getBuffer(int size) { + return newBuffer(size); + } + + public void returnBuffer(Buffer buffer) { + // like I care + } + } }
--- a/src/org/eclipse/jetty/server/ssl/SslSelectChannelConnector.java Sun Oct 23 22:23:50 2016 -0600 +++ b/src/org/eclipse/jetty/server/ssl/SslSelectChannelConnector.java Sun Oct 23 23:59:08 2016 -0600 @@ -28,13 +28,14 @@ import org.eclipse.jetty.http.HttpSchemes; import org.eclipse.jetty.io.AsyncEndPoint; +import org.eclipse.jetty.io.Buffer; import org.eclipse.jetty.io.Buffers; -import org.eclipse.jetty.io.Buffers.Type; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.RuntimeIOException; import org.eclipse.jetty.io.nio.AsyncConnection; import org.eclipse.jetty.io.nio.SslConnection; import org.eclipse.jetty.io.nio.SelectChannelEndPoint; +import org.eclipse.jetty.io.nio.IndirectNIOBuffer; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.AbstractHttpConnection; @@ -71,12 +72,15 @@ super(server,port); _sslContextFactory = sslContextFactory; addBean(_sslContextFactory); -// setUseDirectBuffers(false); - _requestBufferType = Buffers.Type.INDIRECT; - _responseBufferType = Buffers.Type.INDIRECT; setSoLingerTime(30000); } + @Override + protected Buffer newBuffer(int size) { + return new IndirectNIOBuffer(size); + } + + /* ------------------------------------------------------------ */ /** * Allow the Listener a chance to customise the request. before the server