Skip to content

peer selection - backoff when no peers available#458

Merged
freemans13 merged 1 commit intobsv-blockchain:mainfrom
freemans13:stu/no-available-peers-backoff
Feb 5, 2026
Merged

peer selection - backoff when no peers available#458
freemans13 merged 1 commit intobsv-blockchain:mainfrom
freemans13:stu/no-available-peers-backoff

Conversation

@freemans13
Copy link
Collaborator

@freemans13 freemans13 commented Jan 29, 2026

Summary

  • Add backoff mechanism to selectAndActivateNewPeer() to prevent busy-loop when peer selection fails

Problem

When selectAndActivateNewPeer() (called from handleRunningState()) fails to find a suitable sync peer, it simply returns without any backoff. This path runs every 2 seconds via the FSM monitor, and when all peers fail selection (e.g., health checks fail, all on cooldown), it creates a tight loop of:

  1. Check FSM state → RUNNING
  2. Call selectAndActivateNewPeer()
  3. All peers fail selection (health checks, cooldown, etc.)
  4. Return immediately
  5. Wait 2 seconds, repeat

Each iteration involves expensive health check HTTP requests for every peer.

Solution

  1. Add SyncAttemptCooldown to selection criteria - Makes selectAndActivateNewPeer() consistent with selectNewSyncPeer(), which already uses a 1-minute cooldown. Without this, peers could be retried immediately through this code path.

  2. Call enterBackoffMode() when no peer found - When peer selection fails (either no eligible peers or selector returns empty), enter exponential backoff. This uses the existing backoff mechanism (2s, 4s, 8s, etc.) to prevent hammering peers when none are available.

Test plan

  • Verified reduced CPU usage when all peers fail selection
  • Existing tests pass
  • Lint passes

🤖 Generated with Claude Code

@freemans13 freemans13 self-assigned this Jan 29, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Jan 29, 2026

🤖 Claude Code Review

Status: Complete

Summary: The changes look good and directly address the busy loop issue when no eligible peers are available. The addition of enterBackoffMode() calls at both decision points ensures the system backs off instead of repeatedly attempting peer selection.

Analysis:

  • Line 435: Added backoff when no eligible peers exist (banned, low reputation, etc.) - prevents immediate retry
  • Line 442: Added SyncAttemptCooldown: 1 * time.Minute - consistent with line 254 pattern
  • Line 451: Added backoff when peer selection fails (health checks, cooldown) - prevents tight loop

The backoff implementation is already present in the codebase (lines 669-688) with exponential backoff and peer attempt clearing, making this a straightforward fix to invoke it at the right points.

No issues found - the implementation correctly prevents CPU-intensive busy loops while maintaining sync functionality.

@sonarqubecloud
Copy link

@freemans13 freemans13 changed the title peer selection - backoff when no peers available rather than start a … peer selection - backoff when no peers available Feb 2, 2026
@freemans13 freemans13 merged commit 8a73894 into bsv-blockchain:main Feb 5, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants