fix backoff on server error
This commit is contained in:
parent
8e15298919
commit
1e6ebf252f
@ -1,7 +1,7 @@
|
||||
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpStatusCode } from '@angular/common/http';
|
||||
import { Inject, Injectable, Optional } from '@angular/core';
|
||||
import { MonoTypeOperatorFunction, Observable, pipe, retry, throwError, timer } from 'rxjs';
|
||||
import { catchError, mergeMap, tap } from 'rxjs/operators';
|
||||
import { MonoTypeOperatorFunction, Observable, retry, throwError, timer } from 'rxjs';
|
||||
import { catchError, tap } from 'rxjs/operators';
|
||||
import { MAX_RETRIES_ON_SERVER_ERROR } from './max-retries.token';
|
||||
import { ErrorService } from './error.service';
|
||||
import { KeycloakService } from 'keycloak-angular';
|
||||
@ -16,19 +16,19 @@ function updateSeconds(seconds: number) {
|
||||
|
||||
function backoffOnServerError(maxRetries = 3): MonoTypeOperatorFunction<HttpEvent<unknown>> {
|
||||
let seconds = 0;
|
||||
const timerExpiration = pipe(
|
||||
tap<HttpErrorResponse>(() => (seconds = updateSeconds(seconds))),
|
||||
mergeMap((error: HttpErrorResponse, index) => {
|
||||
if ((error.status < HttpStatusCode.InternalServerError && error.status !== 0) || index === maxRetries) {
|
||||
return throwError(() => error);
|
||||
}
|
||||
|
||||
console.error('An error occurred: ', error);
|
||||
console.error(`Retrying in ${seconds} seconds...`);
|
||||
return timer(seconds * 1000);
|
||||
}),
|
||||
);
|
||||
return retry({ delay: (errors: Observable<HttpErrorResponse>) => errors.pipe(timerExpiration) });
|
||||
function delay(error: HttpErrorResponse) {
|
||||
seconds = updateSeconds(seconds);
|
||||
if (error.status < HttpStatusCode.InternalServerError && error.status !== 0) {
|
||||
return throwError(() => error);
|
||||
}
|
||||
|
||||
console.error('An error occurred: ', error);
|
||||
console.error(`Retrying in ${seconds} seconds...`);
|
||||
return timer(seconds * 1000);
|
||||
}
|
||||
|
||||
return retry({ count: maxRetries, delay });
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user