From 6d86a2753d2e0bf88e70cd903897240eb25c813e Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Thu, 19 Jul 2018 18:17:00 +0200 Subject: [PATCH] feat: retry gitlab 429 for up to 5 minutes Retries every minute up to 5 times if receiving 429 from gitlab. Closes #1578 --- lib/platform/gitlab/gl-got-wrapper.js | 44 ++++++++++++++++++--------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/platform/gitlab/gl-got-wrapper.js b/lib/platform/gitlab/gl-got-wrapper.js index daae254205..b5d8e04b89 100644 --- a/lib/platform/gitlab/gl-got-wrapper.js +++ b/lib/platform/gitlab/gl-got-wrapper.js @@ -1,5 +1,7 @@ const URL = require('url'); const glGot = require('gl-got'); +const delay = require('delay'); + const parseLinkHeader = require('parse-link-header'); const endpoints = require('../../util/endpoints'); @@ -18,24 +20,36 @@ async function get(path, options, retries = 5) { return cache[path]; } logger.debug({ path }, method.toUpperCase()); - const res = await glGot(path, opts); - if (opts.paginate) { - // Check if result is paginated - try { - const linkHeader = parseLinkHeader(res.headers.link); - if (linkHeader && linkHeader.next) { - res.body = res.body.concat( - (await get(linkHeader.next.url, opts, retries)).body - ); + try { + const res = await glGot(path, opts); + if (opts.paginate) { + // Check if result is paginated + try { + const linkHeader = parseLinkHeader(res.headers.link); + if (linkHeader && linkHeader.next) { + res.body = res.body.concat( + (await get(linkHeader.next.url, opts, retries)).body + ); + } + } catch (err) { + logger.warn({ err }, 'Pagination error'); } - } catch (err) { - logger.warn({ err }, 'Pagination error'); } + if (method === 'get' && path.startsWith('projects/')) { + cache[path] = res; + } + return res; + } catch (err) /* istanbul ignore next */ { + if (retries < 1) { + throw err; + } + if (err.statusCode === 429) { + logger.info(`Sleeping 1 minute before retrying 429`); + await delay(60000); + return get(path, opts, retries - 1); + } + throw err; } - if (method === 'get' && path.startsWith('projects/')) { - cache[path] = res; - } - return res; } const helpers = ['get', 'post', 'put', 'patch', 'head', 'delete']; -- GitLab