From 0b7b35caf9ab62c790d1bf9ecf377e44f7d21800 Mon Sep 17 00:00:00 2001 From: Christoph Witzko Date: Fri, 30 Jun 2017 19:39:09 +0200 Subject: [PATCH] feat: retry github requests --- lib/github.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/github.js b/lib/github.js index 59adbd16..e4f92d3e 100644 --- a/lib/github.js +++ b/lib/github.js @@ -1,7 +1,33 @@ const url = require('url') const _ = require('lodash') -const Github = require('github') +const { promisify } = require('bluebird') +const promiseRetry = require('promise-retry') + +function ghRetry (ghapi) { + const httpSend = ghapi.prototype.httpSend + ghapi.prototype.httpSend = function (msg, block, callback) { + const send = promisify(httpSend.bind(this, msg, block)) + promiseRetry(retry => { + return send().catch(err => { + const type = err.code || err.message + if (!['ETIMEDOUT', 'ECONNREFUSED', 'ECONNRESET', 'ESOCKETTIMEDOUT'].includes(type)) { + throw err + } + + retry(err) + }) + }, { + retries: 5, + minTimeout: 3000 + }) + .then(res => callback(null, res)) + .catch(callback) + } + return ghapi +} + +const Github = ghRetry(require('github')) const githubHost = {}