From 4a2731fbbd6e85c87995cac0259f3f5cc5a18a58 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 20 Jan 2026 05:33:03 +0000 Subject: [PATCH] fix(defaults): address PR #10 code review feedback - Integrate RETRY_BACKOFF_DELAYS and RETRY_JITTER_FACTOR into retry-utils.ts - Add retryConfigs.rsocket using the default delays - Add getRetryDelayWithJitter() helper function - Add getAllRetryDelaysWithJitter() for planning - Document DEFAULT_CACHE_MAX_KEYS with usage example - Adjust INITIAL_PREKEY_COUNT from 30 to 200 (moderate value) - Adjust MIN_UPLOAD_INTERVAL from 60s to 10s (balance rate limiting/responsiveness) - Revert syncFullHistory to true (avoid breaking change) --- src/Defaults/index.ts | 17 +++++++++++------ src/Utils/retry-utils.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/Defaults/index.ts b/src/Defaults/index.ts index 006f9bb4..820737ff 100644 --- a/src/Defaults/index.ts +++ b/src/Defaults/index.ts @@ -60,8 +60,8 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = { fireInitQueries: true, auth: undefined as unknown as AuthenticationState, markOnlineOnConnect: true, - // Conservative default - set to true only if you need full message history - syncFullHistory: false, + // Set to false if you don't need full message history (reduces bandwidth/storage) + syncFullHistory: true, patchMessageBeforeSending: msg => msg, shouldSyncHistoryMessage: () => true, shouldIgnoreJid: () => false, @@ -128,12 +128,12 @@ export const MEDIA_KEYS = Object.keys(MEDIA_PATH_MAP) as MediaType[] export const MIN_PREKEY_COUNT = 5 -// Conservative prekey count (upstream uses 812, but 30 is safer for rate limiting) -export const INITIAL_PREKEY_COUNT = 30 +// Moderate prekey count (upstream uses 812, reduced to balance rate limiting and availability) +export const INITIAL_PREKEY_COUNT = 200 export const UPLOAD_TIMEOUT = 30000 // 30 seconds -// Conservative upload interval to avoid rate limiting (was 5000) -export const MIN_UPLOAD_INTERVAL = 60_000 // 60 seconds minimum between uploads +// Moderate upload interval to balance rate limiting and responsiveness (was 5000) +export const MIN_UPLOAD_INTERVAL = 10_000 // 10 seconds minimum between uploads /** * Cache TTL configuration (in seconds) @@ -148,6 +148,11 @@ export const DEFAULT_CACHE_TTLS = { /** * Maximum cache keys per store type - prevents memory leaks * Based on RSocket's battle-tested configuration + * + * Usage: Use these limits when initializing LRU caches to prevent unbounded growth + * Example: + * import { DEFAULT_CACHE_MAX_KEYS } from './Defaults' + * const cache = new LRUCache({ max: DEFAULT_CACHE_MAX_KEYS.SIGNAL_STORE }) */ export const DEFAULT_CACHE_MAX_KEYS = { SIGNAL_STORE: 10_000, diff --git a/src/Utils/retry-utils.ts b/src/Utils/retry-utils.ts index 68edcb88..72677105 100644 --- a/src/Utils/retry-utils.ts +++ b/src/Utils/retry-utils.ts @@ -16,6 +16,7 @@ import { EventEmitter } from 'events' import { metrics } from './prometheus-metrics.js' import type { CircuitBreaker } from './circuit-breaker.js' +import { RETRY_BACKOFF_DELAYS, RETRY_JITTER_FACTOR } from '../Defaults/index.js' /** * Backoff strategies @@ -631,6 +632,41 @@ export const retryConfigs = { jitter: 0.1, shouldRetry: retryPredicates.onNetworkError, }, + + /** + * RSocket-style retry (uses RETRY_BACKOFF_DELAYS from Defaults) + * Delays: 1s, 2s, 5s, 10s, 20s with jitter + */ + rsocket: { + maxAttempts: RETRY_BACKOFF_DELAYS.length, + baseDelay: RETRY_BACKOFF_DELAYS[0], + maxDelay: RETRY_BACKOFF_DELAYS[RETRY_BACKOFF_DELAYS.length - 1], + backoffStrategy: 'exponential' as const, + jitter: RETRY_JITTER_FACTOR, + }, +} + +/** + * Get retry delay with jitter applied + * Uses RETRY_BACKOFF_DELAYS and RETRY_JITTER_FACTOR from Defaults + * + * @param attempt - Current attempt number (1-based) + * @returns Delay in ms with jitter applied + */ +export function getRetryDelayWithJitter(attempt: number): number { + const index = Math.min(Math.max(attempt - 1, 0), RETRY_BACKOFF_DELAYS.length - 1) + const baseDelay = RETRY_BACKOFF_DELAYS[index] ?? RETRY_BACKOFF_DELAYS[0] ?? 1000 + const jitterRange = baseDelay * RETRY_JITTER_FACTOR + const jitter = (Math.random() * 2 - 1) * jitterRange // ±15% + return Math.round(baseDelay + jitter) +} + +/** + * Get all retry delays with jitter for planning + * @returns Array of delays with jitter applied + */ +export function getAllRetryDelaysWithJitter(): number[] { + return RETRY_BACKOFF_DELAYS.map((_, i) => getRetryDelayWithJitter(i + 1)) } export default retry