Skip to content

event channel send helper#953

Open
bitcoin-coder-bob wants to merge 1 commit intomasterfrom
bob/event-channel-enhancement
Open

event channel send helper#953
bitcoin-coder-bob wants to merge 1 commit intomasterfrom
bob/event-channel-enhancement

Conversation

@bitcoin-coder-bob
Copy link
Collaborator

@bitcoin-coder-bob bitcoin-coder-bob commented Mar 4, 2026

requested via: #933 (comment)

Changes in pkg/client-lib/client/grpc/client.go:

  1. GetEventStream - send helper for BatchEventChannel: Consolidates all channel-send-or-ctx-cancel patterns into a single send() function. For terminal errors (ev.Err != nil), it uses a 5-second
    timeout instead of ctx.Done(), ensuring consumers always learn why the stream ended even if the context was already cancelled.
  2. GetTransactionsStream - send helper for TransactionEvent: Same consolidation pattern, replacing 5 separate select { case <-ctx.Done(): return; case eventsCh <- ... } blocks with calls to send().

Summary by CodeRabbit

  • Refactor
    • Improved internal event delivery and error handling consistency in streaming operations. Consolidated channel-send logic to ensure reliable event and error transmission with standardized backpressure handling. No public API changes.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 4, 2026

Walkthrough

A local helper function send(ev) is introduced to consolidate scattered event-sending logic across GetEventStream and GetTransactionsStream methods. The helper unifies handling of normal events and terminal errors with context-aware behavior, replacing multiple ad-hoc channel-send operations throughout these methods.

Changes

Cohort / File(s) Summary
Event Sending Consolidation
pkg/client-lib/client/grpc/client.go
Introduces a local send() helper function that encapsulates channel-send logic for both normal and terminal events. Replaces scattered channel sends in error paths and event delivery across GetEventStream and GetTransactionsStream with unified calls to the helper, which implements different backpressure/termination strategies depending on event type and includes a 5-second wait for terminal errors.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'event channel send helper' directly and concisely describes the main change—introducing a helper function for consolidating event channel sends.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bob/event-channel-enhancement

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.

@bitcoin-coder-bob bitcoin-coder-bob changed the title consolidates all channel-send-or-ctx-cancel patterns into a single se… event channel send helper Mar 4, 2026
@bitcoin-coder-bob bitcoin-coder-bob marked this pull request as ready for review March 4, 2026 16:52
Copy link
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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
pkg/client-lib/client/grpc/client.go (1)

299-300: Log when terminal batch error delivery times out.

These branches ignore send(...)'s boolean result. If the 5-second terminal send times out, the error reason is silently lost; adding a warning here would make failures diagnosable.

Also applies to: 322-323, 350-351

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/client-lib/client/grpc/client.go` around lines 299 - 300, The terminal
send calls like send(client.BatchEventChannel{Err: err}) currently ignore
send(...)'s boolean result so timed-out deliveries are dropped silently; update
these branches (the occurrences using send with client.BatchEventChannel{Err:
err} at the shown location and the similar spots around the other two
occurrences) to capture the bool result and, when false, emit a warning log that
includes context and the err value (e.g., "terminal batch error delivery timed
out" + err) so failed/timeouts are diagnosable.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@pkg/client-lib/client/grpc/client.go`:
- Around line 456-463: The send helper currently returns false immediately on
ctx.Done(), which can drop terminal TransactionEvent errors; change send (used
to push to eventsCh) so that for terminal events (the error/terminal kinds sent
on the branches at the locations around lines 474, 503, 544) it will perform a
blocking send to eventsCh (ignoring ctx.Done) to guarantee delivery, while
non-terminal events can keep the non-blocking/select behavior; also add a small
buffer when creating eventsCh to reduce brief consumer stalls. Update the send
function and the eventsCh creation accordingly (referencing send, eventsCh,
ctx.Done and the terminal event sends).

---

Nitpick comments:
In `@pkg/client-lib/client/grpc/client.go`:
- Around line 299-300: The terminal send calls like
send(client.BatchEventChannel{Err: err}) currently ignore send(...)'s boolean
result so timed-out deliveries are dropped silently; update these branches (the
occurrences using send with client.BatchEventChannel{Err: err} at the shown
location and the similar spots around the other two occurrences) to capture the
bool result and, when false, emit a warning log that includes context and the
err value (e.g., "terminal batch error delivery timed out" + err) so
failed/timeouts are diagnosable.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5d7733d0-1dc5-4a3b-92c8-2640f49bcd1b

📥 Commits

Reviewing files that changed from the base of the PR and between 8615412 and 3efd693.

📒 Files selected for processing (1)
  • pkg/client-lib/client/grpc/client.go

Comment on lines +456 to +463
send := func(ev client.TransactionEvent) bool {
select {
case eventsCh <- ev:
return true
case <-ctx.Done():
return false
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Avoid dropping terminal TransactionEvent errors on cancellation.

On Line 460, send returns false immediately on ctx.Done(), so terminal errors sent on Lines 474, 503, and 544 can still be lost. That weakens the transaction-event delivery guarantee.

Suggested adjustment
-		send := func(ev client.TransactionEvent) bool {
-			select {
-			case eventsCh <- ev:
-				return true
-			case <-ctx.Done():
-				return false
-			}
-		}
+		send := func(ev client.TransactionEvent) bool {
+			if ev.Err != nil {
+				timer := time.NewTimer(5 * time.Second)
+				defer timer.Stop()
+				select {
+				case eventsCh <- ev:
+					return true
+				case <-timer.C:
+					return false
+				}
+			}
+			select {
+			case eventsCh <- ev:
+				return true
+			case <-ctx.Done():
+				return false
+			}
+		}

Also consider a small buffer on eventsCh at creation time to improve terminal-event delivery under brief consumer stalls.

Based on learnings: "In the arkd codebase, transaction events should never be dropped when propagating to channels. The interface layer is designed to read events immediately, so buffering channels is preferred over non-blocking sends with drop semantics to maintain delivery guarantees."

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
send := func(ev client.TransactionEvent) bool {
select {
case eventsCh <- ev:
return true
case <-ctx.Done():
return false
}
}
send := func(ev client.TransactionEvent) bool {
if ev.Err != nil {
timer := time.NewTimer(5 * time.Second)
defer timer.Stop()
select {
case eventsCh <- ev:
return true
case <-timer.C:
return false
}
}
select {
case eventsCh <- ev:
return true
case <-ctx.Done():
return false
}
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/client-lib/client/grpc/client.go` around lines 456 - 463, The send helper
currently returns false immediately on ctx.Done(), which can drop terminal
TransactionEvent errors; change send (used to push to eventsCh) so that for
terminal events (the error/terminal kinds sent on the branches at the locations
around lines 474, 503, 544) it will perform a blocking send to eventsCh
(ignoring ctx.Done) to guarantee delivery, while non-terminal events can keep
the non-blocking/select behavior; also add a small buffer when creating eventsCh
to reduce brief consumer stalls. Update the send function and the eventsCh
creation accordingly (referencing send, eventsCh, ctx.Done and the terminal
event sends).

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.

1 participant