Skip to content

Hardware layer: Add exponential backoff to auto-reconnect #101

@ng

Description

@ng

Background

Currently, HardwareClient.ensureConnected() will attempt to reconnect immediately when config.autoReconnect = true. If the hardware daemon is down, callers in retry loops (e.g., polling jobs) can hammer the socket path repeatedly.

Problem

  • No delay between reconnection attempts
  • No maximum retry limit
  • Can overwhelm system resources if daemon is persistently unavailable
  • Logs fill with connection errors

Proposed Solution

Add bounded retry logic with exponential backoff:

class HardwareClient {
  private reconnectAttempt = 0
  private lastReconnectTime = 0
  
  private async ensureConnected() {
    if (needs reconnect) {
      const now = Date.now()
      const backoff = Math.min(30000, 1000 * 2 ** this.reconnectAttempt)
      
      if (now - this.lastReconnectTime < backoff) {
        throw new HardwareError('Reconnect cooldown active')
      }
      
      this.reconnectAttempt++
      this.lastReconnectTime = now
      
      if (this.reconnectAttempt > 5) {
        throw new HardwareError('Max reconnect attempts exceeded')
      }
      
      await this.connect()
      this.reconnectAttempt = 0 // Reset on success
    }
  }
}

Configuration

Add to HardwareClientConfig:

  • maxReconnectAttempts (default: 5)
  • reconnectBackoffMs (default: 1000)
  • maxBackoffMs (default: 30000)

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions