ws service: optimize session storage
This also removes tracking sessions with no id
This commit is contained in:
@@ -24,8 +24,6 @@
|
||||
*/
|
||||
package net.runelite.http.service.account;
|
||||
|
||||
import net.runelite.http.service.account.beans.UserEntry;
|
||||
import net.runelite.http.service.account.beans.SessionEntry;
|
||||
import com.github.scribejava.apis.GoogleApi20;
|
||||
import com.github.scribejava.core.builder.ServiceBuilder;
|
||||
import com.github.scribejava.core.model.OAuth2AccessToken;
|
||||
@@ -42,9 +40,10 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import net.runelite.http.api.RuneLiteAPI;
|
||||
import net.runelite.http.api.account.OAuthResponse;
|
||||
import net.runelite.http.api.ws.messages.LoginResponse;
|
||||
import net.runelite.http.service.account.beans.SessionEntry;
|
||||
import net.runelite.http.service.account.beans.UserEntry;
|
||||
import net.runelite.http.service.ws.SessionManager;
|
||||
import net.runelite.http.service.ws.WSService;
|
||||
import net.runelite.http.service.ws.WSSession;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -233,15 +232,13 @@ public class AccountService
|
||||
|
||||
private void notifySession(UUID uuid, String username)
|
||||
{
|
||||
WSSession session = SessionManager.findSession(uuid);
|
||||
if (session == null)
|
||||
WSService service = SessionManager.findSession(uuid);
|
||||
if (service == null)
|
||||
{
|
||||
logger.info("Session {} logged in - but no websocket session", uuid);
|
||||
return;
|
||||
}
|
||||
|
||||
WSService service = session.getServlet();
|
||||
|
||||
LoginResponse response = new LoginResponse();
|
||||
response.setUsername(username);
|
||||
|
||||
|
||||
@@ -24,49 +24,39 @@
|
||||
*/
|
||||
package net.runelite.http.service.ws;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import javax.websocket.Session;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
public class SessionManager
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(SessionManager.class);
|
||||
private static final ConcurrentMap<UUID, WSService> sessions = new ConcurrentHashMap<>();
|
||||
|
||||
private static final Set<WSSession> sessions = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
public static void add(WSService service, Session session)
|
||||
public static void changeSessionUID(WSService service, UUID uuid)
|
||||
{
|
||||
WSSession wssession = new WSSession(service, session);
|
||||
|
||||
logger.info("Adding service {} session {}", service, session);
|
||||
|
||||
sessions.add(wssession);
|
||||
}
|
||||
|
||||
public static void remove(Session session)
|
||||
{
|
||||
WSSession wssession = new WSSession(null, session);
|
||||
sessions.remove(wssession);
|
||||
}
|
||||
|
||||
public static WSSession findSession(UUID uuid)
|
||||
{
|
||||
synchronized (sessions)
|
||||
synchronized (service)
|
||||
{
|
||||
for (WSSession session : sessions)
|
||||
remove(service);
|
||||
service.setUuid(uuid);
|
||||
sessions.put(uuid, service);
|
||||
}
|
||||
}
|
||||
|
||||
static void remove(WSService service)
|
||||
{
|
||||
synchronized (service)
|
||||
{
|
||||
UUID current = service.getUuid();
|
||||
if (current != null)
|
||||
{
|
||||
if (Objects.equal(session.getServlet().getUuid(), uuid))
|
||||
{
|
||||
return session;
|
||||
}
|
||||
sessions.remove(current);
|
||||
service.setUuid(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
public static WSService findSession(UUID uuid)
|
||||
{
|
||||
return sessions.get(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,9 +34,12 @@ import javax.websocket.OnMessage;
|
||||
import javax.websocket.OnOpen;
|
||||
import javax.websocket.Session;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import net.runelite.http.api.ws.messages.Handshake;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.runelite.http.api.ws.WebsocketGsonFactory;
|
||||
import net.runelite.http.api.ws.WebsocketMessage;
|
||||
import net.runelite.http.api.ws.messages.Handshake;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -48,13 +51,10 @@ public class WSService
|
||||
private static final Gson gson = WebsocketGsonFactory.build();
|
||||
|
||||
private Session session;
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
@Setter(AccessLevel.PACKAGE)
|
||||
private UUID uuid;
|
||||
|
||||
public UUID getUuid()
|
||||
{
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void send(WebsocketMessage message)
|
||||
{
|
||||
String json = gson.toJson(message, WebsocketMessage.class);
|
||||
@@ -68,21 +68,20 @@ public class WSService
|
||||
public void onOpen(Session session, EndpointConfig config)
|
||||
{
|
||||
this.session = session;
|
||||
SessionManager.add(this, session);
|
||||
logger.debug("New session {}", session);
|
||||
}
|
||||
|
||||
@OnClose
|
||||
public void onClose(Session session, CloseReason resaon)
|
||||
{
|
||||
SessionManager.remove(session);
|
||||
SessionManager.remove(this);
|
||||
logger.debug("Close session {}", session);
|
||||
}
|
||||
|
||||
@OnError
|
||||
public void onError(Session session, Throwable ex)
|
||||
{
|
||||
SessionManager.remove(session);
|
||||
SessionManager.remove(this);
|
||||
logger.debug("Error in session {}", session, ex);
|
||||
}
|
||||
|
||||
@@ -95,7 +94,7 @@ public class WSService
|
||||
if (message instanceof Handshake)
|
||||
{
|
||||
Handshake hs = (Handshake) message;
|
||||
uuid = hs.getSession();
|
||||
SessionManager.changeSessionUID(this, hs.getSession());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Adam <Adam@sigterm.info>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package net.runelite.http.service.ws;
|
||||
|
||||
import java.util.Objects;
|
||||
import javax.websocket.Session;
|
||||
|
||||
public class WSSession
|
||||
{
|
||||
private final WSService servlet;
|
||||
private final Session session;
|
||||
|
||||
public WSSession(WSService servlet, Session session)
|
||||
{
|
||||
this.servlet = servlet;
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
public WSService getServlet()
|
||||
{
|
||||
return servlet;
|
||||
}
|
||||
|
||||
public Session getSession()
|
||||
{
|
||||
return session;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
int hash = 3;
|
||||
hash = 29 * hash + Objects.hashCode(this.session);
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (this == obj)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (obj == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
final WSSession other = (WSSession) obj;
|
||||
if (!Objects.equals(this.session, other.session))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user