Open-source and local-first Slack and Notion alternative that puts you in control of your data
TypeScript chatchat-applicationcrdteditorelectronknowledge-baselocal-firstnotionnotion-alternativerealtime-collaborationself-hostedslacksqliteteam-collaborationwikiyjs
Implement exponential backoff for api requests
Hakan Shehu2b3141c5d40e
diff --git a/desktop/src/electron/app-manager.ts b/desktop/src/electron/app-manager.tsindex 84c79d6d..21fc4a08 100644--- a/desktop/src/electron/app-manager.ts+++ b/desktop/src/electron/app-manager.ts@@ -303,7 +303,7 @@ class AppManager {}}} catch (error) {- console.error('Error in event loop:', error);+ // console.error('Error in event loop:', error);}setTimeout(this.executeEventLoop, EVENT_LOOP_INTERVAL);}diff --git a/desktop/src/electron/socket-manager.ts b/desktop/src/electron/socket-manager.tsindex 216aa9ce..4050a235 100644--- a/desktop/src/electron/socket-manager.ts+++ b/desktop/src/electron/socket-manager.ts@@ -2,6 +2,7 @@ import { WebSocket } from 'ws';import { Account } from '@/types/accounts';import { Server } from '@/types/servers';import { buildSynapseUrl } from '@/lib/servers';+import { BackoffCalculator } from '@/lib/backoff-calculator';export type SocketMessage = {type: string;@@ -12,17 +13,17 @@ export class SocketManager {private readonly server: Server;private readonly account: Account;private socket: WebSocket | null;- private closingCount: number;+ private backoffCalculator: BackoffCalculator;private listeners: Map<string, ((payload: any) => void)[]>;- private lastInitAt: number;+ private closingCount: number;constructor(server: Server, account: Account) {this.server = server;this.account = account;this.socket = null;- this.closingCount = 0;+ this.backoffCalculator = new BackoffCalculator();this.listeners = new Map();- this.lastInitAt = 0;+ this.closingCount = 0;}public init(): void {@@ -30,14 +31,10 @@ export class SocketManager {return;}- // Prevent multiple init calls in a short period- // init only once in 5 seconds- if (Date.now() - this.lastInitAt < 5000) {+ if (!this.backoffCalculator.canRetry()) {return;}- this.lastInitAt = Date.now();- this.closingCount = 0;this.socket = new WebSocket(buildSynapseUrl(this.server, this.account.deviceId),);@@ -61,8 +58,12 @@ export class SocketManager {}};- this.socket.onerror = (event) => {- console.error('Socket error:', event);+ this.socket.onopen = () => {+ this.backoffCalculator.reset();+ };++ this.socket.onerror = () => {+ this.backoffCalculator.increaseError();};}diff --git a/desktop/src/electron/workspace-manager.ts b/desktop/src/electron/workspace-manager.tsindex ca8e610e..f93cbbff 100644--- a/desktop/src/electron/workspace-manager.ts+++ b/desktop/src/electron/workspace-manager.ts@@ -24,7 +24,7 @@ import {ServerMutation,} from '@/types/mutations';import { eventBus } from '@/lib/event-bus';-import { AxiosInstance } from 'axios';+import { AxiosInstance, isAxiosError } from 'axios';import { debounce, isEqual } from 'lodash';import {ServerNode,@@ -32,12 +32,14 @@ import {ServerNodeReaction,} from '@/types/nodes';import { SelectNode } from '@/electron/schemas/workspace';+import { BackoffCalculator } from '@/lib/backoff-calculator';export class WorkspaceManager {private readonly workspace: Workspace;private readonly axios: AxiosInstance;private readonly database: Kysely<WorkspaceDatabaseSchema>;private readonly subscribers: Map<string, SubscribedQueryResult<unknown>>;+ private readonly backoffCalculator: BackoffCalculator;private readonly debouncedNotifyQuerySubscribers: (affectedTables: string[],) => void;@@ -46,6 +48,7 @@ export class WorkspaceManager {this.workspace = workspace;this.axios = axios;this.subscribers = new Map();+ this.backoffCalculator = new BackoffCalculator();const workspaceDir = `${accountPath}/${workspace.id}`;if (!fs.existsSync(workspaceDir)) {@@ -189,18 +192,22 @@ export class WorkspaceManager {}public async sendMutations(): Promise<void> {- const mutations = await this.database- .selectFrom('mutations')- .selectAll()- .orderBy('id asc')- .limit(20)- .execute();-- if (!mutations || mutations.length === 0) {+ if (!this.backoffCalculator.canRetry()) {return;}try {+ const mutations = await this.database+ .selectFrom('mutations')+ .selectAll()+ .orderBy('id asc')+ .limit(20)+ .execute();++ if (!mutations || mutations.length === 0) {+ return;+ }+const { status, data } =await this.axios.post<ServerExecuteMutationsResponse>('v1/mutations', {workspaceId: this.workspace.id,@@ -228,8 +235,12 @@ export class WorkspaceManager {.where('id', 'in', failedMutationIds).execute();}++ this.backoffCalculator.reset();} catch (error) {- console.error(error);+ if (isAxiosError(error)) {+ this.backoffCalculator.increaseError();+ }}}@@ -310,71 +321,84 @@ export class WorkspaceManager {}public async sync(): Promise<boolean> {- if (this.workspace.synced) {- return true;+ if (!this.backoffCalculator.canRetry()) {+ return false;}- const { data, status } = await this.axios.get<WorkspaceSyncData>(- `v1/${this.workspace.id}/sync`,- );+ try {+ if (this.workspace.synced) {+ return true;+ }- if (status !== 200) {- return false;- }+ const { data, status } = await this.axios.get<WorkspaceSyncData>(+ `v1/${this.workspace.id}/sync`,+ );- if (data.nodes.length === 0 && data.nodeReactions.length === 0) {- return true;- }+ if (status !== 200) {+ return false;+ }- await this.database.transaction().execute(async (trx) => {- if (data.nodes.length > 0) {- await trx.deleteFrom('nodes').execute();-- await trx- .insertInto('nodes')- .values(- data.nodes.map((node) => {- return {- id: node.id,- attributes: JSON.stringify(node.attributes),- state: node.state,- created_at: node.createdAt,- created_by: node.createdBy,- updated_by: node.updatedBy,- updated_at: node.updatedAt,- version_id: node.versionId,- server_created_at: node.serverCreatedAt,- server_updated_at: node.serverUpdatedAt,- server_version_id: node.versionId,- };- }),- )- .execute();+ if (data.nodes.length === 0 && data.nodeReactions.length === 0) {+ return true;}- if (data.nodeReactions.length > 0) {- await trx.deleteFrom('node_reactions').execute();-- await trx- .insertInto('node_reactions')- .values(- data.nodeReactions.map((nodeReaction) => {- return {- node_id: nodeReaction.nodeId,- reactor_id: nodeReaction.reactorId,- reaction: nodeReaction.reaction,- created_at: nodeReaction.createdAt,- server_created_at: nodeReaction.serverCreatedAt,- };- }),- )- .execute();+ await this.database.transaction().execute(async (trx) => {+ if (data.nodes.length > 0) {+ await trx.deleteFrom('nodes').execute();++ await trx+ .insertInto('nodes')+ .values(+ data.nodes.map((node) => {+ return {+ id: node.id,+ attributes: JSON.stringify(node.attributes),+ state: node.state,+ created_at: node.createdAt,+ created_by: node.createdBy,+ updated_by: node.updatedBy,+ updated_at: node.updatedAt,+ version_id: node.versionId,+ server_created_at: node.serverCreatedAt,+ server_updated_at: node.serverUpdatedAt,+ server_version_id: node.versionId,+ };+ }),+ )+ .execute();+ }++ if (data.nodeReactions.length > 0) {+ await trx.deleteFrom('node_reactions').execute();++ await trx+ .insertInto('node_reactions')+ .values(+ data.nodeReactions.map((nodeReaction) => {+ return {+ node_id: nodeReaction.nodeId,+ reactor_id: nodeReaction.reactorId,+ reaction: nodeReaction.reaction,+ created_at: nodeReaction.createdAt,+ server_created_at: nodeReaction.serverCreatedAt,+ };+ }),+ )+ .execute();+ }+ });++ this.workspace.synced = true;+ this.backoffCalculator.reset();+ this.debouncedNotifyQuerySubscribers(['nodes', 'node_reactions']);+ return true;+ } catch (error) {+ if (isAxiosError(error)) {+ this.backoffCalculator.increaseError();}- });+ }- this.workspace.synced = true;- this.debouncedNotifyQuerySubscribers(['nodes', 'node_reactions']);- return true;+ return false;}public async syncNodeFromServer(node: ServerNode): Promise<void> {diff --git a/desktop/src/lib/backoff-calculator.ts b/desktop/src/lib/backoff-calculator.tsnew file mode 100644index 00000000..5beb56fd--- /dev/null+++ b/desktop/src/lib/backoff-calculator.ts@@ -0,0 +1,44 @@+export class BackoffCalculator {+ private baseDelay: number = 5000; // Start at 5 seconds (5000 ms)+ private maxDelay: number = 600000; // Maximum delay is 10 minutes (600000 ms)+ private attempt: number = 0;+ private lastAttemptTime: number | null = null;++ constructor() {}++ // Increases the error count and sets the last attempt time+ public increaseError(): void {+ this.attempt += 1;+ this.lastAttemptTime = Date.now();+ }++ // Checks if the current time has passed the calculated backoff delay+ public canRetry(): boolean {+ if (this.attempt === 0 || this.lastAttemptTime === null) {+ return true; // Can retry immediately if no failures yet+ }+ const delay = this.getDelay();+ const timeSinceLastAttempt = Date.now() - this.lastAttemptTime;+ return timeSinceLastAttempt >= delay;+ }++ // Calculates the current backoff delay based on the number of attempts+ private getDelay(): number {+ const delay = Math.min(+ this.baseDelay * Math.pow(2, this.attempt - 1),+ this.maxDelay,+ );+ return delay;+ }++ // Resets the calculator after a successful attempt+ public reset(): void {+ this.attempt = 0;+ this.lastAttemptTime = null;+ }++ // Returns the current delay time for informational purposes+ public getCurrentDelay(): number {+ return this.getDelay();+ }+}