Skip to content

Commit

Permalink
add request timeout to prevent blocking queues
Browse files Browse the repository at this point in the history
  • Loading branch information
devxpy committed Jan 26, 2022
1 parent 080a379 commit e7e7321
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
const metascraper = require("metascraper")([
require("metascraper-title")(),
require("metascraper-description")(),
require("metascraper-image")()
require("metascraper-image")(),
]);
const got = require("got");
const zmq = require("zeromq");
const twitter = require("twitter-text");

require("dotenv").config();
const BIND_ADDR = process.env.BIND_ADDR;
const REQUEST_TIMEOUT_MS = parseInt(process.env.REQUEST_TIMEOUT_SEC) * 1000;

(async function() {
(async function () {
const sock = zmq.socket("rep");
await sock.bind(BIND_ADDR);
console.log(`serving @ "${BIND_ADDR}"`);

sock.on("message", async function(msg) {
sock.on("message", async function (msg) {
msg = JSON.parse(msg);
console.log("<", msg);

Expand Down Expand Up @@ -49,6 +50,13 @@ async function dispatch({ cmd, data }) {
}

async function fetchMetadata(targetUrl) {
const { body: html, url } = await got(targetUrl);
const { body: html, url } = await got(targetUrl, {
timeout: {
request: REQUEST_TIMEOUT_MS,
},
retry: {
limit: 0,
},
});
return await metascraper({ html, url });
}

0 comments on commit e7e7321

Please sign in to comment.