A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
refactor(app): move read state acks into their own queue
Hampus54a2f7820198
diff --git a/fluxer_app/src/features/read_state/state/ReadStates.ts b/fluxer_app/src/features/read_state/state/ReadStates.tsindex ab686918c..2ab482e1c 100644--- a/fluxer_app/src/features/read_state/state/ReadStates.ts+++ b/fluxer_app/src/features/read_state/state/ReadStates.ts@@ -3,32 +3,27 @@import {Endpoints} from '@app/features/app/constants/Endpoints';import Channels from '@app/features/channel/state/Channels';import type {SnapshotReadStateRow} from '@app/features/gateway/snapshot/SnapshotEntities';+import Guilds from '@app/features/guild/state/Guilds';import Messages from '@app/features/messaging/state/MessagingMessages';import AutoAck from '@app/features/notification/state/NotificationAutoAck';import {http} from '@app/features/platform/transport/RestTransport';import {HttpError} from '@app/features/platform/types/EndpointError';import {Logger} from '@app/features/platform/utils/AppLogger';import {resolveReadStateAckDecision} from '@app/features/read_state/state/read_states/ReadStateAckMachine';+import {ReadStateAckQueue} from '@app/features/read_state/state/read_states/ReadStateAckQueue';import {ReadStateEntry} from '@app/features/read_state/state/read_states/ReadStateEntry';import {resolveReadStateIncomingMessageDecision} from '@app/features/read_state/state/read_states/ReadStateIncomingMessageMachine';import {resolveReadStateServerAckDecision} from '@app/features/read_state/state/read_states/ReadStateServerAckMachine';import {- ACK_BATCH_DELAY_MS,- ACK_BATCH_SIZE,- ACK_RETRY_BASE_DELAY_MS,- ACK_RETRY_MAX_DELAY_MS,type AckOptions,type AppliedAck,type ArchivedReadState,type ChannelPayload,- chunkEntries,compareMessageIds,type GatewayReadState,isNewerMessageId,- type PendingAck,parseTimestamp,type ReadStateAckRequestEntry,- type Timer,} from '@app/features/read_state/state/read_states/shared';import Relationships from '@app/features/relationship/state/Relationships';import Dimension from '@app/features/ui/state/Dimension';@@ -56,12 +51,17 @@ class ReadStates {private readonly versionBox = observable.box(0);private readonly privateChannelVersionBox = observable.box(0);private hasPendingPrivateChannelChange = false;- private readonly pendingAcks = new Map<ChannelId, PendingAck>();+ private readonly ackQueue = new ReadStateAckQueue({+ isPreviewChannel: (channelId) => this.isPreviewChannel(channelId),+ currentUserId: () => Users.getCurrentUser()?.id ?? null,+ getState: (channelId) => this.getIfExists(channelId),+ postAckEntries: (entries) => this.postAckEntries(entries),+ applyResponse: (response) => this.applyAckResponse(response, false),+ reportError: (error) => logger.error('Failed to flush read-state acknowledgements:', error),+ });updateCounter = 0;private pendingChanges = new Map<ChannelId, GuildId | null>();private pendingGlobalRecompute = false;- private ackFlushTimer: Timer | null = null;- private isFlushingAcks = false;private suppressVersionBumpDepth = 0;private hasSuppressedVersionBump = false;@@ -219,7 +219,7 @@ class ReadStates {return false;}const guildId = entry.guildId;- this.cancelPendingAck(channelId);+ this.ackQueue.cancel(channelId);this.states.delete(channelId as ChannelId);this.mentionChannels.delete(channelId as ChannelId);this.notifyChange(channelId, {guildIdOverride: guildId});@@ -248,8 +248,7 @@ class ReadStates {this.mentionChannels.clear();this.pendingChanges.clear();this.pendingGlobalRecompute = false;- this.pendingAcks.clear();- this.clearAckFlushTimer();+ this.ackQueue.clear();}get mentionChannelIds(): Array<ChannelId> {@@ -362,7 +361,13 @@ class ReadStates {}}+ private isPreviewChannel(channelId: string): boolean {+ const guildId = Channels.getChannel(channelId)?.guildId;+ return guildId != null && Guilds.getGuild(guildId)?.visiting === true;+ }+async sendManualAck(channelId: string, messageId: string, mentionCount: number): Promise<void> {+ if (this.isPreviewChannel(channelId)) return;const response = await this.postAckEntries([{channel_id: channelId,@@ -396,7 +401,7 @@ class ReadStates {handleGatewayReady(action: {readState: Array<GatewayReadState>; channels: Array<ChannelPayload>}): void {this.suppressVersionBumps(() => {- const preservedAcks = this.capturePendingAcks();+ const preservedAcks = this.ackQueue.capture();this.reset();const readStates = action.readState;const channelsWithReadState = new Set<ChannelId>();@@ -419,14 +424,14 @@ class ReadStates {}this.clearUnreadStateIfRead(state);}- this.restorePendingAcks(preservedAcks);+ this.ackQueue.restore(preservedAcks);this.notifyChange(undefined, {global: true});});}hydrateFromSnapshot(readStates: ReadonlyMap<string, SnapshotReadStateRow>): void {this.suppressVersionBumps(() => {- const preservedAcks = this.capturePendingAcks();+ const preservedAcks = this.ackQueue.capture();this.reset();for (const [channelId, row] of readStates) {const state = this.get(channelId);@@ -439,7 +444,7 @@ class ReadStates {state.lastPinTimestamp = Channels.getChannel(channelId)?.lastPinTimestamp?.getTime() ?? 0;this.clearUnreadStateIfRead(state);}- this.restorePendingAcks(preservedAcks);+ this.ackQueue.restore(preservedAcks);this.notifyChange(undefined, {global: true});});}@@ -529,7 +534,7 @@ class ReadStates {});switch (decision.type) {case 'ackCurrentUserMessage':- this.cancelPendingAck(action.channelId);+ this.ackQueue.cancel(action.channelId);state.clearStickyUnread();this.applyAck(state, {messageId: action.message.id, local: true});this.notifyChange(action.channelId);@@ -663,7 +668,7 @@ class ReadStates {action.channel.type === ChannelTypes.DM_PERSONAL_NOTES)) {if (action.channel.type === ChannelTypes.GROUP_DM) {- this.cancelPendingAck(action.channel.id);+ this.ackQueue.cancel(action.channel.id);}state.messagesLoaded = false;state.ackedManually = false;@@ -797,7 +802,7 @@ class ReadStates {state.ackedManually = true;state.rebuild(action.messageId, {recomputeMentions: true});state.serverVersion = action.version ?? state.serverVersion;- this.cancelPendingAck(action.channelId);+ this.ackQueue.cancel(action.channelId);AutoAck.disableForChannel(action.channelId);if (mentionCount != null) {this.setMentionCount(state, mentionCount);@@ -818,7 +823,7 @@ class ReadStates {if (decision.shouldNotify) {this.notifyChange(action.channelId);}- this.cancelPendingAckIfCovered(action.channelId, action.messageId);+ this.ackQueue.cancelIfCovered(action.channelId, action.messageId);return;case 'advanceAck': {const result = this.applyAck(state, {messageId: action.messageId, local: true});@@ -829,7 +834,7 @@ class ReadStates {this.setMentionCount(state, mentionCount);}state.serverVersion = action.version ?? state.serverVersion;- this.cancelPendingAckIfCovered(action.channelId, action.messageId);+ this.ackQueue.cancelIfCovered(action.channelId, action.messageId);this.notifyChange(action.channelId);return;}@@ -862,27 +867,7 @@ class ReadStates {}async flushPendingAcks(): Promise<void> {- await this.flushDueAcks(true);- }-- private capturePendingAcks(): Array<PendingAck> {- return Array.from(this.pendingAcks.values(), (pending) => ({...pending}));- }-- private restorePendingAcks(preserved: ReadonlyArray<PendingAck>): void {- const currentUserId = Users.getCurrentUser()?.id ?? null;- for (const pending of preserved) {- if (pending.userId !== currentUserId) continue;- const state = this.getIfExists(pending.channelId);- if (state == null) continue;- if (compareMessageIds(pending.messageId, state.ackMessageId) <= 0) continue;- this.pendingAcks.set(pending.channelId as ChannelId, {...pending, deadline: Date.now()});- }- if (this.pendingAcks.size === 0) {- return;- }- this.scheduleAckFlush();- void this.flushDueAcks(true);+ await this.ackQueue.flush('all');}private applyAck(state: ReadStateEntry, options: AckOptions): AppliedAck {@@ -936,34 +921,6 @@ class ReadStates {return true;}- private queueAck(- channelId: string,- messageId: string,- options: {- immediate?: boolean;- hadMentions?: boolean;- },- ): void {- const now = Date.now();- const delay = options.immediate || options.hadMentions ? 0 : ACK_BATCH_DELAY_MS;- const deadline = now + delay;- const existing = this.pendingAcks.get(channelId as ChannelId);- const existingIsNewer = existing != null && compareMessageIds(existing.messageId, messageId) > 0;- const pending: PendingAck = {- channelId,- messageId: existingIsNewer ? existing.messageId : messageId,- deadline: existing == null ? deadline : Math.min(existing.deadline, deadline),- attempt: existing?.attempt ?? 0,- userId: Users.getCurrentUser()?.id ?? null,- };- this.pendingAcks.set(channelId as ChannelId, pending);- const state = this.getIfExists(channelId);- if (state != null) {- state.inFlightAckMessageId = pending.messageId;- }- this.scheduleAckFlush();- }-private queueAppliedAck(channelId: string,result: AppliedAck,@@ -972,103 +929,7 @@ class ReadStates {},): void {if (!result.acked || result.messageId == null) return;- this.queueAck(channelId, result.messageId, {immediate: options.immediate, hadMentions: result.hadMentions});- }-- private clearAckFlushTimer(): void {- if (this.ackFlushTimer != null) {- clearTimeout(this.ackFlushTimer);- this.ackFlushTimer = null;- }- }-- private scheduleAckFlush(): void {- this.clearAckFlushTimer();- if (this.pendingAcks.size === 0 || this.isFlushingAcks) {- return;- }- let nextDeadline = Number.POSITIVE_INFINITY;- for (const pending of this.pendingAcks.values()) {- nextDeadline = Math.min(nextDeadline, pending.deadline);- }- const delay = Math.max(0, nextDeadline - Date.now());- this.ackFlushTimer = setTimeout(() => {- this.ackFlushTimer = null;- void this.flushDueAcks(false);- }, delay);- }-- private cancelPendingAck(channelId: string): void {- const deleted = this.pendingAcks.delete(channelId as ChannelId);- const state = this.getIfExists(channelId);- if (state != null) {- state.inFlightAckMessageId = null;- }- if (deleted) {- this.scheduleAckFlush();- }- }-- private cancelPendingAckIfCovered(channelId: string, ackedMessageId: string): void {- const pending = this.pendingAcks.get(channelId as ChannelId);- if (pending == null) return;- if (compareMessageIds(ackedMessageId, pending.messageId) < 0) return;- this.cancelPendingAck(channelId);- }-- private async flushDueAcks(force: boolean): Promise<void> {- if (this.isFlushingAcks) {- return;- }- this.clearAckFlushTimer();- const now = Date.now();- const due = Array.from(this.pendingAcks.values()).filter((pending) => force || pending.deadline <= now);- if (due.length === 0) {- this.scheduleAckFlush();- return;- }- this.isFlushingAcks = true;- try {- for (const chunk of chunkEntries(due, ACK_BATCH_SIZE)) {- await this.sendAckChunk(chunk);- }- } finally {- this.isFlushingAcks = false;- this.scheduleAckFlush();- }- }-- private async sendAckChunk(entries: Array<PendingAck>): Promise<void> {- try {- const response = await this.postAckEntries(- entries.map((entry) => ({- channel_id: entry.channelId,- message_id: entry.messageId,- })),- );- this.applyAckResponse(response, false);- for (const entry of entries) {- const current = this.pendingAcks.get(entry.channelId as ChannelId);- if (current?.messageId === entry.messageId) {- this.pendingAcks.delete(entry.channelId as ChannelId);- const state = this.getIfExists(entry.channelId);- if (state?.inFlightAckMessageId === entry.messageId) {- state.inFlightAckMessageId = null;- }- }- }- } catch (error) {- logger.error('Failed to flush read-state acknowledgements:', error);- const now = Date.now();- for (const entry of entries) {- const current = this.pendingAcks.get(entry.channelId as ChannelId);- if (current == null || current.messageId !== entry.messageId) continue;- const attempt = current.attempt + 1;- const retryDelay = Math.min(ACK_RETRY_MAX_DELAY_MS, ACK_RETRY_BASE_DELAY_MS * 2 ** (attempt - 1));- current.attempt = attempt;- current.deadline = now + retryDelay;- }- }+ this.ackQueue.enqueue(channelId, result.messageId, {immediate: options.immediate, hadMentions: result.hadMentions});}private async postAckEntries(entries: Array<ReadStateAckRequestEntry>): Promise<ReadStateAckResponse | null> {diff --git a/fluxer_app/src/features/read_state/state/read_states/ReadStateAckQueue.ts b/fluxer_app/src/features/read_state/state/read_states/ReadStateAckQueue.tsnew file mode 100644index 000000000..8e723429b--- /dev/null+++ b/fluxer_app/src/features/read_state/state/read_states/ReadStateAckQueue.ts@@ -0,0 +1,203 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import type {ReadStateEntry} from '@app/features/read_state/state/read_states/ReadStateEntry';+import {+ ACK_BATCH_DELAY_MS,+ chunkEntries,+ compareMessageIds,+ type ReadStateAckRequestEntry,+} from '@app/features/read_state/state/read_states/shared';+import type {ReadStateAckResponse} from '@fluxer/schema/src/domains/channel/ChannelRequestSchemas';++const ACK_BATCH_SIZE = 100;+const ACK_RETRY_BASE_DELAY_MS = 5000;+const ACK_RETRY_MAX_DELAY_MS = 60000;++interface PendingAck {+ channelId: string;+ messageId: string;+ deadline: number;+ attempt: number;+ userId: string | null;+}++interface AckQueueCallbacks {+ isPreviewChannel: (channelId: string) => boolean;+ currentUserId: () => string | null;+ getState: (channelId: string) => Pick<ReadStateEntry, 'ackMessageId' | 'inFlightAckMessageId'> | undefined;+ postAckEntries: (entries: Array<ReadStateAckRequestEntry>) => Promise<ReadStateAckResponse | null>;+ applyResponse: (response: ReadStateAckResponse | null) => void;+ reportError: (error: unknown) => void;+}++export class ReadStateAckQueue {+ private readonly pendingAcks = new Map<string, PendingAck>();+ private ackFlushTimer: ReturnType<typeof setTimeout> | null = null;+ private isFlushingAcks = false;++ constructor(private readonly callbacks: AckQueueCallbacks) {}++ clear(): void {+ this.pendingAcks.clear();+ this.clearAckFlushTimer();+ }++ capture(): Array<PendingAck> {+ return Array.from(this.pendingAcks.values(), (pending) => ({...pending}));+ }++ restore(preserved: ReadonlyArray<PendingAck>): void {+ const currentUserId = this.callbacks.currentUserId();+ for (const pending of preserved) {+ if (pending.userId !== currentUserId) {+ continue;+ }+ const state = this.callbacks.getState(pending.channelId);+ if (state == null) {+ continue;+ }+ if (compareMessageIds(pending.messageId, state.ackMessageId) <= 0) {+ continue;+ }+ this.pendingAcks.set(pending.channelId, {...pending, deadline: Date.now()});+ }+ if (this.pendingAcks.size === 0) {+ return;+ }+ this.scheduleAckFlush();+ void this.flush('all');+ }++ enqueue(+ channelId: string,+ messageId: string,+ options: {+ immediate?: boolean;+ hadMentions?: boolean;+ },+ ): void {+ if (this.callbacks.isPreviewChannel(channelId)) {+ return;+ }+ const now = Date.now();+ const delay = options.immediate || options.hadMentions ? 0 : ACK_BATCH_DELAY_MS;+ const deadline = now + delay;+ const existing = this.pendingAcks.get(channelId);+ const existingIsNewer = existing != null && compareMessageIds(existing.messageId, messageId) > 0;+ const pending: PendingAck = {+ channelId,+ messageId: existingIsNewer ? existing.messageId : messageId,+ deadline: existing == null ? deadline : Math.min(existing.deadline, deadline),+ attempt: existing?.attempt ?? 0,+ userId: this.callbacks.currentUserId(),+ };+ this.pendingAcks.set(channelId, pending);+ const state = this.callbacks.getState(channelId);+ if (state != null) {+ state.inFlightAckMessageId = pending.messageId;+ }+ this.scheduleAckFlush();+ }++ private clearAckFlushTimer(): void {+ if (this.ackFlushTimer != null) {+ clearTimeout(this.ackFlushTimer);+ this.ackFlushTimer = null;+ }+ }++ private scheduleAckFlush(): void {+ this.clearAckFlushTimer();+ if (this.pendingAcks.size === 0 || this.isFlushingAcks) {+ return;+ }+ let nextDeadline = Number.POSITIVE_INFINITY;+ for (const pending of this.pendingAcks.values()) {+ nextDeadline = Math.min(nextDeadline, pending.deadline);+ }+ const delay = Math.max(0, nextDeadline - Date.now());+ this.ackFlushTimer = setTimeout(() => {+ this.ackFlushTimer = null;+ void this.flush('due');+ }, delay);+ }++ cancel(channelId: string): void {+ const deleted = this.pendingAcks.delete(channelId);+ const state = this.callbacks.getState(channelId);+ if (state != null) {+ state.inFlightAckMessageId = null;+ }+ if (deleted) {+ this.scheduleAckFlush();+ }+ }++ cancelIfCovered(channelId: string, ackedMessageId: string): void {+ const pending = this.pendingAcks.get(channelId);+ if (pending == null) {+ return;+ }+ if (compareMessageIds(ackedMessageId, pending.messageId) < 0) {+ return;+ }+ this.cancel(channelId);+ }++ async flush(mode: 'due' | 'all'): Promise<void> {+ if (this.isFlushingAcks) {+ return;+ }+ this.clearAckFlushTimer();+ const now = Date.now();+ const due = Array.from(this.pendingAcks.values()).filter((pending) => mode === 'all' || pending.deadline <= now);+ if (due.length === 0) {+ this.scheduleAckFlush();+ return;+ }+ this.isFlushingAcks = true;+ try {+ for (const chunk of chunkEntries(due, ACK_BATCH_SIZE)) {+ await this.sendAckChunk(chunk);+ }+ } finally {+ this.isFlushingAcks = false;+ this.scheduleAckFlush();+ }+ }++ private async sendAckChunk(entries: Array<PendingAck>): Promise<void> {+ try {+ const response = await this.callbacks.postAckEntries(+ entries.map((entry) => ({+ channel_id: entry.channelId,+ message_id: entry.messageId,+ })),+ );+ this.callbacks.applyResponse(response);+ for (const entry of entries) {+ const current = this.pendingAcks.get(entry.channelId);+ if (current?.messageId === entry.messageId) {+ this.pendingAcks.delete(entry.channelId);+ const state = this.callbacks.getState(entry.channelId);+ if (state?.inFlightAckMessageId === entry.messageId) {+ state.inFlightAckMessageId = null;+ }+ }+ }+ } catch (error) {+ this.callbacks.reportError(error);+ const now = Date.now();+ for (const entry of entries) {+ const current = this.pendingAcks.get(entry.channelId);+ if (current == null || current.messageId !== entry.messageId) {+ continue;+ }+ const attempt = current.attempt + 1;+ const retryDelay = Math.min(ACK_RETRY_MAX_DELAY_MS, ACK_RETRY_BASE_DELAY_MS * 2 ** (attempt - 1));+ current.attempt = attempt;+ current.deadline = now + retryDelay;+ }+ }+ }+}diff --git a/fluxer_app/src/features/read_state/state/read_states/shared.ts b/fluxer_app/src/features/read_state/state/read_states/shared.tsindex 5c5ee4a4d..ca075f9c4 100644--- a/fluxer_app/src/features/read_state/state/read_states/shared.ts+++ b/fluxer_app/src/features/read_state/state/read_states/shared.ts@@ -1,11 +1,6 @@// SPDX-License-Identifier: AGPL-3.0-or-laterimport {compare as compareSnowflakes, extractTimestamp} from '@fluxer/snowflake/src/SnowflakeUtils';export const ACK_BATCH_DELAY_MS = 3000;-export const ACK_BATCH_SIZE = 100;-export const ACK_RETRY_BASE_DELAY_MS = 5000;-export const ACK_RETRY_MAX_DELAY_MS = 60000;--export type Timer = NodeJS.Timeout;export interface GatewayReadState {id: string;@@ -45,14 +40,6 @@ export interface AppliedAck {hadMentions: boolean;}-export interface PendingAck {- channelId: string;- messageId: string;- deadline: number;- attempt: number;- userId: string | null;-}-export interface ArchivedReadState {ackMessageId: string | null;acknowledgedPinTimestamp: number;@@ -89,9 +76,10 @@ export function normalizeCount(count: number | undefined | null): number {export function compareReadStateVersions(a?: string | null, b?: string | null): number {const left = normalizeVersion(a);const right = normalizeVersion(b);- if (left == null || right == null) return 0;- if (left.length !== right.length) return left.length < right.length ? -1 : 1;- return left === right ? 0 : left < right ? -1 : 1;+ if (left == null || right == null) {+ return 0;+ }+ return compareSnowflakes(left, right);}export function chunkEntries<T>(entries: Array<T>, size: number): Array<Array<T>> {