| export class OAuthReconnectionTracker { |
| |
| private failed: Map<string, Set<string>> = new Map(); |
| |
| private active: Map<string, Set<string>> = new Map(); |
| |
| private activeTimestamps: Map<string, number> = new Map(); |
| |
| private readonly RECONNECTION_TIMEOUT_MS = 3 * 60 * 1000; |
|
|
| public isFailed(userId: string, serverName: string): boolean { |
| return this.failed.get(userId)?.has(serverName) ?? false; |
| } |
|
|
| |
| public isActive(userId: string, serverName: string): boolean { |
| return this.active.get(userId)?.has(serverName) ?? false; |
| } |
|
|
| |
| public isStillReconnecting(userId: string, serverName: string): boolean { |
| if (!this.isActive(userId, serverName)) { |
| return false; |
| } |
|
|
| const key = `${userId}:${serverName}`; |
| const startTime = this.activeTimestamps.get(key); |
|
|
| |
| if (startTime && Date.now() - startTime > this.RECONNECTION_TIMEOUT_MS) { |
| return false; |
| } |
|
|
| return true; |
| } |
|
|
| |
| 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; |
| } |
|
|
| 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); |
|
|
| |
| const key = `${userId}:${serverName}`; |
| this.activeTimestamps.set(key, Date.now()); |
| } |
|
|
| 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); |
| } |
|
|
| |
| const key = `${userId}:${serverName}`; |
| this.activeTimestamps.delete(key); |
| } |
| } |
|
|