From 4edb47491799760819ecf72783b1972baee14985 Mon Sep 17 00:00:00 2001 From: pedroanastacio Date: Thu, 8 Jan 2026 14:48:32 -0300 Subject: [PATCH] refactor(socket): removes unnecessary retry request logic --- packages/socket-server/src/socket/index.ts | 5 +- packages/socket-server/src/utils/index.ts | 1 - .../src/utils/retryWithBackoff.ts | 52 ------------------- 3 files changed, 2 insertions(+), 56 deletions(-) delete mode 100644 packages/socket-server/src/utils/retryWithBackoff.ts diff --git a/packages/socket-server/src/socket/index.ts b/packages/socket-server/src/socket/index.ts index 4a968e227..326814199 100644 --- a/packages/socket-server/src/socket/index.ts +++ b/packages/socket-server/src/socket/index.ts @@ -5,7 +5,7 @@ import { TransactionEventHandler } from '@modules/transactions' import { SwitchNetworkEventHandler } from '../modules/switchNetwork' import { SocketEvents, SocketUsernames } from '../types' -import { DatabaseClass, retryWithBackoff } from '@src/utils' +import { DatabaseClass } from '@src/utils' // import Redis from 'ioredis' // import { createAdapter } from '@socket.io/redis-adapter' @@ -92,8 +92,7 @@ export const setupSocket = (io: SocketIOServer, database: DatabaseClass, api: Ax const connectorRoom = `${sessionId}:${SocketUsernames.CONNECTOR}:${request_id}` try { - const connectionStateUrl = `/connections/${sessionId}/state` - const { data: connected } = await retryWithBackoff(() => api.get(connectionStateUrl), connectionStateUrl) + const { data: connected } = await api.get(`/connections/${sessionId}/state`) console.log('[SOCKET] [CONNECTION_STATE] Connected state for session', sessionId, '->', connected) diff --git a/packages/socket-server/src/utils/index.ts b/packages/socket-server/src/utils/index.ts index 0cfc2856c..e1ddd7b2f 100644 --- a/packages/socket-server/src/utils/index.ts +++ b/packages/socket-server/src/utils/index.ts @@ -1,2 +1 @@ export * from './database' -export * from './retryWithBackoff' diff --git a/packages/socket-server/src/utils/retryWithBackoff.ts b/packages/socket-server/src/utils/retryWithBackoff.ts deleted file mode 100644 index f400e9d96..000000000 --- a/packages/socket-server/src/utils/retryWithBackoff.ts +++ /dev/null @@ -1,52 +0,0 @@ -const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)) - -type RetryOptions = { - retries?: number - baseDelay?: number -} - -const DEFAULT_RETRIES = 3 -const DEFAULT_BASE_DELAY = 300 // ms - -/** - * Executes an async function with retry attempts and exponential backoff for 5xx errors. - * - * - Retries only for HTTP 5xx errors (status >= 500 and < 600). - * - The delay between attempts increases exponentially and includes random jitter. - * - By default, tries up to 3 times and starts with a 500ms delay. - * - Logs the url (if provided) on each retry attempt. - * - * @template T The return type of the async function - * @param fn The async function to execute - * @param url The url related to the request (for logging) - * @param options.retries Maximum number of attempts (default: 3) - * @param options.baseDelay Base delay in ms for exponential backoff (default: 300) - * @returns The result of the async function if any attempt succeeds - * @throws Rethrows the original error if not a 5xx error or if all attempts fail - */ -async function retryWithBackoff(fn: () => Promise, url?: string, { retries = DEFAULT_RETRIES, baseDelay = DEFAULT_BASE_DELAY }: RetryOptions = {}): Promise { - for (let attempt = 0; attempt <= retries; attempt++) { - try { - return await fn() - } catch (error) { - const status = error?.response?.status - - if (!status || status < 500 || status >= 600) { - throw error - } - - if (attempt === retries) { - throw error - } - - const jitter = Math.random() * 100 - const delay = baseDelay * Math.pow(2, attempt) + jitter - - console.warn(`[RETRY] Attempt ${attempt + 1}/${retries + 1} failed (status ${status})${url ? ` for URL: ${url}` : ''}. Retrying in ${Math.round(delay)}ms...`) - - await sleep(delay) - } - } -} - -export { retryWithBackoff }