Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(network): include backoff, max number of retries in network recovery #4238

Merged
merged 1 commit into from
Oct 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions app/scripts/modules/core/src/api/network.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
IDeferred, IHttpInterceptor, IHttpPromiseCallbackArg, IHttpProvider, IPromise, IQService, IRequestConfig,
IWindowService, module
ITimeoutService, IWindowService, module
} from 'angular';
import { Dictionary } from 'lodash';

/**
* Handles two scenarios:
Expand All @@ -12,10 +13,12 @@ import {
export class NetworkInterceptor implements IHttpInterceptor {

private networkAvailable: IDeferred<void>;
private retryQueue: IRequestConfig[] = [];
private retryQueue: Dictionary<number> = {};
private MAX_RETRIES = 4;

constructor(private $q: IQService,
private $window: IWindowService,
private $timeout: ITimeoutService,
private $injector: any) {
'ngInject';
this.$window.addEventListener('offline', this.handleOffline);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need @BindAll() on the class, or use this.handleOffline.bind(this)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only if we want it to work

Expand All @@ -38,7 +41,7 @@ export class NetworkInterceptor implements IHttpInterceptor {
}

private removeFromQueue(config: IRequestConfig): void {
this.retryQueue = this.retryQueue.filter(c => c !== config);
delete this.retryQueue[config.url];
}

// see http://www.couchcoder.com/angular-1-interceptors-using-typescript for more details on why we need to do this
Expand All @@ -47,11 +50,18 @@ export class NetworkInterceptor implements IHttpInterceptor {
// the handlers"
public responseError = <T>(response: IHttpPromiseCallbackArg<T>): IPromise<T> => {
const { config, status } = response;
// status of -1 indicates the request was aborted, retry if we haven't already
if (status === -1 && !this.retryQueue.includes(config)) {
// status of -1 indicates the request was aborted, retry if we haven't already, with incremental backoff + jitter
const retryCount = this.retryQueue[config.url] || 0;
if (status === -1 && retryCount < this.MAX_RETRIES) {
this.retryQueue[config.url] = retryCount + 1;
return this.networkAvailable.promise.then(() => {
return this.$q.resolve(this.$injector.get('$http')(config))
.finally(() => this.removeFromQueue(config));
return this.$timeout(() => {
return this.$q.resolve(this.$injector.get('$http')(config))
.then((result: any) => {
this.removeFromQueue(config);
return result;
});
}, (retryCount + 1 + Math.random()) * 1000);
});
}
return this.$q.reject(response).finally(() => this.removeFromQueue(config));
Expand Down