-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module.exports = errorRequest | ||
|
||
async function errorRequest (octokit, error, options) { | ||
if (error.status === 500) { | ||
const retries = 3 | ||
const retryAfter = Math.pow((options.request.retryCount || 0) + 1, 2) | ||
throw octokit.retry.retryRequest(error, retries, retryAfter) | ||
} | ||
|
||
/* | ||
TODO: | ||
Add more cases here. | ||
Use the 500 error above as example. | ||
*/ | ||
|
||
throw error | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module.exports = retryPlugin | ||
|
||
const wrapRequest = require('./wrap-request') | ||
const errorRequest = require('./error-request') | ||
|
||
function retryPlugin (octokit) { | ||
const state = { | ||
retryAfterBaseValue: 1000 | ||
} | ||
|
||
octokit.retry = { | ||
_options: (options = {}) => Object.assign(state, options), | ||
retryRequest: (error, retries, retryAfter) => { | ||
error.request.request.retries = retries | ||
error.request.request.retryAfter = retryAfter | ||
return error | ||
} | ||
} | ||
|
||
octokit.hook.error('request', errorRequest.bind(null, octokit)) | ||
octokit.hook.wrap('request', wrapRequest.bind(null, state)) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module.exports = wrapRequest | ||
|
||
const Bottleneck = require('bottleneck/light') | ||
|
||
async function wrapRequest (state, request, options) { | ||
const limiter = new Bottleneck() | ||
|
||
limiter.on('failed', function (error, info) { | ||
const maxRetries = ~~error.request.request.retries | ||
const after = ~~error.request.request.retryAfter | ||
options.request.retryCount = info.retryCount + 1 | ||
|
||
if (maxRetries > info.retryCount) { | ||
return after * state.retryAfterBaseValue | ||
} | ||
}) | ||
|
||
return limiter.schedule(request, options) | ||
} |