Reduce .onBackpressureLatest() cache from 127 to 1
See original GitHub issueHi!
Could you please explain how does buffer(bufferOpenings, bufferClosingSelector)
work?
I want to use Observables (especially, interval()
) as triggers to do some long-lasting job.
In easy words, trigger observable emits an item, it’s being processed and while it is processed, my trigger emits 5 more items. When the first item is processed, I don’t care, how many items were emitted, I only want to know if there were items emitted or not. In the former case, I want to start processing, in the latter case - I want to wait until the next trigger emission.
I have looked into the backpressure, but RxJava caches the items before backpressure strategies are applied (127 items are cached, other are dropped/buffered).
All other operators are also only delaying the items, but send to the downstream all of them.
The last working solution was to:
val lock = BehaviorSubject.create(true)
fun run() = triggerObservable
.buffer { lock }
.observeOn(Schedulers.io())
.map {
if (!it.isEmpty()) {
Thread.sleep(5000) // time consuming operation
}
lock.onNext(true)
}
The problems here are with the second case in the image:
- If I move the
lock.onNext(true)
to theif()
, buffer will stuck if there were no trigger emissions beforeThread.sleep(5000)
. - If I leave it outside
if()
, it will run in a loop eating device power and be doing nothing until there is a new trigger emission.
I have thought, that I can keep the buffer opened (emit the items instantly) when no items are being processed. And to start buffering when an item is being processed, then opening the “gate” again.
What can you suggest here? Thanks!
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
There is an overload to observeOn which allows you to limit the number of items buffered by it.
Looks like this question has been answered. If you have further input on the issue, don’t hesitate to reopen this issue or post a new one.