scalarx-javareactive-programmingrx-scalabackpressure

Backpressure observable based on resource scarcity


In RxJava 1 / RxScala, how can I throttle/backpressure a source observable in the following situation?

def fast: Observable[Foo] // Supports backpressure

def afterExpensiveOp: Observable[Bar] = 
    fast.flatMap(foo => Observable.from(expensiveOp(foo))

// Signature and behavior is out of my control
def expensiveOp(foo: Foo)(implicit ec: ExecutionContext): Future[Bar] = {
   if(noResources()) Future.failed(new OutOfResourcesException())
   else Future { Bar() }
}

A possible solution is just blocking until. Which works,but that is very inelegant and prevents multiple simultaneous requests:

def afterExpensiveOp: Observable[Bar] = fast.flatMap(foo => 
   Observable.just(Observable.from(expensiveOp(foo)).toBlocking.head)
)

Solution

  • flatMap has a parameter to limit the number of concurrent subscribers. If you use this flatMap takes care of the backpressure for you.

    def afterExpensiveOp = fast.flatMap(safeNumberOfConccurrentExpensiveOps, x => Observable.from(expensiveOp(x)))