Add random timeout up to 500ms
This commit is contained in:
@@ -6,6 +6,10 @@ const {
|
|||||||
SCRAPER_API_BASE_URL
|
SCRAPER_API_BASE_URL
|
||||||
} = require("../config/appConfig");
|
} = require("../config/appConfig");
|
||||||
|
|
||||||
|
const timeout = (ms) => {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
const fetch = async (url, options = {}) => {
|
const fetch = async (url, options = {}) => {
|
||||||
const newOptions = Object.assign({}, options);
|
const newOptions = Object.assign({}, options);
|
||||||
if (!newOptions["headers"]) {
|
if (!newOptions["headers"]) {
|
||||||
@@ -21,7 +25,8 @@ const fetch = async (url, options = {}) => {
|
|||||||
const urlAdaptedForScraping = USE_SCRAPER_API
|
const urlAdaptedForScraping = USE_SCRAPER_API
|
||||||
? `${SCRAPER_API_BASE_URL}?api_key=${SCRAPER_API_KEY}&url=${urlToFetchThroughAPI}`
|
? `${SCRAPER_API_BASE_URL}?api_key=${SCRAPER_API_KEY}&url=${urlToFetchThroughAPI}`
|
||||||
: url;
|
: url;
|
||||||
|
const randomPauseMS = Math.floor(Math.random() * Math.floor(500));
|
||||||
|
await timeout(randomPauseMS);
|
||||||
return nodeFetch(urlAdaptedForScraping, newOptions);
|
return nodeFetch(urlAdaptedForScraping, newOptions);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user