Skip to content

Startup retry loop for nbxplorer connection#1083

Open
s373nZ wants to merge 2 commits into
masterfrom
nbxplorer-connect-retry
Open

Startup retry loop for nbxplorer connection#1083
s373nZ wants to merge 2 commits into
masterfrom
nbxplorer-connect-retry

Conversation

@s373nZ
Copy link
Copy Markdown
Contributor

@s373nZ s373nZ commented May 26, 2026

Avoids an arkd-wallet crash and restart by ensuring nbxplorer can serve RPCs before continuing with a connection. Retries 30 times at 5 second intervals for a total of 2.5 minutes, leaving the orchestrator to restart afterward.

Summary by CodeRabbit

  • New Features
    • Introduced a bounded connection retry mechanism for the Bitcoin service: multiple reconnection attempts with wait intervals, per-attempt warnings on failure, and clearer error reporting when all retries are exhausted.

Review Change Stack

Avoids an `arkd-wallet` crash by ensuring nbxplorer can serve RPCs
before continuing with a connection. Retries 30 times at 5 second
intervals for a total of 2.5 minutes, leaving the orchestrator to
restart afterward.
@s373nZ s373nZ self-assigned this May 26, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 26, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7092fc1b-a7c8-40bb-8a8b-be7503e77a67

📥 Commits

Reviewing files that changed from the base of the PR and between 138424a and 6c964d4.

📒 Files selected for processing (1)
  • pkg/arkd-wallet/core/infrastructure/nbxplorer/service.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/arkd-wallet/core/infrastructure/nbxplorer/service.go

Walkthrough

The NBXplorer service initializer now implements retry logic for establishing connectivity. Package constants define maximum retries and backoff intervals; the New function replaces a single status check with a bounded retry loop that logs per-attempt warnings and sleeps between attempts.

Changes

NBXplorer Connection Initialization

Layer / File(s) Summary
Connection retry with configurable backoff
pkg/arkd-wallet/core/infrastructure/nbxplorer/service.go
Retry constants and retry loop in New replace single immediate GetBitcoinStatus call, adding bounded retries with sleep intervals, per-attempt warning logs, and explicit exhaustion error.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: adding a retry loop for nbxplorer connection startup, which aligns with the PR objectives of verifying nbxplorer readiness before establishing a connection.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch nbxplorer-connect-retry

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@arkanaai arkanaai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approve — Startup retry loop for nbxplorer connection

Scope: Infrastructure startup code only. Not protocol-critical. No VTXO/signing/forfeit/round/exit paths touched.

What it does: Wraps the GetBitcoinStatus call in New() with a retry loop — 30 attempts × 5s = 2.5 min max wait — so arkd-wallet doesn't crash-loop when nbxplorer takes a moment to come up.

Assessment

The change is correct, minimal, and solves a real operational problem. The retry logic is straightforward with no subtle bugs.

Nits (non-blocking)

  1. No context propagation in retry loopservice.go:77: The loop uses context.Background() and time.Sleep(). If the process receives a shutdown signal during startup, it'll block up to 2.5 minutes before giving up. Ideally New() would accept a context.Context (or at least the retry sleep would use a time.After/select pattern). This is a pre-existing design limitation since New() never took a context, so not blocking this PR on it — but worth a follow-up.

  2. No test coverage — There are zero *_test.go files under pkg/arkd-wallet/core/infrastructure/nbxplorer/. The retry logic is simple enough to review by inspection, but a unit test with a mock HTTP server that fails N times then succeeds would be nice. Again, non-blocking given the simplicity.

  3. Fixed interval vs backoff — 5s fixed interval is fine for "waiting for a dependent service to start." No need for exponential backoff here.

Verified

  • Constants are reasonable (30 retries × 5s = 2.5 min)
  • Loop terminates correctly on both success and max retries
  • Error message on final failure includes attempt count
  • Warning log on each retry includes attempt number and error
  • status is properly used after the loop (line svc.minRelayTxFee = status.MinRelayTxFee)
  • Single caller in config.go:126 — no API change, no downstream breakage
  • No cross-repo impact (internal infrastructure, unexported behavior change)

LGTM. Ship it.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
pkg/arkd-wallet/core/infrastructure/nbxplorer/service.go (1)

77-78: 🏗️ Heavy lift

Consider accepting a context parameter to enable cancellation during initialization.

The retry loop uses context.Background(), which creates an uncancellable 2.5-minute initialization window. If the service needs to shut down during this period, it must wait for all retries to complete.

Accepting a context parameter would enable:

  • Graceful shutdown during startup
  • Configurable timeouts per deployment environment
  • Easier testing with context cancellation
Suggested approach

Modify the function signature to accept a context:

-func New(rawURL string) (ports.Nbxplorer, error) {
+func New(ctx context.Context, rawURL string) (ports.Nbxplorer, error) {
 	rawURL = strings.TrimSuffix(rawURL, "/")
 	// ... URL validation ...
 	
 	for attempt := 1; attempt <= nbxplorerMaxRetries; attempt++ {
-		status, err = svc.GetBitcoinStatus(context.Background())
+		status, err = svc.GetBitcoinStatus(ctx)
 		if err == nil {
 			break
 		}
+		
+		if ctx.Err() != nil {
+			return nil, fmt.Errorf("context cancelled during nbxplorer initialization: %w", ctx.Err())
+		}

This requires updating call sites (e.g., in pkg/arkd-wallet/config/config.go).

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/arkd-wallet/core/infrastructure/nbxplorer/service.go` around lines 77 -
78, Change the initialization function that contains the retry loop (the
function in pkg/arkd-wallet/core/infrastructure/nbxplorer/service.go that calls
svc.GetBitcoinStatus and uses nbxplorerMaxRetries) to accept a context.Context
parameter instead of using context.Background(); replace context.Background()
with the passed ctx when calling svc.GetBitcoinStatus, check
ctx.Done()/ctx.Err() inside the retry loop and return early with a canceled
error if context is canceled, and propagate the new ctx parameter to all callers
(e.g., update the call site in pkg/arkd-wallet/config/config.go) so startup can
be canceled or timed out.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@pkg/arkd-wallet/core/infrastructure/nbxplorer/service.go`:
- Around line 77-78: Change the initialization function that contains the retry
loop (the function in pkg/arkd-wallet/core/infrastructure/nbxplorer/service.go
that calls svc.GetBitcoinStatus and uses nbxplorerMaxRetries) to accept a
context.Context parameter instead of using context.Background(); replace
context.Background() with the passed ctx when calling svc.GetBitcoinStatus,
check ctx.Done()/ctx.Err() inside the retry loop and return early with a
canceled error if context is canceled, and propagate the new ctx parameter to
all callers (e.g., update the call site in pkg/arkd-wallet/config/config.go) so
startup can be canceled or timed out.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: fcd375f5-ed94-400a-a488-512d604c6bc4

📥 Commits

Reviewing files that changed from the base of the PR and between 299b7ad and 138424a.

📒 Files selected for processing (1)
  • pkg/arkd-wallet/core/infrastructure/nbxplorer/service.go

Copy link
Copy Markdown
Contributor

@Dunsin-cyber Dunsin-cyber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 138424a

Comment thread pkg/arkd-wallet/core/infrastructure/nbxplorer/service.go
Copy link
Copy Markdown
Contributor

@arkanaai arkanaai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review after 6c964d41 — cosmetic whitespace fix only (aligned = in constant block). No logic change. Previous approval stands.

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