Using `onBackpressureLatest` to drop intermediate messages in blocking Flowable

你。 提交于 2019-12-25 00:26:31

问题


I have a chain where I do some blocking IO calls (e.g. HTTP-call). I want the blocking call to consume a value, proceed without interrupting, but drop everything that is piling up meanwhile, and then consume the next value in the same manner.

Consider the following example:

fun main() {
  Flowable.interval(100, TimeUnit.MILLISECONDS).onBackpressureLatest().map {
    Thread.sleep(1000)
    it
  }.blockingForEach { println(it) }
}

From a naive point of view, I would it expect to print something like 0, 10, 20, ..., but it prints 0, 1, 2, ....

What am I doing wrong?

EDIT:

I thought about naively adding debounce to eat up the incoming stream:

fun main() {
  Flowable.interval(100, TimeUnit.MILLISECONDS)
    .debounce(0, TimeUnit.MILLISECONDS)
    .map {
      Thread.sleep(1000)
      it
    }
    .blockingForEach { println(it) }
}

But, now I get a java.lang.InterruptedException: sleep interrupted.

EDIT:

What seems to work is the following:

fun main() {
  Flowable.interval(100, TimeUnit.MILLISECONDS)
    .throttleLast(0, TimeUnit.MILLISECONDS)
    .map {
      Thread.sleep(1000)
      it
    }
    .blockingForEach { println(it) }
}

The output is as expected 0, 10, 20, ...!!

Is that the correct way?

I noted that throttleLast will switch to the Computation-Scheduler. Is there a way to go back to the original scheduler?

EDIT:

I also get an occasional java.lang.InterruptedException: sleep interrupted with that variant.


回答1:


The most simple approach to solve the problem is:

fun <T> Flowable<T>.lossy() : Flowable<T> {
  return onBackpressureLatest().observeOn(Schedulers.io(), false, 1)
}

By calling lossy on a Flowable it starts to drop all element that are coming in faster than the downstream consumer can process.



来源:https://stackoverflow.com/questions/55606278/using-onbackpressurelatest-to-drop-intermediate-messages-in-blocking-flowable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!