Queueing and batching media HTTP requests for improved reliability.

This commit is contained in:
2020-02-22 03:22:30 +01:00
parent b2dfbac9e5
commit 349a5a506e
15 changed files with 251 additions and 78 deletions

8
src/utils/chunk.js Normal file
View File

@@ -0,0 +1,8 @@
'use strict';
function chunk(array, chunkSize) {
return Array.from({ length: Math.ceil(array.length / chunkSize) })
.map((value, index) => array.slice(index * chunkSize, (index * chunkSize) + chunkSize));
}
module.exports = chunk;

76
src/utils/http.js Normal file
View File

@@ -0,0 +1,76 @@
'use strict';
const bhttp = require('bhttp');
const taskQueue = require('promise-task-queue');
const logger = require('../logger')(__filename);
const queue = taskQueue();
queue.on('concurrencyReached:httpGet', () => {
logger.silly('Queueing GET requests');
});
queue.on('concurrencyReached:httpPost', () => {
logger.silly('Queueing POST requests');
});
queue.define('httpGet', async ({
url,
timeout = 30000,
options = {},
}) => {
logger.silly(`GET ${url}`);
const res = await bhttp.get(url, {
responseTimeout: timeout,
...options,
});
res.code = res.statusCode;
return res;
}, {
concurrency: 20,
});
queue.define('httpPost', async ({
url,
body,
timeout = 30000,
options = {},
}) => {
logger.silly(`POST ${url} with ${body}`);
const res = await bhttp.post(url, body, {
responseTimeout: timeout,
...options,
});
res.code = res.statusCode;
return res;
}, {
concurrency: 20,
});
async function get(url, options) {
return queue.push('httpGet', {
method: 'get',
url,
options,
});
}
async function post(url, body, options) {
return queue.push('httpPost', {
url,
body,
options,
});
}
module.exports = {
get,
post,
};

View File

@@ -2,7 +2,7 @@
const { JSDOM } = require('jsdom');
const moment = require('moment');
const bhttp = require('bhttp');
const http = require('./http');
function trim(str) {
return str.trim().replace(/\s+/g, ' ');
@@ -228,7 +228,7 @@ function extractAll(html, selector) {
}
async function get(url, selector, headers, all = false) {
const res = await bhttp.get(url, {
const res = await http.get(url, {
headers,
});