max idle time for websocket

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1116 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2009-12-02 03:38:07 +00:00
parent 76cab45109
commit 9dfabfb184
5 changed files with 82 additions and 20 deletions

View File

@ -17,10 +17,12 @@ import org.eclipse.jetty.io.nio.IndirectNIOBuffer;
*/
public class WebSocketBuffers
{
final private int _bufferSize;
final private ThreadLocalBuffers _buffers;
public WebSocketBuffers(final int bufferSize)
{
_bufferSize=bufferSize;
_buffers = new ThreadLocalBuffers()
{
@Override
@ -57,4 +59,9 @@ public class WebSocketBuffers
{
_buffers.returnBuffer(buffer);
}
public int getBufferSize()
{
return _bufferSize;
}
}

View File

@ -6,7 +6,6 @@ import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.nio.SelectChannelEndPoint;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.util.log.Log;
public class WebSocketConnection implements Connection, WebSocket.Outbound
@ -17,9 +16,9 @@ public class WebSocketConnection implements Connection, WebSocket.Outbound
final WebSocketGenerator _generator;
final long _timestamp;
final WebSocket _websocket;
final int _maxIdleTimeMs=30000;
final int _maxIdleTimeMs=300000;
public WebSocketConnection(WebSocketBuffers buffers, EndPoint endpoint, long timestamp, WebSocket websocket)
public WebSocketConnection(WebSocket websocket, EndPoint endpoint, WebSocketBuffers buffers, long timestamp, long maxIdleTime)
{
_endp = endpoint;
_timestamp = timestamp;
@ -62,22 +61,27 @@ public class WebSocketConnection implements Connection, WebSocket.Outbound
}
});
_idle = (_endp instanceof SelectChannelEndPoint) ?
new IdleCheck()
if (_endp instanceof SelectChannelEndPoint)
{
public void access(EndPoint endp)
final SelectChannelEndPoint scep=(SelectChannelEndPoint)_endp;
scep.cancelIdle();
_idle=new IdleCheck()
{
((SelectChannelEndPoint)_endp).scheduleIdle();
}
public void access(EndPoint endp)
{
scep.getSelectSet().scheduleTimeout(scep.getTimeoutTask(),_maxIdleTimeMs);
}
};
scep.getSelectSet().scheduleTimeout(scep.getTimeoutTask(),_maxIdleTimeMs);
}
:new IdleCheck()
else
{
public void access(EndPoint endp)
_idle = new IdleCheck()
{
}
};
public void access(EndPoint endp)
{}
};
}
}
public void handle() throws IOException

View File

@ -14,11 +14,58 @@ import org.eclipse.jetty.server.HttpConnection;
/* ------------------------------------------------------------ */
/** Factory to create WebSocket connections
*/
public class WebSocketFactory extends WebSocketBuffers
public class WebSocketFactory
{
private WebSocketBuffers _buffers;
private long _maxIdleTime=300000;
/* ------------------------------------------------------------ */
public WebSocketFactory()
{
_buffers=new WebSocketBuffers(8192);
}
/* ------------------------------------------------------------ */
public WebSocketFactory(int bufferSize)
{
super(bufferSize);
_buffers=new WebSocketBuffers(bufferSize);
}
/* ------------------------------------------------------------ */
/** Get the maxIdleTime.
* @return the maxIdleTime
*/
public long getMaxIdleTime()
{
return _maxIdleTime;
}
/* ------------------------------------------------------------ */
/** Set the maxIdleTime.
* @param maxIdleTime the maxIdleTime to set
*/
public void setMaxIdleTime(long maxIdleTime)
{
_maxIdleTime = maxIdleTime;
}
/* ------------------------------------------------------------ */
/** Get the bufferSize.
* @return the bufferSize
*/
public int getBufferSize()
{
return _buffers.getBufferSize();
}
/* ------------------------------------------------------------ */
/** Set the bufferSize.
* @param bufferSize the bufferSize to set
*/
public void setBufferSize(int bufferSize)
{
if (bufferSize!=getBufferSize())
_buffers=new WebSocketBuffers(bufferSize);
}
/* ------------------------------------------------------------ */
@ -44,7 +91,7 @@ public class WebSocketFactory extends WebSocketBuffers
HttpConnection http = HttpConnection.getCurrentConnection();
ConnectedEndPoint endp = (ConnectedEndPoint)http.getEndPoint();
WebSocketConnection connection = new WebSocketConnection(this,endp,http.getTimeStamp(),websocket);
WebSocketConnection connection = new WebSocketConnection(websocket,endp,_buffers,http.getTimeStamp(), _maxIdleTime);
String uri=request.getRequestURI();
String host=request.getHeader("Host");
@ -63,7 +110,5 @@ public class WebSocketFactory extends WebSocketBuffers
websocket.onConnect(connection);
throw new UpgradeConnectionException(connection);
}
}

View File

@ -22,6 +22,9 @@ import org.eclipse.jetty.server.HttpConnection;
* <p>
* The initParameter "bufferSize" can be used to set the buffer size,
* which is also the max frame byte size (default 8192).
* <p>
* The initParameter "maxIdleTime" can be used to set the time in ms
* that a websocket may be idle before closing (default 300,000).
*
*/
public abstract class WebSocketServlet extends HttpServlet
@ -37,6 +40,9 @@ public abstract class WebSocketServlet extends HttpServlet
{
String bs=getInitParameter("bufferSize");
_websocket = new WebSocketFactory(bs==null?8192:Integer.parseInt(bs));
String mit=getInitParameter("maxIdleTime");
if (mit!=null)
_websocket.setMaxIdleTime(Integer.parseInt(mit));
}
/* ------------------------------------------------------------ */

View File

@ -59,7 +59,7 @@ public class WebSocketTestClient extends AbstractLifeCycle
protected Connection newConnection(SocketChannel channel, SelectChannelEndPoint endpoint)
{
WebSocket ws=(WebSocket)endpoint.getSelectionKey().attachment();
WebSocketConnection connection = new WebSocketConnection(_buffers,endpoint,System.currentTimeMillis(),ws);
WebSocketConnection connection = new WebSocketConnection(ws,endpoint,_buffers,System.currentTimeMillis(), 30000);
// TODO Blocking upgrade code. Should be async
ByteArrayBuffer upgrade=new ByteArrayBuffer(