Skip to content

Commit

Permalink
Add ability to retry Graphql requests (#26)
Browse files Browse the repository at this point in the history
* Retry twice

* Retry multiple times

* Extract util functions

* Fix typo

Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>

* Increase delay between retries using exponential backoff

* Regenerate pnpm lockfile to pease CI

---------

Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>
  • Loading branch information
joselcvarela and rijkvanzanten authored Jul 10, 2023
1 parent 030b5ff commit b542588
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
35 changes: 34 additions & 1 deletion gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chalk = require('chalk');
const { Directus } = require('@directus/sdk');
const { sourceNodes, createSchemaCustomization } = require('gatsby-source-graphql/gatsby-node');
const { createRemoteFileNode } = require('gatsby-source-filesystem');
const nodeFetch = require('node-fetch').default;

/**
* Validate plugin options
Expand Down Expand Up @@ -32,6 +33,7 @@ exports.pluginOptionsSchema = ({ Joi }) => {
}),
graphql: Joi.object(),
concurrency: Joi.number().default(10),
retries: Joi.number().default(5),
});
};

Expand Down Expand Up @@ -149,10 +151,11 @@ class Plugin {
this.refreshInterval = 0;
this.authPromise = null;
this.concurrency = 10;
this.retries = 5;
}

async setOptions(options) {
const { url, dev, auth, concurrency } = options;
const { url, dev, auth, concurrency, retries } = options;

if (isEmpty(url)) error('"url" must be defined');

Expand Down Expand Up @@ -202,6 +205,7 @@ class Plugin {

this.options = options;
this.concurrency = concurrency;
this.retries = retries;

return this.authPromise;
}
Expand All @@ -219,6 +223,31 @@ class Plugin {
typeName: this.options?.type?.name || 'DirectusData',
fieldName: this.options?.type?.field || 'directus',
headers: this.headers.bind(this),
fetch: async (uri, options) => {
function request() {
return nodeFetch(uri, options);
}

let count = 0;
let response = null;
let error = null;

while (response === null && count++ < this.retries) {
try {
response = await request();
} catch (err) {
error = err;
}

if (count > 0) {
await sleep(Math.pow(2, count) * 1000);
}
}

if (response === null) throw error;

return response;
},
};
}

Expand Down Expand Up @@ -317,4 +346,8 @@ function warning(message) {
Log.warning(message);
}

function sleep(ms) {
return new Promise((res) => setTimeout(res, ms));
}

const plugin = new Plugin();
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
],
"dependencies": {
"@directus/sdk": "10.2.0",
"ms": "2.1.3"
"ms": "2.1.3",
"node-fetch": "3.3.1"
},
"peerDependencies": {
"eslint": "7||8",
"gatsby-source-filesystem": "4||5",
"gatsby-source-graphql": "4||5",
"eslint": "7||8"
"gatsby-source-graphql": "4||5"
},
"repository": "directus/gatsby-source-directus",
"bugs": {
Expand Down
54 changes: 53 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b542588

Please sign in to comment.