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)
This commit is contained in:
+11
-6
@@ -60,8 +60,8 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = {
|
|||||||
fireInitQueries: true,
|
fireInitQueries: true,
|
||||||
auth: undefined as unknown as AuthenticationState,
|
auth: undefined as unknown as AuthenticationState,
|
||||||
markOnlineOnConnect: true,
|
markOnlineOnConnect: true,
|
||||||
// Conservative default - set to true only if you need full message history
|
// Set to false if you don't need full message history (reduces bandwidth/storage)
|
||||||
syncFullHistory: false,
|
syncFullHistory: true,
|
||||||
patchMessageBeforeSending: msg => msg,
|
patchMessageBeforeSending: msg => msg,
|
||||||
shouldSyncHistoryMessage: () => true,
|
shouldSyncHistoryMessage: () => true,
|
||||||
shouldIgnoreJid: () => false,
|
shouldIgnoreJid: () => false,
|
||||||
@@ -128,12 +128,12 @@ export const MEDIA_KEYS = Object.keys(MEDIA_PATH_MAP) as MediaType[]
|
|||||||
|
|
||||||
export const MIN_PREKEY_COUNT = 5
|
export const MIN_PREKEY_COUNT = 5
|
||||||
|
|
||||||
// Conservative prekey count (upstream uses 812, but 30 is safer for rate limiting)
|
// Moderate prekey count (upstream uses 812, reduced to balance rate limiting and availability)
|
||||||
export const INITIAL_PREKEY_COUNT = 30
|
export const INITIAL_PREKEY_COUNT = 200
|
||||||
|
|
||||||
export const UPLOAD_TIMEOUT = 30000 // 30 seconds
|
export const UPLOAD_TIMEOUT = 30000 // 30 seconds
|
||||||
// Conservative upload interval to avoid rate limiting (was 5000)
|
// Moderate upload interval to balance rate limiting and responsiveness (was 5000)
|
||||||
export const MIN_UPLOAD_INTERVAL = 60_000 // 60 seconds minimum between uploads
|
export const MIN_UPLOAD_INTERVAL = 10_000 // 10 seconds minimum between uploads
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cache TTL configuration (in seconds)
|
* Cache TTL configuration (in seconds)
|
||||||
@@ -148,6 +148,11 @@ export const DEFAULT_CACHE_TTLS = {
|
|||||||
/**
|
/**
|
||||||
* Maximum cache keys per store type - prevents memory leaks
|
* Maximum cache keys per store type - prevents memory leaks
|
||||||
* Based on RSocket's battle-tested configuration
|
* 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 = {
|
export const DEFAULT_CACHE_MAX_KEYS = {
|
||||||
SIGNAL_STORE: 10_000,
|
SIGNAL_STORE: 10_000,
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
import { EventEmitter } from 'events'
|
import { EventEmitter } from 'events'
|
||||||
import { metrics } from './prometheus-metrics.js'
|
import { metrics } from './prometheus-metrics.js'
|
||||||
import type { CircuitBreaker } from './circuit-breaker.js'
|
import type { CircuitBreaker } from './circuit-breaker.js'
|
||||||
|
import { RETRY_BACKOFF_DELAYS, RETRY_JITTER_FACTOR } from '../Defaults/index.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Backoff strategies
|
* Backoff strategies
|
||||||
@@ -631,6 +632,41 @@ export const retryConfigs = {
|
|||||||
jitter: 0.1,
|
jitter: 0.1,
|
||||||
shouldRetry: retryPredicates.onNetworkError,
|
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
|
export default retry
|
||||||
|
|||||||
Reference in New Issue
Block a user