Files
rondevu-client/src/utils/exponential-backoff.ts
Bas van den Aakster 3e55166cea Reorganize src/ directory into feature/domain-based structure
Restructure flat src/ directory (17 files) into organized folders:

Structure:
- src/core/ - Main API (rondevu.ts, offer-pool.ts, types.ts, index.ts)
- src/connections/ - WebRTC connections (base.ts, offerer.ts, answerer.ts, config.ts, events.ts)
- src/api/ - HTTP layer (client.ts, batcher.ts)
- src/crypto/ - Crypto adapters (adapter.ts, node.ts, web.ts)
- src/utils/ - Utilities (async-lock.ts, exponential-backoff.ts, message-buffer.ts)

Changes:
- Move all 17 files to appropriate feature folders
- Update all import paths to reflect new structure
- Update package.json main/types to point to dist/core/index.js
- Preserve git history with git mv

Benefits:
- Clear separation of concerns
- Easier navigation and maintenance
- Better scalability for future features
- Logical grouping of related files

🤖 Generated with Claude Code
2025-12-16 22:51:23 +01:00

60 lines
1.7 KiB
TypeScript

/**
* Exponential backoff utility for connection reconnection
*/
export interface BackoffConfig {
base: number // Base delay in milliseconds
max: number // Maximum delay in milliseconds
jitter: number // Jitter factor (0-1) to add randomness
}
export class ExponentialBackoff {
private attempt: number = 0
constructor(private config: BackoffConfig) {
if (config.jitter < 0 || config.jitter > 1) {
throw new Error('Jitter must be between 0 and 1')
}
}
/**
* Calculate the next delay based on the current attempt number
* Formula: min(base * 2^attempt, max) with jitter
*/
next(): number {
const exponentialDelay = this.config.base * Math.pow(2, this.attempt)
const cappedDelay = Math.min(exponentialDelay, this.config.max)
// Add jitter: delay ± (jitter * delay)
const jitterAmount = cappedDelay * this.config.jitter
const jitter = (Math.random() * 2 - 1) * jitterAmount // Random value between -jitterAmount and +jitterAmount
const finalDelay = Math.max(0, cappedDelay + jitter)
this.attempt++
return Math.round(finalDelay)
}
/**
* Get the current attempt number
*/
getAttempt(): number {
return this.attempt
}
/**
* Reset the backoff state
*/
reset(): void {
this.attempt = 0
}
/**
* Peek at what the next delay would be without incrementing
*/
peek(): number {
const exponentialDelay = this.config.base * Math.pow(2, this.attempt)
const cappedDelay = Math.min(exponentialDelay, this.config.max)
return cappedDelay
}
}