417022 - Access current HttpConnection from Request not ThreadLocal

+ Adjusting WebSocketServerFactory.upgrade() to receive HttpConnection
  from Request object instead of ThreadLocal.
This commit is contained in:
Joakim Erdfelt 2014-03-11 12:12:44 -07:00
parent 6802dbd75c
commit b95e454f7d
2 changed files with 13 additions and 6 deletions

View File

@ -18,6 +18,8 @@
package org.eclipse.jetty.websocket.jsr356.server; package org.eclipse.jetty.websocket.jsr356.server;
import static org.hamcrest.Matchers.*;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean; import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage; import java.lang.management.MemoryUsage;
@ -116,7 +118,7 @@ public class MemoryUsageTest
// Assume no more than 25 KiB per session pair (client and server). // Assume no more than 25 KiB per session pair (client and server).
long expected = 25 * 1024 * sessionCount; long expected = 25 * 1024 * sessionCount;
Assert.assertTrue(heapUsed < expected); Assert.assertThat("heap used", heapUsed,lessThan(expected));
} }
private static abstract class EndpointAdapter extends Endpoint implements MessageHandler.Whole<String> private static abstract class EndpointAdapter extends Endpoint implements MessageHandler.Whole<String>

View File

@ -164,9 +164,12 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
return false; return false;
} }
// Get the original HTTPConnection
HttpConnection connection = (HttpConnection)request.getAttribute("org.eclipse.jetty.server.HttpConnection");
// Send the upgrade // Send the upgrade
EventDriver driver = eventDriverFactory.wrap(websocketPojo); EventDriver driver = eventDriverFactory.wrap(websocketPojo);
return upgrade(sockreq, sockresp, driver); return upgrade(connection, sockreq, sockresp, driver);
} }
catch (URISyntaxException e) catch (URISyntaxException e)
{ {
@ -407,12 +410,13 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
* This method will not normally return, but will instead throw a UpgradeConnectionException, to exit HTTP handling and initiate WebSocket handling of the * This method will not normally return, but will instead throw a UpgradeConnectionException, to exit HTTP handling and initiate WebSocket handling of the
* connection. * connection.
* *
* @param http the raw http connection
* @param request The request to upgrade * @param request The request to upgrade
* @param response The response to upgrade * @param response The response to upgrade
* @param driver The websocket handler implementation to use * @param driver The websocket handler implementation to use
* @throws IOException * @throws IOException
*/ */
public boolean upgrade(ServletUpgradeRequest request, ServletUpgradeResponse response, EventDriver driver) throws IOException private boolean upgrade(HttpConnection http, ServletUpgradeRequest request, ServletUpgradeResponse response, EventDriver driver) throws IOException
{ {
if (!"websocket".equalsIgnoreCase(request.getHeader("Upgrade"))) if (!"websocket".equalsIgnoreCase(request.getHeader("Upgrade")))
{ {
@ -477,11 +481,12 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
extensionStack.negotiate(request.getExtensions()); extensionStack.negotiate(request.getExtensions());
} }
// Create connection // Get original HTTP connection
HttpConnection http = HttpConnection.getCurrentConnection();
EndPoint endp = http.getEndPoint(); EndPoint endp = http.getEndPoint();
Executor executor = http.getConnector().getExecutor(); Executor executor = http.getConnector().getExecutor();
ByteBufferPool bufferPool = http.getConnector().getByteBufferPool(); ByteBufferPool bufferPool = http.getConnector().getByteBufferPool();
// Setup websocket connection
WebSocketServerConnection wsConnection = new WebSocketServerConnection(endp, executor, scheduler, driver.getPolicy(), bufferPool); WebSocketServerConnection wsConnection = new WebSocketServerConnection(endp, executor, scheduler, driver.getPolicy(), bufferPool);
extensionStack.setPolicy(driver.getPolicy()); extensionStack.setPolicy(driver.getPolicy());
@ -526,7 +531,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
throw new IOException("Unable to start Extension Stack", e); throw new IOException("Unable to start Extension Stack", e);
} }
// Tell jetty about the new connection // Tell jetty about the new upgraded connection
request.setServletAttribute(HttpConnection.UPGRADE_CONNECTION_ATTRIBUTE, wsConnection); request.setServletAttribute(HttpConnection.UPGRADE_CONNECTION_ATTRIBUTE, wsConnection);
// Process (version specific) handshake response // Process (version specific) handshake response