Skip to content

Implement non-blocking trySend and tryReceive methods#262

Open
endrju19 wants to merge 6 commits intomainfrom
jox-try-send
Open

Implement non-blocking trySend and tryReceive methods#262
endrju19 wants to merge 6 commits intomainfrom
jox-try-send

Conversation

@endrju19
Copy link
Contributor

Summary

  • Implement non-blocking trySend/tryReceive (and OrClosed variants) on Channel, Sink, and Source for integration with non-blocking frameworks (Netty, Vert.x, etc.)
  • Add optimized implementations in Channel using CAS-based cell reservation with pre-check optimization to avoid unnecessary CAS attempts
  • Update downstream usages in FromFlowPublisher, KafkaDrain, and KafkaStage to use trySend instead of blocking send
  • Add Channel.estimateSize() for monitoring and observability
  • Add Fray deterministic concurrency tests and comprehensive unit tests covering all channel types
  • Add contention caveat to Javadoc and channels.md documentation

Resolves #187

Andrzej Kobyliński and others added 6 commits February 9, 2026 08:59
Add a non-blocking O(1) method that returns a best-effort estimate of
the number of elements in a channel, computed as the difference between
send and receive operations initiated. Designed for monitoring, metrics
export, and debugging - not for control flow.

Includes comprehensive Javadoc with anti-patterns, recommended usage,
and a monitoring code example. Tests cover buffered, unlimited, and
rendezvous channels, concurrent modification, closed/error states, and
waiting senders beyond buffer capacity.
Add non-blocking operations to the Channel implementation and the Source/Sink interfaces. These methods are essential for integration with event loops (e.g., Netty, Vert.x) where blocking the thread is prohibited.

- Added 'trySend(T)' and 'tryReceive()' to Channel, Source, and Sink.
- Implemented high-performance, lock-free versions in Channel.java.
- Provided default implementations in interfaces using 'Select' fallback for binary compatibility.
- Updated documentation with usage examples and clear guidance on non-blocking scenarios.
- Refactored 'flows' and 'kafka' modules to use the new API in critical non-blocking callbacks, removing unnecessary 'InterruptedException' handling.
…nels.md

Add explanatory comments for the transient RESUMING state in
tryUpdateCellReceive, matching the existing comments in the blocking
updateCellReceive variant. Revert unrelated cosmetic changes to
channels.md from the trySend/tryReceive PR, keeping only the new
non-blocking operations section and a contention caveat note.
…ered-values test

Clarify Sink/Source Javadoc: replace "spuriously" with explicit
explanation of what happens under contention. Add test verifying that
tryReceiveOrClosed returns ChannelError immediately when error() is
called on a channel with buffered values.
Remove narrating comments (CAS unsuccessful, reserving cell, etc.)
that just restate what the code does. Keep comments that explain why,
matching the style of the blocking variants.
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.

Feature Request: Add Non-blocking trySend() and tryReceive() to Channel

1 participant