mirror of
https://github.com/magefree/mage.git
synced 2025-12-24 04:22:01 -08:00
fixed timeout issue - HeartbeatHandler now monitors connection health
This commit is contained in:
parent
1a6e0e5d09
commit
d2ae55fff6
4 changed files with 65 additions and 64 deletions
|
|
@ -109,7 +109,7 @@ public class Session {
|
|||
if (user == null) { // user already exists
|
||||
user = UserManager.getInstance().findUser(userName);
|
||||
if (user.getHost().equals(host)) {
|
||||
user.updateLastActivity(null); // minimizes possible expiration
|
||||
// user.updateLastActivity(null); // minimizes possible expiration
|
||||
this.userId = user.getId();
|
||||
if (user.getSessionId().isEmpty()) {
|
||||
logger.info("Reconnecting session for " + userName);
|
||||
|
|
|
|||
|
|
@ -215,11 +215,11 @@ public class SessionManager {
|
|||
return null;
|
||||
}
|
||||
|
||||
public boolean extendUserSession(String sessionId, String pingInfo) {
|
||||
Session session = sessions.get(sessionId);
|
||||
if (session != null) {
|
||||
return UserManager.getInstance().extendUserSession(session.getUserId(), pingInfo);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// public boolean extendUserSession(String sessionId, String pingInfo) {
|
||||
// Session session = sessions.get(sessionId);
|
||||
// if (session != null) {
|
||||
// return UserManager.getInstance().extendUserSession(session.getUserId(), pingInfo);
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -81,7 +81,8 @@ public class User {
|
|||
private String sessionId;
|
||||
private String info = "";
|
||||
private String pingInfo = "";
|
||||
private Date lastActivity;
|
||||
private Date disconnectionTime;
|
||||
// private Date lastActivity;
|
||||
private UserState userState;
|
||||
private UserData userData;
|
||||
|
||||
|
|
@ -92,7 +93,7 @@ public class User {
|
|||
this.userState = UserState.Created;
|
||||
|
||||
this.connectionTime = new Date();
|
||||
this.lastActivity = new Date();
|
||||
// this.lastActivity = new Date();
|
||||
|
||||
this.tables = new ConcurrentHashMap<>();
|
||||
this.gameSessions = new ConcurrentHashMap<>();
|
||||
|
|
@ -151,7 +152,7 @@ public class User {
|
|||
return userState.equals(UserState.Connected) || userState.equals(UserState.Reconnected);
|
||||
}
|
||||
|
||||
public String getDisconnectDuration() {
|
||||
private String getDisconnectDuration() {
|
||||
long secondsDisconnected = getSecondsDisconnected();
|
||||
long secondsLeft;
|
||||
String sign = "";
|
||||
|
|
@ -168,7 +169,7 @@ public class User {
|
|||
}
|
||||
|
||||
public long getSecondsDisconnected() {
|
||||
return SystemUtil.getDateDiff(lastActivity, new Date(), TimeUnit.SECONDS);
|
||||
return SystemUtil.getDateDiff(disconnectionTime, new Date(), TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public Date getConnectionTime() {
|
||||
|
|
@ -235,49 +236,49 @@ public class User {
|
|||
}
|
||||
|
||||
public void sendPlayerUUID(final UUID gameId, final UUID data) {
|
||||
lastActivity = new Date();
|
||||
// lastActivity = new Date();
|
||||
GameManager.getInstance().sendPlayerUUID(gameId, userId, data);
|
||||
}
|
||||
|
||||
public void sendPlayerString(final UUID gameId, final String data) {
|
||||
lastActivity = new Date();
|
||||
// lastActivity = new Date();
|
||||
GameManager.getInstance().sendPlayerString(gameId, userId, data);
|
||||
}
|
||||
|
||||
public void sendPlayerManaType(final UUID gameId, final UUID playerId, final ManaType data) {
|
||||
lastActivity = new Date();
|
||||
// lastActivity = new Date();
|
||||
GameManager.getInstance().sendPlayerManaType(gameId, playerId, userId, data);
|
||||
}
|
||||
|
||||
public void sendPlayerBoolean(final UUID gameId, final Boolean data) {
|
||||
lastActivity = new Date();
|
||||
// lastActivity = new Date();
|
||||
GameManager.getInstance().sendPlayerBoolean(gameId, userId, data);
|
||||
}
|
||||
|
||||
public void sendPlayerInteger(final UUID gameId, final Integer data) {
|
||||
lastActivity = new Date();
|
||||
// lastActivity = new Date();
|
||||
GameManager.getInstance().sendPlayerInteger(gameId, userId, data);
|
||||
}
|
||||
|
||||
public void updateLastActivity(String pingInfo) {
|
||||
if (pingInfo != null) {
|
||||
this.pingInfo = pingInfo;
|
||||
}
|
||||
lastActivity = new Date();
|
||||
if (userState == UserState.Disconnected) { // this can happen if user reconnects very fast after disconnect
|
||||
userState = UserState.Reconnected;
|
||||
}
|
||||
}
|
||||
// public void updateLastActivity(String pingInfo) {
|
||||
// if (pingInfo != null) {
|
||||
// this.pingInfo = pingInfo;
|
||||
// }
|
||||
// lastActivity = new Date();
|
||||
// if (userState == UserState.Disconnected) { // this can happen if user reconnects very fast after disconnect
|
||||
// userState = UserState.Reconnected;
|
||||
// }
|
||||
// }
|
||||
|
||||
public boolean isExpired(Date expired) {
|
||||
if (lastActivity.before(expired)) {
|
||||
logger.trace(userName + " is expired!");
|
||||
userState = UserState.Expired;
|
||||
return true;
|
||||
}
|
||||
logger.trace(new StringBuilder("isExpired: User ").append(userName).append(" lastActivity: ").append(lastActivity).append(" expired: ").append(expired).toString());
|
||||
return false; /*userState == UserState.Disconnected && */
|
||||
}
|
||||
// public boolean isExpired(Date expired) {
|
||||
// if (lastActivity.before(expired)) {
|
||||
// logger.trace(userName + " is expired!");
|
||||
// userState = UserState.Expired;
|
||||
// return true;
|
||||
// }
|
||||
// logger.trace(new StringBuilder("isExpired: User ").append(userName).append(" lastActivity: ").append(lastActivity).append(" expired: ").append(expired).toString());
|
||||
// return false; /*userState == UserState.Disconnected && */
|
||||
// }
|
||||
|
||||
private void reconnect() {
|
||||
for (Entry<UUID, Table> entry: tables.entrySet()) {
|
||||
|
|
|
|||
|
|
@ -64,14 +64,14 @@ public class UserManager {
|
|||
return INSTANCE;
|
||||
}
|
||||
|
||||
private UserManager() {
|
||||
expireExecutor.scheduleAtFixedRate(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
checkExpired();
|
||||
}
|
||||
}, 60, 60, TimeUnit.SECONDS);
|
||||
}
|
||||
// private UserManager() {
|
||||
// expireExecutor.scheduleAtFixedRate(new Runnable() {
|
||||
// @Override
|
||||
// public void run() {
|
||||
// checkExpired();
|
||||
// }
|
||||
// }, 60, 60, TimeUnit.SECONDS);
|
||||
// }
|
||||
|
||||
public User createUser(String userName, String host) {
|
||||
if (findUser(userName) != null) {
|
||||
|
|
@ -159,31 +159,31 @@ public class UserManager {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean extendUserSession(UUID userId, String pingInfo) {
|
||||
if (userId != null) {
|
||||
User user = users.get(userId);
|
||||
if (user != null) {
|
||||
user.updateLastActivity(pingInfo);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// public boolean extendUserSession(UUID userId, String pingInfo) {
|
||||
// if (userId != null) {
|
||||
// User user = users.get(userId);
|
||||
// if (user != null) {
|
||||
// user.updateLastActivity(pingInfo);
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Is the connection lost for more than 3 minutes, the user will be removed (within 3 minutes the user can reconnect)
|
||||
*/
|
||||
private void checkExpired() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.add(Calendar.MINUTE, -3);
|
||||
List<User> usersToCheck = new ArrayList<>();
|
||||
usersToCheck.addAll(users.values());
|
||||
for (User user : usersToCheck) {
|
||||
if (!user.getUserState().equals(UserState.Expired) && user.isExpired(calendar.getTime())) {
|
||||
removeUser(user.getId(), DisconnectReason.SessionExpired);
|
||||
}
|
||||
}
|
||||
}
|
||||
// private void checkExpired() {
|
||||
// Calendar calendar = Calendar.getInstance();
|
||||
// calendar.add(Calendar.MINUTE, -3);
|
||||
// List<User> usersToCheck = new ArrayList<>();
|
||||
// usersToCheck.addAll(users.values());
|
||||
// for (User user : usersToCheck) {
|
||||
// if (!user.getUserState().equals(UserState.Expired) && user.isExpired(calendar.getTime())) {
|
||||
// removeUser(user.getId(), DisconnectReason.SessionExpired);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
public void handleException(Exception ex) {
|
||||
if (ex != null) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue