@@ -40,7 +40,8 @@ type RequestParams<T> = {
40
40
resolve : ( body : T ) => void ,
41
41
reject : ( err : Error ) => void
42
42
) => void ,
43
- callback ?: ( err : ?Error , res : any , body : any ) => void
43
+ callback ?: ( err : ?Error , res : any , body : any ) => void ,
44
+ retryAttempts ?: number
44
45
} ;
45
46
46
47
type RequestOptions = {
@@ -78,6 +79,7 @@ export default class RequestManager {
78
79
79
80
params . method = params . method || "GET" ;
80
81
params . forever = true ;
82
+ params . retryAttempts = 0 ;
81
83
82
84
params . headers = Object . assign ( {
83
85
"User-Agent" : constants . USER_AGENT
@@ -110,18 +112,24 @@ export default class RequestManager {
110
112
*/
111
113
112
114
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 ( ) ;
115
117
if ( err . code === "ENOTFOUND" && possibleOfflineChange ) {
116
118
// can't resolve a domain
117
119
return true ;
118
120
}
119
121
122
+ // used to be able to resolve this domain! something is wrong
120
123
if ( err . code === "ENOTFOUND" && successHosts [ err . hostname ] ) {
121
124
// can't resolve this domain but we've successfully resolved it before
122
125
return true ;
123
126
}
124
127
128
+ // network was previously offline and we can't resolve the domain
129
+ if ( err . code === "ENOTFOUND" && controlOffline ) {
130
+ return true ;
131
+ }
132
+
125
133
// TODO: detect timeouts
126
134
127
135
return false ;
@@ -133,7 +141,7 @@ export default class RequestManager {
133
141
*/
134
142
135
143
queueForOffline ( opts : RequestOptions ) {
136
- if ( this . offlineQueue . length ) {
144
+ if ( ! this . offlineQueue . length ) {
137
145
this . reporter . warn ( "There appears to be trouble with your network connection. Retrying..." ) ;
138
146
this . initOfflineRetry ( ) ;
139
147
}
@@ -147,24 +155,11 @@ export default class RequestManager {
147
155
*/
148
156
149
157
initOfflineRetry ( ) {
150
- let requeue = ( ) = > {
158
+ setTimeout ( ( ) => {
151
159
let queue = this . offlineQueue ;
152
160
this . offlineQueue = [ ] ;
153
161
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 ) ;
168
163
}
169
164
170
165
/**
@@ -212,7 +207,9 @@ export default class RequestManager {
212
207
let req = new Request ( params ) ;
213
208
214
209
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 ;
216
213
if ( params . cleanup ) params . cleanup ( ) ;
217
214
this . queueForOffline ( opts ) ;
218
215
} else {
0 commit comments