Merge pull request #3836 from eclipse/jetty-9.4.x-3835-WebSocketSession-LifeCycle

Issue #3835 - ensure WebSocketSessions are always stopped
This commit is contained in:
Lachlan 2019-07-04 12:59:49 +10:00 committed by GitHub
commit d8164a1728
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 59 additions and 21 deletions

View File

@ -21,13 +21,18 @@ package org.eclipse.jetty.websocket.tests;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.common.WebSocketSession;
import org.eclipse.jetty.websocket.common.WebSocketSessionListener;
import org.eclipse.jetty.websocket.server.WebSocketServerFactory;
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
import org.junit.jupiter.api.AfterEach;
@ -45,6 +50,7 @@ public class ConcurrentConnectTest
private Server server;
private WebSocketClient client;
private URI uri;
private WebSocketServerFactory serverFactory;
@BeforeEach
public void start() throws Exception
@ -56,7 +62,17 @@ public class ConcurrentConnectTest
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.addServlet(MyWebSocketServlet.class, "/");
WebSocketServlet servlet = new WebSocketServlet() {
@Override
public void configure(WebSocketServletFactory factory)
{
factory.register(EventSocket.EchoSocket.class);
serverFactory = (WebSocketServerFactory)factory;
}
};
context.addServlet(new ServletHolder(servlet), "/");
server.setHandler(context);
server.start();
@ -78,6 +94,8 @@ public class ConcurrentConnectTest
public void testConcurrentConnect() throws Exception
{
List<EventSocket> listeners = new ArrayList();
CloseListener closeListener = new CloseListener();
client.addSessionListener(closeListener);
final int messages = MAX_CONNECTIONS;
for (int i = 0; i < messages; i++)
@ -113,14 +131,26 @@ public class ConcurrentConnectTest
assertThat(l.closeReason, is("close from client"));
//assertNull(l.failure); //TODO: we can get failures after close??
}
closeListener.closeLatch.await(5, TimeUnit.SECONDS);
for (EventSocket l : listeners)
{
assertTrue(((WebSocketSession)l.session).isStopped());
}
assertTrue(client.getOpenSessions().isEmpty());
assertTrue(client.getContainedBeans(WebSocketSession.class).isEmpty());
assertTrue(serverFactory.getContainedBeans(WebSocketSession.class).isEmpty());
}
public static class MyWebSocketServlet extends WebSocketServlet
public static class CloseListener implements WebSocketSessionListener
{
public CountDownLatch closeLatch = new CountDownLatch(MAX_CONNECTIONS);
@Override
public void configure(WebSocketServletFactory factory)
public void onSessionClosed(WebSocketSession session)
{
factory.register(EventSocket.EchoSocket.class);
closeLatch.countDown();
}
}
}

View File

@ -63,11 +63,6 @@ public class ClientOpenSessionTracker implements Connection.Listener, WebSocketS
this.closeConnectionLatch.countDown();
}
@Override
public void onSessionOpened(WebSocketSession session)
{
}
@Override
public void onSessionClosed(WebSocketSession session)
{

View File

@ -102,11 +102,6 @@ public class ClientSessionsTest
client.addSessionListener(new WebSocketSessionListener()
{
@Override
public void onSessionOpened(WebSocketSession session)
{
}
@Override
public void onSessionClosed(WebSocketSession session)
{
@ -134,7 +129,7 @@ public class ClientSessionsTest
assertThat("Session.upgradeRequest", sess.getUpgradeRequest(), notNullValue());
assertThat("Session.upgradeResponse", sess.getUpgradeResponse(), notNullValue());
Collection<WebSocketSession> sessions = client.getBeans(WebSocketSession.class);
Collection<WebSocketSession> sessions = client.getOpenSessions();
assertThat("client.connectionManager.sessions.size", sessions.size(), is(1));
RemoteEndpoint remote = sess.getRemote();

View File

@ -634,7 +634,6 @@ public class WebSocketUpgradeRequest extends HttpRequest implements CompleteList
session.addManaged(extensionStack);
session.setFuture(fut);
wsClient.addManaged(session);
if (upgradeListener != null)
{

View File

@ -18,14 +18,16 @@
package org.eclipse.jetty.websocket.common;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.component.Dumpable;
import org.eclipse.jetty.util.component.LifeCycle;
public class SessionTracker extends AbstractLifeCycle implements WebSocketSessionListener
public class SessionTracker extends AbstractLifeCycle implements WebSocketSessionListener, Dumpable
{
private CopyOnWriteArraySet<WebSocketSession> sessions = new CopyOnWriteArraySet<>();
@ -35,8 +37,9 @@ public class SessionTracker extends AbstractLifeCycle implements WebSocketSessio
}
@Override
public void onSessionOpened(WebSocketSession session)
public void onSessionCreated(WebSocketSession session)
{
LifeCycle.start(session);
sessions.add(session);
}
@ -44,6 +47,7 @@ public class SessionTracker extends AbstractLifeCycle implements WebSocketSessio
public void onSessionClosed(WebSocketSession session)
{
sessions.remove(session);
LifeCycle.stop(session);
}
@Override
@ -55,4 +59,10 @@ public class SessionTracker extends AbstractLifeCycle implements WebSocketSessio
}
super.doStop();
}
@Override
public void dump(Appendable out, String indent) throws IOException
{
Dumpable.dumpObjects(out, indent, this, sessions);
}
}

View File

@ -101,6 +101,8 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Rem
addBean(this.connection);
addBean(this.websocket);
notifySessionListeners(containerScope, (listener) -> listener.onSessionCreated(this));
}
/**

View File

@ -20,7 +20,15 @@ package org.eclipse.jetty.websocket.common;
public interface WebSocketSessionListener
{
void onSessionOpened(WebSocketSession session);
default void onSessionCreated(WebSocketSession session)
{
}
void onSessionClosed(WebSocketSession session);
default void onSessionOpened(WebSocketSession session)
{
}
default void onSessionClosed(WebSocketSession session)
{
}
}

View File

@ -672,7 +672,6 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
// Start Components
session.addManaged(extensionStack);
this.addManaged(session);
if (session.isFailed())
{