Implement RPC request batching and throttling

Adds automatic request batching to reduce HTTP overhead by combining
multiple RPC calls into a single request.

Features:
- RpcBatcher class for intelligent request batching
- Configurable batch size (default: 10 requests)
- Configurable wait time (default: 50ms)
- Throttling to prevent overwhelming the server (default: 10ms)
- Automatic flushing when batch is full
- Enabled by default, can be disabled via options

Changes:
- Created rpc-batcher.ts with RpcBatcher class
- Updated RondevuAPI to use batcher by default
- Added batching option to RondevuOptions
- Updated README with batching documentation
- Bumped version to 0.16.0

Example usage:
  // Default (batching enabled with defaults)
  const rondevu = new Rondevu({ apiUrl: 'https://api.ronde.vu' })

  // Custom batching settings
  const rondevu = new Rondevu({
    apiUrl: 'https://api.ronde.vu',
    batching: { maxBatchSize: 20, maxWaitTime: 100 }
  })

  // Disable batching
  const rondevu = new Rondevu({
    apiUrl: 'https://api.ronde.vu',
    batching: false
  })

This can reduce HTTP requests by up to 90% during intensive operations
like ICE candidate exchange.

🤖 Generated with Claude Code
https://claude.com/claude-code

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-12 20:42:02 +01:00
parent d55abf2b63
commit 7223e45b98
5 changed files with 202 additions and 9 deletions

View File

@@ -1,4 +1,4 @@
import { RondevuAPI, Keypair, Service, ServiceRequest, IceCandidate } from './api.js'
import { RondevuAPI, Keypair, Service, ServiceRequest, IceCandidate, BatcherOptions } from './api.js'
import { CryptoAdapter } from './crypto-adapter.js'
export interface RondevuOptions {
@@ -6,6 +6,7 @@ export interface RondevuOptions {
username?: string // Optional, will generate anonymous if not provided
keypair?: Keypair // Optional, will generate if not provided
cryptoAdapter?: CryptoAdapter // Optional, defaults to WebCryptoAdapter
batching?: BatcherOptions | false // Optional, defaults to enabled with default options
}
export interface PublishServiceOptions {
@@ -55,17 +56,20 @@ export class Rondevu {
private keypair: Keypair | null = null
private usernameClaimed = false
private cryptoAdapter?: CryptoAdapter
private batchingOptions?: BatcherOptions | false
constructor(options: RondevuOptions) {
this.apiUrl = options.apiUrl
this.username = options.username || this.generateAnonymousUsername()
this.keypair = options.keypair || null
this.cryptoAdapter = options.cryptoAdapter
this.batchingOptions = options.batching
console.log('[Rondevu] Constructor called:', {
username: this.username,
hasKeypair: !!this.keypair,
publicKey: this.keypair?.publicKey
publicKey: this.keypair?.publicKey,
batchingEnabled: options.batching !== false
})
}
@@ -99,8 +103,14 @@ export class Rondevu {
console.log('[Rondevu] Using existing keypair, publicKey:', this.keypair.publicKey)
}
// Create API instance with username, keypair, and crypto adapter
this.api = new RondevuAPI(this.apiUrl, this.username, this.keypair, this.cryptoAdapter)
// Create API instance with username, keypair, crypto adapter, and batching options
this.api = new RondevuAPI(
this.apiUrl,
this.username,
this.keypair,
this.cryptoAdapter,
this.batchingOptions
)
console.log('[Rondevu] Created API instance with username:', this.username)
}