Skip to content

Commit

Permalink
Issue #5108 - use set based on ConcurrentHashMap and remove synchroni…
Browse files Browse the repository at this point in the history
…zed blocks

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Aug 3, 2020
1 parent d37501d commit 85c4fc5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
Expand Up @@ -21,45 +21,37 @@
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;
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);
}
Expand Down
Expand Up @@ -22,47 +22,39 @@
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;
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);
}
Expand All @@ -72,6 +64,6 @@ protected void doStop() throws Exception
@Override
public void dump(Appendable out, String indent) throws IOException
{
Dumpable.dumpObjects(out, indent, this, getSessions());
Dumpable.dumpObjects(out, indent, this, sessions);
}
}

0 comments on commit 85c4fc5

Please sign in to comment.