jox icon indicating copy to clipboard operation
jox copied to clipboard

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

Open magicprinc opened this issue 1 month ago • 1 comments

At least trySend() 🙏

TL;DR Add non-blocking trySend(T) and tryReceive() methods to Channel to enable integration with NIO frameworks (Netty, Vert.x, etc.), where blocking the event loop thread is prohibited.

  • Should return immediately without parking the virtual thread
  • Must be safe to call from platform threads (event loops)
  • Should respect channel capacity: return false/full for full buffered channels
  • Consider atomic operations for rendezvous channels (immediate failure if no waiting receiver)

e.g:

vertx.eventBus().consumer("events", msg -> {
    // Non-blocking channel operation in event loop
    if (channel.trySend(msg.body())) {
        msg.reply("Accepted");
    } else {
        msg.reply("Channel full, backpressure applied");
    }
});

Kotlin provides https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-send-channel/try-send.html and tryReceive() returning ChannelResult

Project Reactor has tryEmitNext() for similar NIO integration

magicprinc avatar Nov 08 '25 10:11 magicprinc

Yes we do want that :)

As a temporary work-around, you can use select+default clauses: https://github.com/softwaremill/jox/blob/main/channels/src/test/java/com/softwaremill/jox/SelectSendTest.java#L234-L250

(not as performant, though, as a native impl would be)

adamw avatar Nov 14 '25 10:11 adamw