When i have `audio processing producer consumer` s...
# coroutines
u
When i have
audio processing producer consumer
system. Typically in blocking world that is solved with 2 threads and a
RingBuffer
. Say now I want to do that in ktor server, where producer is a web socket. The ktor ws api is suspending. Does it make sense to implement this with `Channel`s? Don’t channels require immutable data? How would you go about it? Is it a good idea to flatten a byte array and send each byte into a Channel?
b
you don't need immutable data. you can make it work with channels, you can even make it work with flows. definitely better than blocking read method in a loop or something like that! but your emmision/production i think will also require a coroutine scope. i wouldnt relly directly on the execution associated with the websocket i would launch/queue things from there to the scope associated with the RingBuffer which i would continue to regard as the critical section! basically i would give the RingBuffer component an internal/intimate CoroutineScope and go from there.
omg channels dont have scoped collection like flows actually. ya i think i would use flow.
hope this helps you!
Copy code
val blockingCircularBuffer = MutableSharedFlow<String>(
        replay = 0, // No replay needed for a strict blocking buffer behavior
        extraBufferCapacity = bufferSize,
        onBufferOverflow = BufferOverflow.SUSPEND // This makes emit() block when full, you can change it to drop oldest/drop latest
    )
and you can launch collection in the same scope (or a different one) and write back to socket