0

在 RxJava 1 / RxScala 中,如何在以下情况下限制/背压可观察的源?

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() }
}

一个可能的解决方案就是阻塞直到。哪个有效,但这非常不雅,并且会阻止多个同时请求:

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

1 回答 1

0

flatMap 有一个参数来限制并发订阅者的数量。如果你使用这个 flatMap 会为你处理背压。

def afterExpensiveOp = fast.flatMap(safeNumberOfConccurrentExpensiveOps, x => Observable.from(expensiveOp(x)))
于 2016-11-15T09:25:37.643 回答