Combining timeout () with retryWhen ()

I am creating a simple application to connect to bluetooth devices using the RxAndroidBle library (Cheers Guys for a great job!). What I am experiencing is sometimes when I connect to a device I get a Gatt error with a status of 133. I know this can happen, so I want to retry everything when this error occurs. This is not a problem, I can easily do it with a statement retryWhen()

, however I have another requirement - the thread should terminate after 30 seconds if no connection has been made. I used to do this timeout()

, but the problem is when restarting the timer starts again.

So the question is how to link the timeout () statement with retryWhen (), so I can repeat some specific error, but keep counting.

I have some ideas with a concatenation of observables or some separate observables that will check the state of the connection after a wait period, but I am pondering if I can do this in a single observable.

My code looks like this:

public Observable<ConnectingViewState> connectToDevice(String macAddress) {
        final RxBleDevice rxBleDevice = rxBleClient.getBleDevice(macAddress);
        return rxBleDevice.establishConnection(false)
                .subscribeOn(Schedulers.io())
                .map(rxBleConnection -> new ConnectingViewState.ConnectedViewState(rxBleConnection))
                .cast(ConnectingViewState.class)
                .timeout(40, TimeUnit.SECONDS)
                .startWith(new ConnectingViewState.ConnectingInProgressViewState())
                .retryWhen(errors -> errors.flatMap(error -> {
                            if (isDefaultGattError(error)) {
                                return Observable.just(new Object());
                            } else {
                                return Observable.error(error);
                            }
                        }
                ))
                .onErrorReturn(throwable -> new ConnectingViewState.ErrorState(throwable));
    }

      

+3


source to share


2 answers


The operator retryWhen

works by re-subscribing to the chain of operators above it. Since you posted yours timeout

before this, the specified timeout is re-signed and thus starts over again.



Placement timeout

after retryWhen

must use a global time-out for all the return flow.

+2


source


As discussed, I wrote a test with RxJava2. The code was taken from the book "Reactive Programming with RxJava" (p. 257)



private final static int ATTEMPTS = 10;

@Test
public void name() throws Exception {
    Subject<Integer> establishConnection = PublishSubject.create();
    TestScheduler testScheduler = new TestScheduler();

    Observable<Integer> timeout = establishConnection.
            retryWhen(failures -> failures
                    .zipWith(Observable.range(1, ATTEMPTS), (err, attempt) ->
                            {
                                // check here for your error if(...)

                                if (attempt < ATTEMPTS) {
                                    long expDelay = (long) Math.pow(2, attempt - 2);
                                    return Observable.timer(expDelay, TimeUnit.SECONDS, testScheduler);
                                } else {
                                    return Observable.error(err);
                                }
                            }
                    )
                    .flatMap(x -> x))
            .timeout(30, TimeUnit.SECONDS, testScheduler)
            .onErrorResumeNext(throwable -> {
                if (throwable instanceof TimeoutException) {
                    return Observable.just(42);
                }
                return Observable.error(throwable);
            });

    TestObserver<Integer> test = timeout.test();

    testScheduler.advanceTimeBy(10, TimeUnit.SECONDS);
    establishConnection.onError(new IOException("Exception 1"));

    testScheduler.advanceTimeBy(20, TimeUnit.SECONDS);
    establishConnection.onError(new IOException("Exception 2"));

    testScheduler.advanceTimeBy(31, TimeUnit.SECONDS);

    test.assertValue(42);
}

      

0


source







All Articles