-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved `retryMiddleware` with thunk retries delays and force fetch
- Loading branch information
Showing
5 changed files
with
118 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,115 @@ | ||
/* eslint-disable no-console */ | ||
import fetchWithRetries from 'fbjs/lib/fetchWithRetries'; | ||
|
||
function fetchOnError(request) { | ||
return fetchWithRetries(request.url, request).then((response) => response); | ||
const timeoutError = new Error("fetch timeout"); | ||
|
||
function isFunction(value) { | ||
return !!(value && value.constructor && value.call && value.apply); | ||
} | ||
|
||
function promiseTimeoutDelay(promise, timeoutMS, delayMS = 0, forceRetryWhenDelay) { | ||
return new Promise((resolve, reject) => { | ||
const timeoutPromise = () => { | ||
const timeoutId = setTimeout(() => { | ||
reject(timeoutError); | ||
}, timeoutMS); | ||
|
||
promise.then( | ||
(res) => { | ||
clearTimeout(timeoutId); | ||
resolve(res); | ||
}, | ||
(err) => { | ||
clearTimeout(timeoutId); | ||
reject(err); | ||
} | ||
); | ||
}; | ||
|
||
if (delayMS > 0) { | ||
let delayInProgress = true; | ||
const delayId = setTimeout(() => { | ||
delayInProgress = false; | ||
timeoutPromise(); | ||
}, delayMS); | ||
|
||
if (forceRetryWhenDelay) { | ||
forceRetryWhenDelay(() => { | ||
if (delayInProgress) { | ||
clearTimeout(delayId); | ||
timeoutPromise(); | ||
} | ||
}, delayMS); | ||
} | ||
} else { | ||
timeoutPromise(); | ||
} | ||
}); | ||
} | ||
|
||
export default function retryMiddleware(opts = {}) { | ||
const fetchTimeout = opts.fetchTimeout || false; | ||
const retryDelays = opts.retryDelays || false; | ||
const fetchTimeout = opts.fetchTimeout || 15000; | ||
const retryDelays = opts.retryDelays || [1000, 3000]; | ||
const statusCodes = opts.statusCodes || false; | ||
const logger = opts.logger || console.log.bind(console, '[RELAY-NETWORK]'); | ||
const allowMutations = opts.allowMutations || false; | ||
const forceRetry = opts.forceRetry || false; | ||
|
||
return next => req => ( | ||
next(req).then(res => { | ||
if (res.status < 200 || res.status > 300 || !res.status.ok) { | ||
const request = Object.assign({}, req); | ||
|
||
if (fetchTimeout || retryDelays) { | ||
request.fetchTimeout = fetchTimeout; | ||
request.retryDelays = retryDelays; | ||
} | ||
if (statusCodes && statusCodes.indexOf(res.status) !== -1) { | ||
return fetchOnError(request); | ||
} else if (!statusCodes) { | ||
return fetchOnError(request); | ||
let retryAfterMs = () => false; | ||
if (retryDelays) { | ||
if (Array.isArray(retryDelays)) { | ||
retryAfterMs = (attempt) => { | ||
if (retryDelays.length >= attempt) { | ||
return retryDelays[attempt - 1]; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
} else if (isFunction(retryDelays)) { | ||
retryAfterMs = retryDelays; | ||
} | ||
} | ||
|
||
|
||
return next => req => { | ||
if (req.relayReqType === 'mutation' && !allowMutations) { | ||
return next(req); | ||
}) | ||
); | ||
} | ||
|
||
let attempt = 0; | ||
|
||
const sendTimedRequest = (timeout, delay = 0) => { | ||
attempt++; | ||
return promiseTimeoutDelay(next(req), timeout, delay, forceRetry) | ||
.then(res => { | ||
let statusError = false; | ||
if (statusCodes) { | ||
statusError = statusCodes.indexOf(res.status) !== -1; | ||
} else { | ||
statusError = res.status < 200 || res.status > 300; | ||
} | ||
|
||
if (statusError) { | ||
const retryDelayMS = retryAfterMs(attempt); | ||
if (retryDelayMS) { | ||
logger(`response status ${res.status}, retrying after ${retryDelayMS} ms`); | ||
return sendTimedRequest(timeout, retryDelayMS); | ||
} | ||
} | ||
|
||
return res; | ||
}) | ||
.catch(err => { | ||
if (err === timeoutError) { | ||
const retryDelayMS = retryAfterMs(attempt); | ||
if (retryDelayMS) { | ||
logger(`response timeout, retrying after ${retryDelayMS} ms`); | ||
return sendTimedRequest(timeout, retryDelayMS); | ||
} | ||
} | ||
|
||
return new Promise((resolve, reject) => reject(err)); | ||
}); | ||
}; | ||
|
||
return sendTimedRequest(fetchTimeout, 0); | ||
}; | ||
} |