问题
Angular 7 docs provide this example of practical usage of rxjs Observables in implementing an exponential backoff for an AJAX request:
import { pipe, range, timer, zip } from 'rxjs';
import { ajax } from 'rxjs/ajax';
import { retryWhen, map, mergeMap } from 'rxjs/operators';
function backoff(maxTries, ms) {
return pipe(
retryWhen(attempts => range(1, maxTries)
.pipe(
zip(attempts, (i) => i),
map(i => i * i),
mergeMap(i => timer(i * ms))
)
)
);
}
ajax('/api/endpoint')
.pipe(backoff(3, 250))
.subscribe(data => handleData(data));
function handleData(data) {
// ...
}
While I understand the concept of both Observables and backoff, I can’t quite figure out, how exactly retryWhen will calculate time intervals for resubscribing to the source ajax.
Specifically, how do zip, map, and mapMerge work in this setup?
And what’s going to be contained in the attempts object when it’s emitted into retryWhen?
I went through their reference pages, but still can’t wrap my head around this.
回答1:
I have spent quite some time researching this (for learning purposes) and will try to explain the workings of this code as thoroughly as possible.
First, here’s the original code, annotated:
import { pipe, range, timer, zip } from 'rxjs';
import { ajax } from 'rxjs/ajax';
import { retryWhen, map, mergeMap } from 'rxjs/operators';
function backoff(maxTries, ms) { // (1)
return pipe( // (2)
retryWhen(attempts => range(1, maxTries) // (3)
.pipe(
zip(attempts, (i) => i), // (4)
map(i => i * i), // (5)
mergeMap(i => timer(i * ms)) // (6)
)
)
); // (7)
}
ajax('/api/endpoint')
.pipe(backoff(3, 250))
.subscribe(data => handleData(data));
function handleData(data) {
// ...
}
- Easy enough, we’re creating custom
backoffoperator out ofretryWhenoperator. We’ll be able to apply this later withinpipefunction. - In this context,
pipemethod returns a custom operator. Our custom operator is going to be a modified
retryWhenoperator. It takes a function argument. This function is going to be called once — specifically, when thisretryWhenis first encountered/invoked. By the way,retryWhengets into play only when the source observable produces an error. It then prevents error from propagating further and resubscribes to the source. If the source produces a non-error result (whether on first subscription or on a retry),retryWhenis passed over and is not involved.A few words on
attempts. It’s an observable. It is not the source observable. It is created specifically forretryWhen. It has one use and one use only: whenever subscription (or re-subscription) to the source observable results in an error,attemptsfires anext. We are givenattemptsand are free to use it in order to react in some way to each failed subscription attempt to the source observable.So that’s what we are going to do.
First we create
range(1, maxTries), an observable that has an integer for every retry we are willing to perform.rangeis ready to fire all it’s numbers right then and there, but we have to hold its horses: we only need a new number when another retry happens. So, that’s why we...... zip it with the
attempts. Meaning, marry each emitted value ofattemptswith a single value ofrange.Remember, function we’re currently in is going to be called only once, and at that time,
attemptswill have only firednextonce — for the initial failed subscription. So, at this point, our two zipped observables have produced just one value.Btw, what are the values of the two observables zipped into one? This function decides that:
(i) => i. For clarity it can be written(itemFromRange, itemFromAttempts) => itemFromRange. Second argument is not used, so it’s dropped, and first is renamed intoi.What happens here, is we simply disregard the values fired by
attempts, we are only interested in the fact that they are fired. And whenever that happens we pull the next value fromrangeobservable......and square it. This is for the exponential part of the exponential backoff.
So, now whenever (re-)subscription to source fails, we have an ever increasing integer on our hands (1, 4, 9, 16...). How do we transform that integer into a time delay until next re-subscription?
Remember, this function we are currently inside of, it must return an observable, using
attemptsas input. This resulting observable is only built once.retryWhenthen subscribes to that resulting observable and: retries subscribing to source observable whenever resulting observable firesnext; callscompleteorerroron source observable whenever resulting observable fires those corresponding events.Long story short, we need to make
retryWhenwait a bit. delay operator could maybe be used, but setting up exponential growth of the delay would likely be pain. Instead,mergeMapoperator comes into play.mergeMapis a shortcut for two operators combined:mapandmergeAll.mapsimply converts every increasing integer (1, 4, 9, 16...) into atimerobservable which firesnextafter passed number of milliseconds.mergeAllforcesretryWhento actually subscribe totimer. If that last bit didn’t happen, our resulting observable would just firenextimmediately withtimerobservable instance as value.At this point, we’ve built our custom observable which will be used by
retryWhento decide when exactly to attempt to re-subscribe to source observable.
As it stands I see two problems with this implementation:
As soon as our resulting observable fires its last
next(causing the last attempt to resubscribe), it also immediately firescomplete. Unless the source observable returns result very quickly (assuming that the very last retry will be the one that succeeds), that result is going to be ignored.This is because as soon as
retryWhenhearscompletefrom our observable, it callscompleteon source, which may still be in the process of making AJAX request.If all retries were unsuccessful, source actually calls
completeinstead of more logicalerror.
To solve both these issues, I think that our resulting observable should fire error at the very end, after giving the last retry some reasonable time to attempt to do its job.
Here’s my implementation of said fix, which also takes into account deprecation of zip operator in latest rxjs v6:
import { delay, dematerialize, map, materialize, retryWhen, switchMap } from "rxjs/operators";
import { concat, pipe, range, throwError, timer, zip } from "rxjs";
function backoffImproved(maxTries, ms) {
return pipe(
retryWhen(attempts => {
const observableForRetries =
zip(range(1, maxTries), attempts)
.pipe(
map(([elemFromRange, elemFromAttempts]) => elemFromRange),
map(i => i * i),
switchMap(i => timer(i * ms))
);
const observableForFailure =
throwError(new Error('Could not complete AJAX request'))
.pipe(
materialize(),
delay(1000),
dematerialize()
);
return concat(observableForRetries, observableForFailure);
})
);
}
I tested this code and it seems to work properly in all cases. I can’t be bothered to explain it in detail right now; I doubt anyone will even read the wall of text above.
Anyway, big thanks to @BenjaminGruenbaum and @cartant for setting me onto right path for wrapping my head around all this.
来源:https://stackoverflow.com/questions/53015170/exponential-backoff-implementation-with-rxjs