Skip to content

Commit db7c557

Browse files
Sebastian McKenziebestander
authored andcommitted
error out when failed network requests fail 5 times, improve possible offline error detection (#146)
1 parent a0c0e1f commit db7c557

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

src/util/request-manager.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ type RequestParams<T> = {
4040
resolve: (body: T) => void,
4141
reject: (err: Error) => void
4242
) => void,
43-
callback?: (err: ?Error, res: any, body: any) => void
43+
callback?: (err: ?Error, res: any, body: any) => void,
44+
retryAttempts?: number
4445
};
4546

4647
type RequestOptions = {
@@ -78,6 +79,7 @@ export default class RequestManager {
7879

7980
params.method = params.method || "GET";
8081
params.forever = true;
82+
params.retryAttempts = 0;
8183

8284
params.headers = Object.assign({
8385
"User-Agent": constants.USER_AGENT
@@ -110,18 +112,24 @@ export default class RequestManager {
110112
*/
111113

112114
isPossibleOfflineError(err: RequestError): boolean {
113-
let possibleOfflineChange = !controlOffline && network.isOffline();
114-
115+
// network was previously online but now we're offline
116+
let possibleOfflineChange = !controlOffline && !network.isOffline();
115117
if (err.code === "ENOTFOUND" && possibleOfflineChange) {
116118
// can't resolve a domain
117119
return true;
118120
}
119121

122+
// used to be able to resolve this domain! something is wrong
120123
if (err.code === "ENOTFOUND" && successHosts[err.hostname]) {
121124
// can't resolve this domain but we've successfully resolved it before
122125
return true;
123126
}
124127

128+
// network was previously offline and we can't resolve the domain
129+
if (err.code === "ENOTFOUND" && controlOffline) {
130+
return true;
131+
}
132+
125133
// TODO: detect timeouts
126134

127135
return false;
@@ -133,7 +141,7 @@ export default class RequestManager {
133141
*/
134142

135143
queueForOffline(opts: RequestOptions) {
136-
if (this.offlineQueue.length) {
144+
if (!this.offlineQueue.length) {
137145
this.reporter.warn("There appears to be trouble with your network connection. Retrying...");
138146
this.initOfflineRetry();
139147
}
@@ -147,24 +155,11 @@ export default class RequestManager {
147155
*/
148156

149157
initOfflineRetry() {
150-
let requeue = () => {
158+
setTimeout(() => {
151159
let queue = this.offlineQueue;
152160
this.offlineQueue = [];
153161
for (let opts of queue) this.execute(opts);
154-
};
155-
156-
if (!controlOffline && network.isOffline()) {
157-
// we were online before but now we aren't so let's use that as our check
158-
let interval = setInterval(function () {
159-
if (!network.isOffline()) {
160-
clearInterval(interval);
161-
requeue();
162-
}
163-
}, 500);
164-
} else {
165-
// just try again in 3 seconds
166-
setTimeout(requeue, 3000);
167-
}
162+
}, 3000);
168163
}
169164

170165
/**
@@ -212,7 +207,9 @@ export default class RequestManager {
212207
let req = new Request(params);
213208

214209
req.on("error", (err) => {
215-
if (this.isPossibleOfflineError(err)) {
210+
let attempts = params.retryAttempts || 0;
211+
if (attempts < 5 && this.isPossibleOfflineError(err)) {
212+
params.retryAttempts = attempts + 1;
216213
if (params.cleanup) params.cleanup();
217214
this.queueForOffline(opts);
218215
} else {

0 commit comments

Comments
 (0)