'RxJava - timeout while using retryWhen

I need to send requests in 10s intervals. It means the next request should be sent in 10s after response from previous request has been received. The last request should start within 60 seconds. For example:

0:00: 1st request: sent
0:05: 1st request: response
0:15: 2nd request: sent
0:45: 2nd request: response
0:55: 3rd request: sent
1:10: 3rd request: response
No more requests

This is my code:

mainRepository.getFlightSearchResults(uuid)
                .repeatWhen { it.delay(10, TimeUnit.SECONDS) }
                .timeout(60, TimeUnit.SECONDS)
                .observeOn(schedulerProvider.ui)

where getFlightSearchResults returns Observable. Requests are being sent as described above, however requests are not stopping being sent after 60s. How can I stop sending (not receiving response) requests after 60s?



Solution 1:[1]

Solved using takeWhile:

val startTimeMillis = System.currentTimeMillis()
mainRepository.getFlightSearchResults(uuid)
                .repeatWhen { it.delay(10, TimeUnit.SECONDS) }
                .takeWhile { System.currentTimeMillis() - startTimeMillis < 60_000 }
                .observeOn(schedulerProvider.ui)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1