Skip to content

Commit

Permalink
Added logic to suppress failing callbacks to users that lost connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
LevelX2 committed Jan 28, 2018
1 parent 34a8845 commit 7f17011
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 28 deletions.
40 changes: 13 additions & 27 deletions Mage.Server/src/main/java/mage/server/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
package mage.server;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -66,7 +65,6 @@ public class Session {
private final Date timeConnected;
private boolean isAdmin = false;
private final AsynchInvokerCallbackHandler callbackHandler;
private boolean error = false;

private final ReentrantLock lock;

Expand Down Expand Up @@ -348,42 +346,30 @@ public void userLostConnection() {
}
}

public void kill(DisconnectReason reason) {
boolean lockSet = false;
try {
if (lock.tryLock(5000, TimeUnit.MILLISECONDS)) {
lockSet = true;
logger.debug("SESSION LOCK SET sessionId: " + sessionId);
} else {
logger.error("SESSION LOCK - kill: userId " + userId);
}
UserManager.instance.removeUserFromAllTablesAndChat(userId, reason);
} catch (InterruptedException ex) {
logger.error("SESSION LOCK - kill: userId " + userId, ex);
} finally {
if (lockSet) {
lock.unlock();
logger.debug("SESSION LOCK UNLOCK sessionId: " + sessionId);
public boolean setLock() {
return lock.tryLock();
}

}
}
public void unlock() {
lock.unlock();
}

public void kill(DisconnectReason reason) {
UserManager.instance.removeUserFromAllTablesAndChat(userId, reason);
}

public void fireCallback(final ClientCallback call) {
if (error) {
return;
}
try {
call.setMessageId(messageId++);
callbackHandler.handleCallbackOneway(new Callback(call));
if (!isLocked()) { // only fire callback if session isn't about to be killed or in the process of handling disconnect detection
call.setMessageId(messageId++);
callbackHandler.handleCallbackOneway(new Callback(call));
}
} catch (HandleCallbackException ex) {
error = true; // to reduce repeated SESSION CALLBACK EXCEPTION
UserManager.instance.getUser(userId).ifPresent(user -> {
SessionManager.instance.disconnect(sessionId, LostConnection);
user.setUserState(User.UserState.Disconnected);
logger.warn("SESSION CALLBACK EXCEPTION - " + user.getName() + " userId " + userId + " - cause: " + getBasicCause(ex).toString());
logger.trace("Stack trace:", ex);
SessionManager.instance.disconnect(sessionId, LostConnection);
});
}
}
Expand Down
5 changes: 4 additions & 1 deletion Mage.Server/src/main/java/mage/server/SessionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public boolean setUserData(String userName, String sessionId, UserData userData,
public void disconnect(String sessionId, DisconnectReason reason) {
Session session = sessions.get(sessionId);
if (session != null) {
boolean lockWasSet = session.setLock();
if (!sessions.containsKey(sessionId)) {
// session was removed meanwhile by another thread so we can return
return;
Expand All @@ -148,7 +149,9 @@ public void disconnect(String sessionId, DisconnectReason reason) {
default:
logger.trace("endSession: unexpected reason " + reason.toString() + " - sessionId: " + sessionId);
}

if (lockWasSet) {
session.unlock();
}
}

}
Expand Down

0 comments on commit 7f17011

Please sign in to comment.