Tech stack: rx-java
1.1.x, retrofit
1.9.x, spring
4.3.x.
A bit of context: I'm pretty new to rx-java. My service A has an API endpoint that makes a search call to service B that is frequently used, and fails a bit too often then it should. Some of the errors are pretty clear timeouts from other different services deep down, that took >30s, but quite a lot of them are pretty quick ones, around <1s.
What exactly I'm trying to do: Retry only the calls that fail under given threshold (let's say <1s), ideally the ones returning 5xx HTTP code responses.
Ideas that came to my mind, but do not solve the problem:
Regular Observable.timeout()
seems of no use, because for now I don't want to touch (interrupt) calls that are taking longer. I only want to retry those that came back as failed (5XX
response), not interrupt the longer ones.
retry()
seems of no use, because I don't want to simply retry every failed call.
retryWhen()
could be of use, but I am not sure how can I extract the HTTP from a Throwable and what exactly should I measure in the Observable call.
Code:
@RestController
@RequestMapping(...)
public class MyController {
@RequestMapping(method = GET)
public DeferredResult<MyJsonWrapper> fetchSomething() {
MySearchRequest searchRequest,
BindingResult bindingResult,
HttpServletRequest request) {
return new MyDeferredResult(
serviceB.searchSomething(...)
.doOnNext( result -> /* log size of search */ ));
}
serviceB.searchSomething(...)
also returns Observable<MyJsonWrapper>
What is MyDeferredResult:
class MyDeferredResult<T> extends DeferredResult<T> {
public MyDeferredResult(Observable<T> observable) {
onTimeout(this::handleTimeout);
ConnectableObservable<T> publication = observable.publish();
publication.subscribe(this::onNext, this::onError, this::onCompleted);
publication.connect(subscription -> this.subscription = subscription);
}
(...)
private void handleTimeout() {
setErrorResult(new MyTimeoutException( /* some info about request */ ));
subscription.unsubscribe();
}
How can I retry only the requests that failed under 1s that are 5xx HTTP responses?