A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(desktop): log background account gateway sessions (#3637)
Hampus36ca87573c0f
diff --git a/fluxer_desktop/src/engine/AccountEngine.ts b/fluxer_desktop/src/engine/AccountEngine.tsindex 00af8cb76..01cea038d 100644--- a/fluxer_desktop/src/engine/AccountEngine.ts+++ b/fluxer_desktop/src/engine/AccountEngine.ts@@ -18,6 +18,7 @@ import {type EngineInitMessage,type EngineLogLevel,type EnginePowerEvent,+ type EngineSessionCounts,type EngineToHostMessage,type HostToEngineMessage,parseEngineAccountRecord,@@ -49,6 +50,18 @@ const CLIENT_CONFIG_META_KEY = 'client_config';const UNREAD_BADGE_CUSTOMIZATION_META_KEY = 'unread_badge_customization';const SEED_BATCH_SIZE = 4;+const SESSION_COUNTERS: Record<AccountEngineConnectionState, Exclude<keyof EngineSessionCounts, 'sessions'> | null> = {+ [AccountEngineConnectionState.STARTING]: 'connecting',+ [AccountEngineConnectionState.CONNECTING]: 'connecting',+ [AccountEngineConnectionState.RESUMING]: 'connecting',+ [AccountEngineConnectionState.READY]: 'ready',+ [AccountEngineConnectionState.BACKOFF]: 'backoff',+ [AccountEngineConnectionState.OFFLINE]: 'backoff',+ [AccountEngineConnectionState.FOREGROUND]: 'foreground',+ [AccountEngineConnectionState.AUTH_FAILED]: 'authFailed',+ [AccountEngineConnectionState.STOPPED]: null,+};+interface AccountEngineDependencies {readonly send: (message: EngineToHostMessage) => void;readonly clock: EngineClock;@@ -688,11 +701,16 @@ export class AccountEngine {if (this.shuttingDown) {return;}+ const counts = {sessions: this.sessions.size, ready: 0, connecting: 0, backoff: 0, foreground: 0, authFailed: 0};for (const session of this.sessions.values()) {session.refreshUnread();+ const counter = SESSION_COUNTERS[session.connectionState];+ if (counter !== null) {+ counts[counter] += 1;+ }}const usage = this.deps.memoryUsage();- this.deps.send({type: 'health', rss: usage.rss, heapUsed: usage.heapUsed, sessions: this.sessions.size});+ this.deps.send({type: 'health', rss: usage.rss, heapUsed: usage.heapUsed, counts});this.healthTimer = this.clock.setTimeout(() => this.reportHealth(), HEALTH_INTERVAL_MS);}diff --git a/fluxer_desktop/src/engine/AccountSession.ts b/fluxer_desktop/src/engine/AccountSession.tsindex f9a19c0c3..9358c8560 100644--- a/fluxer_desktop/src/engine/AccountSession.ts+++ b/fluxer_desktop/src/engine/AccountSession.ts@@ -134,6 +134,7 @@ export class AccountSession implements SchedulerClient, DispatchConsumer {private supersededToken: string | null = null;private failedTokenTag: string | null = null;private handshakeSucceeded = false;+ private handshakeStartedAt = 0;private invalidations = 0;private notificationTokens = NOTIFICATION_BURST;private notificationsRefilledAt: number;@@ -198,6 +199,14 @@ export class AccountSession implements SchedulerClient, DispatchConsumer {return this.record.instanceKey;}+ private get gatewayHost(): string {+ try {+ return new URL(this.record.gatewayEndpoint).host;+ } catch {+ return this.record.gatewayEndpoint;+ }+ }+get priority(): number {return this.record.lastActive;}@@ -448,6 +457,7 @@ export class AccountSession implements SchedulerClient, DispatchConsumer {this.dropQueue();this.deps.pump.forget(this);this.disconnect(code, reason);+ this.logGateway('Account engine gateway stopped', {reason});this.resume = null;this.setState(AccountEngineConnectionState.STOPPED);if (options.dispose) {@@ -533,12 +543,14 @@ export class AccountSession implements SchedulerClient, DispatchConsumer {}this.cacheUpdatedAt = this.deps.clock.now();this.setState(AccountEngineConnectionState.READY);+ this.logReady(false);return;}if (item.type === 'RESUMED') {this.attempt = 0;this.handshakeSucceeded = true;this.setState(AccountEngineConnectionState.READY);+ this.logReady(true);} else {this.core.applyDispatch(item.type, item.data);}@@ -621,6 +633,7 @@ export class AccountSession implements SchedulerClient, DispatchConsumer {return;}this.client = client;+ this.handshakeStartedAt = this.deps.clock.now();client.start();}@@ -681,6 +694,7 @@ export class AccountSession implements SchedulerClient, DispatchConsumer {if (this.stopped || this.held) {return;}+ this.logGateway('Account engine gateway disconnected', exit);switch (exit.kind) {case 'auth_failed':this.resume = null;@@ -759,9 +773,23 @@ export class AccountSession implements SchedulerClient, DispatchConsumer {this.client = null;this.dropQueue();this.deps.scheduler.releaseSlot(this.accountKey);+ if (client !== null && !this.stopped) {+ this.logGateway('Account engine gateway closed', {code, reason});+ }client?.close(code, reason);}+ private logReady(resumed: boolean): void {+ this.logGateway('Account engine gateway ready', {+ resumed,+ readyMs: this.deps.clock.now() - this.handshakeStartedAt,+ });+ }++ private logGateway(message: string, detail: object): void {+ this.deps.log('info', message, {accountKey: this.accountKey, host: this.gatewayHost, ...detail});+ }+private dropQueue(): void {this.queue = [];this.queueHead = 0;@@ -806,6 +834,9 @@ export class AccountSession implements SchedulerClient, DispatchConsumer {}this.state = state;this.stateChangedAt = this.deps.clock.now();+ if (state === AccountEngineConnectionState.FOREGROUND) {+ this.logGateway('Account engine session moved to the foreground', {});+ }if (state !== AccountEngineConnectionState.BACKOFF && state !== AccountEngineConnectionState.OFFLINE) {this.nextRetryAt = null;}diff --git a/fluxer_desktop/src/engine/HostProtocol.ts b/fluxer_desktop/src/engine/HostProtocol.tsindex 129244173..e493fd435 100644--- a/fluxer_desktop/src/engine/HostProtocol.ts+++ b/fluxer_desktop/src/engine/HostProtocol.ts@@ -62,6 +62,15 @@ export type HostToEngineMessage =export type EngineTokenPersistOutcome = 'persisted' | 'rejected' | 'renderer';+export interface EngineSessionCounts {+ readonly sessions: number;+ readonly ready: number;+ readonly connecting: number;+ readonly backoff: number;+ readonly foreground: number;+ readonly authFailed: number;+}+export type EngineLogLevel = 'debug' | 'info' | 'warn' | 'error';export type EngineToHostMessage =@@ -78,7 +87,12 @@ export type EngineToHostMessage =| {readonly type: 'tokenRotated'; readonly accountKey: string; readonly previousToken: string; readonly token: string}| {readonly type: 'summaries'; readonly summaries: ReadonlyArray<AccountEngineAccountSummary>}| {readonly type: 'removed'; readonly accountKeys: ReadonlyArray<string>}- | {readonly type: 'health'; readonly rss: number; readonly heapUsed: number; readonly sessions: number}+ | {+ readonly type: 'health';+ readonly rss: number;+ readonly heapUsed: number;+ readonly counts: EngineSessionCounts;+ }| {readonly type: 'log'; readonly level: EngineLogLevel; readonly message: string; readonly detail: string | null}| {readonly type: 'shutdownComplete'};diff --git a/fluxer_desktop/src/main/AccountEngineHost.ts b/fluxer_desktop/src/main/AccountEngineHost.tsindex bdc37e8d9..28a968de9 100644--- a/fluxer_desktop/src/main/AccountEngineHost.ts+++ b/fluxer_desktop/src/main/AccountEngineHost.ts@@ -324,7 +324,7 @@ export class AccountEngineHostController {this.deps.log('info', 'Account engine health', {rss: message.rss,heapUsed: message.heapUsed,- sessions: message.sessions,+ ...message.counts,});return;case 'log':