kotlinx.coroutines
kotlinx.coroutines copied to clipboard
Create way to get notified when emit or tryEmit is called on a SharedFlow whose buffer is already full
I'm working on a library for sensor monitoring and using SharedFlows for data streams from the sensors. In general it's not OK to miss sensor updates and the program should be configured so the buffer never fills up. Occasionally suspending on buffer overflow can be OK but often times even that is problematic, especially if it is happening often. Basically the buffer filling up means something in the hardware setup needs to be adjusted or the update rate needs to be reduced so the program can keep up. So there are many situations where I am OK with occasionally suspending or occasionally dropping a value but I need to be able to monitor when this is happening so either:
- The program can make adjustments dynamically.
- We can look over logs and see if we need to make adjustments to the underlying system.
- We can present to someone looking at a GUI in real time that we're skipping updates or slowing down the system and update rate should potentially be reduced.
The problem is there is only a single situation in which you can get any feedback about whether or not the buffer is full when attempting to emit and that is when onBufferOverflow is set to suspend and tryEmit is called. I need to always have access to this feedback mechanism in some way. Either through return value of tryEmit or registering a callback on the SharedFlow or watching another Flow that emits notifications. Of course return value on emit wouldn't work because the caller would be suspended.