question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reduce .onBackpressureLatest() cache from 127 to 1

See original GitHub issue

Hi!

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.

How it should work

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 the if(), buffer will stuck if there were no trigger emissions before Thread.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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
akarnokdcommented, Oct 3, 2017

There is an overload to observeOn which allows you to limit the number of items buffered by it.

0reactions
akarnokdcommented, Oct 5, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reactor 3 Reference Guide
MAJOR is the current generation of Reactor, where each new generation can bring fundamental changes to the structure of the project (which ...
Read more >
rxjava rxjava2 reactor_en2_3
getName() + " can drive " + vehicle.getName());. Observable<String> starships = Observable.from(luke.getStarshipsIds()) .flatMap(remoteApi::starship).
Read more >
Second Edition - Tutorialspoint
Section 1: Foundations of Reactive Programming in ... reduce(). 81. Boolean operators. 82 all(). 82 any(). 83. isEmpty() ... Replaying and caching.
Read more >
RxAndroid学习:初探_爱炒饭的博客
compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile ... or * another source before (and perhaps after) the application of cache().
Read more >
目录 - Gitee
cache ( ) — remember the sequence of items emitted by the Observable and emit ... one can reduce the likelihood of MissingBackpressureException...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found