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