I am trying to use the SwiftNIO async APIs (i.e. NIOAsyncChannel) to create a client library that communicates with a server using a proprietary TCP protocol. The inbound and outbound properties of NIOAsyncChannel are perfect for this, but have been deprecated in favor of executeThenClose(_:).

I don't want to use executeThenClose(_:) because it locks the lifetime of the channel to the scope of the closure supplied to executeThenClose(_:). This is a problem for my use case because the consumer of my library may have its own event loop or callback mechanism (e.g. for processing users' requests) that requires a channel remain open longer.

Any suggestions? Would it be possible for the inbound and outbound properties to be un-deprecated to support this scenario? Thanks!

The desire is to push users strongly towards a structured concurrency pattern. That is one where it isn't possible to leak the underlying connections, or to have them close at unexpected times, and that requires the scoped access of a with block.

The best solution to what you want here is to use a Structured Concurrency approach. In that case, you'd launch your NIOAsyncChannel in a task, and then communicate between that task and the user's code using AsyncSequences. You could use AsyncStream for that task, but any AsyncSequence will do.

1 Like

Thank you. I will try the approach you described.