2025-09-17 22:49:36 +02:00
|
|
|
export class OAuthReconnectionTracker {
|
2025-09-21 07:55:32 -04:00
|
|
|
/** Map of userId -> Set of serverNames that have failed reconnection */
|
2025-09-17 22:49:36 +02:00
|
|
|
private failed: Map<string, Set<string>> = new Map();
|
2025-09-21 07:55:32 -04:00
|
|
|
/** Map of userId -> Set of serverNames that are actively reconnecting */
|
2025-09-17 22:49:36 +02:00
|
|
|
private active: Map<string, Set<string>> = new Map();
|
2025-09-21 07:55:32 -04:00
|
|
|
/** Map of userId:serverName -> timestamp when reconnection started */
|
|
|
|
private activeTimestamps: Map<string, number> = new Map();
|
|
|
|
/** Maximum time (ms) a server can be in reconnecting state before auto-cleanup */
|
|
|
|
private readonly RECONNECTION_TIMEOUT_MS = 5 * 60 * 1000; // 5 minutes
|
2025-09-17 22:49:36 +02:00
|
|
|
|
|
|
|
public isFailed(userId: string, serverName: string): boolean {
|
|
|
|
return this.failed.get(userId)?.has(serverName) ?? false;
|
|
|
|
}
|
|
|
|
|
2025-09-21 08:30:55 -04:00
|
|
|
/** Check if server is in the active set (original simple check) */
|
2025-09-17 22:49:36 +02:00
|
|
|
public isActive(userId: string, serverName: string): boolean {
|
2025-09-21 08:30:55 -04:00
|
|
|
return this.active.get(userId)?.has(serverName) ?? false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Check if server is still reconnecting (considers timeout) */
|
|
|
|
public isStillReconnecting(userId: string, serverName: string): boolean {
|
|
|
|
if (!this.isActive(userId, serverName)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-09-21 07:55:32 -04:00
|
|
|
const key = `${userId}:${serverName}`;
|
|
|
|
const startTime = this.activeTimestamps.get(key);
|
|
|
|
|
2025-09-21 08:30:55 -04:00
|
|
|
// If there's a timestamp and it has timed out, it's not still reconnecting
|
2025-09-21 07:55:32 -04:00
|
|
|
if (startTime && Date.now() - startTime > this.RECONNECTION_TIMEOUT_MS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-09-21 08:30:55 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Clean up server if it has timed out - returns true if cleanup was performed */
|
|
|
|
public cleanupIfTimedOut(userId: string, serverName: string): boolean {
|
|
|
|
const key = `${userId}:${serverName}`;
|
|
|
|
const startTime = this.activeTimestamps.get(key);
|
|
|
|
|
|
|
|
if (startTime && Date.now() - startTime > this.RECONNECTION_TIMEOUT_MS) {
|
|
|
|
this.removeActive(userId, serverName);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2025-09-17 22:49:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public setFailed(userId: string, serverName: string): void {
|
|
|
|
if (!this.failed.has(userId)) {
|
|
|
|
this.failed.set(userId, new Set());
|
|
|
|
}
|
|
|
|
|
|
|
|
this.failed.get(userId)?.add(serverName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public setActive(userId: string, serverName: string): void {
|
|
|
|
if (!this.active.has(userId)) {
|
|
|
|
this.active.set(userId, new Set());
|
|
|
|
}
|
|
|
|
|
|
|
|
this.active.get(userId)?.add(serverName);
|
2025-09-21 07:55:32 -04:00
|
|
|
|
|
|
|
/** Track when reconnection started */
|
|
|
|
const key = `${userId}:${serverName}`;
|
|
|
|
this.activeTimestamps.set(key, Date.now());
|
2025-09-17 22:49:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public removeFailed(userId: string, serverName: string): void {
|
|
|
|
const userServers = this.failed.get(userId);
|
|
|
|
userServers?.delete(serverName);
|
|
|
|
if (userServers?.size === 0) {
|
|
|
|
this.failed.delete(userId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public removeActive(userId: string, serverName: string): void {
|
|
|
|
const userServers = this.active.get(userId);
|
|
|
|
userServers?.delete(serverName);
|
|
|
|
if (userServers?.size === 0) {
|
|
|
|
this.active.delete(userId);
|
|
|
|
}
|
2025-09-21 07:55:32 -04:00
|
|
|
|
|
|
|
/** Clear timestamp tracking */
|
|
|
|
const key = `${userId}:${serverName}`;
|
|
|
|
this.activeTimestamps.delete(key);
|
2025-09-17 22:49:36 +02:00
|
|
|
}
|
|
|
|
}
|