Issue #5108 - use set based on ConcurrentHashMap and remove synchronized blocks

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-08-03 18:26:14 +10:00
parent d37501dce0
commit 85c4fc5335
2 changed files with 13 additions and 29 deletions

View File

@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.jsr356;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.websocket.Session;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
@ -28,38 +29,29 @@ import org.eclipse.jetty.util.component.LifeCycle;
public class JsrSessionTracker extends AbstractLifeCycle implements JsrSessionListener
{
private final Set<JsrSession> sessions = new HashSet<>();
private final Set<JsrSession> sessions = Collections.newSetFromMap(new ConcurrentHashMap<>());
public Set<Session> getSessions()
{
synchronized (this)
{
return Collections.unmodifiableSet(new HashSet<>(sessions));
}
return Collections.unmodifiableSet(new HashSet<>(sessions));
}
@Override
public void onSessionOpened(JsrSession session)
{
synchronized (this)
{
sessions.add(session);
}
sessions.add(session);
}
@Override
public void onSessionClosed(JsrSession session)
{
synchronized (this)
{
sessions.remove(session);
}
sessions.remove(session);
}
@Override
protected void doStop() throws Exception
{
for (Session session : getSessions())
for (Session session : sessions)
{
LifeCycle.stop(session);
}

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.component.Dumpable;
@ -29,40 +30,31 @@ import org.eclipse.jetty.util.component.LifeCycle;
public class SessionTracker extends AbstractLifeCycle implements WebSocketSessionListener, Dumpable
{
private final Set<WebSocketSession> sessions = new HashSet<>();
private final Set<WebSocketSession> sessions = Collections.newSetFromMap(new ConcurrentHashMap<>());
public Set<WebSocketSession> getSessions()
{
synchronized (this)
{
return Collections.unmodifiableSet(new HashSet<>(sessions));
}
return Collections.unmodifiableSet(new HashSet<>(sessions));
}
@Override
public void onSessionCreated(WebSocketSession session)
{
LifeCycle.start(session);
synchronized (this)
{
sessions.add(session);
}
sessions.add(session);
}
@Override
public void onSessionClosed(WebSocketSession session)
{
synchronized (this)
{
sessions.remove(session);
}
sessions.remove(session);
LifeCycle.stop(session);
}
@Override
protected void doStop() throws Exception
{
for (WebSocketSession session : getSessions())
for (WebSocketSession session : sessions)
{
LifeCycle.stop(session);
}
@ -72,6 +64,6 @@ public class SessionTracker extends AbstractLifeCycle implements WebSocketSessio
@Override
public void dump(Appendable out, String indent) throws IOException
{
Dumpable.dumpObjects(out, indent, this, getSessions());
Dumpable.dumpObjects(out, indent, this, sessions);
}
}