mirror of
https://github.com/xtr-dev/rondevu-client.git
synced 2025-12-19 23:23:23 +00:00
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
This commit is contained in:
59
src/utils/exponential-backoff.ts
Normal file
59
src/utils/exponential-backoff.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user