forked from External/mage
*Changed lock handling of session to prevent continuous locking.
This commit is contained in:
parent
638e42bc7f
commit
b0238b402e
2 changed files with 44 additions and 14 deletions
|
|
@ -73,7 +73,7 @@ public class ChatSession {
|
||||||
|
|
||||||
public void kill(UUID userId, DisconnectReason reason) {
|
public void kill(UUID userId, DisconnectReason reason) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (reason == null) {
|
if (reason == null) {
|
||||||
logger.fatal("User kill without disconnect reason userId: " + userId);
|
logger.fatal("User kill without disconnect reason userId: " + userId);
|
||||||
reason = DisconnectReason.Undefined;
|
reason = DisconnectReason.Undefined;
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,9 @@ import java.util.Date;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import mage.MageException;
|
import mage.MageException;
|
||||||
|
|
@ -63,11 +66,14 @@ public class Session {
|
||||||
private boolean isAdmin = false;
|
private boolean isAdmin = false;
|
||||||
private final AsynchInvokerCallbackHandler callbackHandler;
|
private final AsynchInvokerCallbackHandler callbackHandler;
|
||||||
|
|
||||||
|
private final ReentrantLock lock;
|
||||||
|
|
||||||
public Session(String sessionId, InvokerCallbackHandler callbackHandler) {
|
public Session(String sessionId, InvokerCallbackHandler callbackHandler) {
|
||||||
this.sessionId = sessionId;
|
this.sessionId = sessionId;
|
||||||
this.callbackHandler = (AsynchInvokerCallbackHandler) callbackHandler;
|
this.callbackHandler = (AsynchInvokerCallbackHandler) callbackHandler;
|
||||||
this.isAdmin = false;
|
this.isAdmin = false;
|
||||||
this.timeConnected = new Date();
|
this.timeConnected = new Date();
|
||||||
|
this.lock = new ReentrantLock();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String registerUser(String userName) throws MageException {
|
public String registerUser(String userName) throws MageException {
|
||||||
|
|
@ -205,34 +211,58 @@ public class Session {
|
||||||
}
|
}
|
||||||
|
|
||||||
// because different threads can activate this
|
// because different threads can activate this
|
||||||
synchronized public void userLostConnection() {
|
public void userLostConnection() {
|
||||||
User user = UserManager.getInstance().getUser(userId);
|
try {
|
||||||
if (user == null || !user.isConnected()) {
|
if(lock.tryLock(5, TimeUnit.SECONDS)) {
|
||||||
return; //user was already disconnected by other thread
|
User user = UserManager.getInstance().getUser(userId);
|
||||||
|
if (user == null || !user.isConnected()) {
|
||||||
|
return; //user was already disconnected by other thread
|
||||||
|
}
|
||||||
|
if (!user.getSessionId().equals(sessionId)) {
|
||||||
|
// user already reconnected with another instance
|
||||||
|
logger.info("OLD SESSION IGNORED - " + user.getName());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
logger.info("LOST CONNECTION - " + user.getName());
|
||||||
|
UserManager.getInstance().disconnect(userId, DisconnectReason.LostConnection);
|
||||||
|
} else {
|
||||||
|
logger.error("SESSION LOCK - userId " + userId);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException ex) {
|
||||||
|
logger.error("SESSION LOCK - userId " + userId, ex);
|
||||||
}
|
}
|
||||||
if (!user.getSessionId().equals(sessionId)) {
|
finally {
|
||||||
// user already reconnected with another instance
|
lock.unlock();
|
||||||
logger.info("OLD SESSION IGNORED - " + user.getName());
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
logger.info("LOST CONNECTION - " + user.getName());
|
|
||||||
UserManager.getInstance().disconnect(userId, DisconnectReason.LostConnection);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void kill(DisconnectReason reason) {
|
public void kill(DisconnectReason reason) {
|
||||||
UserManager.getInstance().removeUser(userId, reason);
|
UserManager.getInstance().removeUser(userId, reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized void fireCallback(final ClientCallback call) {
|
public void fireCallback(final ClientCallback call) {
|
||||||
try {
|
try {
|
||||||
call.setMessageId(messageId++);
|
boolean tryLock;
|
||||||
callbackHandler.handleCallbackOneway(new Callback(call));
|
if (lock.tryLock(5, TimeUnit.SECONDS)) {
|
||||||
|
call.setMessageId(messageId++);
|
||||||
|
callbackHandler.handleCallbackOneway(new Callback(call));
|
||||||
|
} else {
|
||||||
|
logger.error("CALLBACK LOCK - userId " + userId);
|
||||||
|
logger.error(" - method: " + call.getMethod());
|
||||||
|
}
|
||||||
} catch (HandleCallbackException ex) {
|
} catch (HandleCallbackException ex) {
|
||||||
logger.info("CALLBACK EXCEPTION - userId " + userId, ex);
|
logger.info("CALLBACK EXCEPTION - userId " + userId, ex);
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
userLostConnection();
|
userLostConnection();
|
||||||
|
} catch (InterruptedException ex) {
|
||||||
|
logger.error("CALLBACK LOCK EXCEPTION - userId " + userId, ex);
|
||||||
|
logger.error(" - method: " + call.getMethod());
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
lock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue