danmactough / node-feedparser

Robust RSS, Atom, and RDF feed parsing in Node.js
Other
1.97k stars 192 forks source link

Stop feedparser from doing its thing? #175

Closed wle8300 closed 8 years ago

wle8300 commented 8 years ago

Is there a way to stop feedparser?

Example: I'm parsing an rss source but they have a really slow server. My request is now taking over 20 minutes.

This isn't acceptable to me, so I want to implement a setTimeout that will: 1) stop feedparser so end won't accidentally invoke my callback 2) invoke callback with an "client timed out" error

danmactough commented 8 years ago

@williamle8300 To stop feedparser, you just stop sending data. You will want to put the timeout around your request (not the parser). Something like this:

var onDoneCalled = false;
var timer;

function onDone (err) {
  if (timer) {
    clearTimeout(timer);
    timer = null;
  }
  if (!onDoneCalled) {
    onDoneCalled = true;
    return yourCallback(err);
  }
  // ignore or maybe log -- but avoid calling your callback more than once
  return;
}

timer = setTimeout(function () {
  var eTimedOut = new Error("client timed out");
  onDone(eTimedOut);
}, 20 * 60 * 1000 /* 20 minutes */);

request( /* ... */).pipe(feedparser).on("end", onDone);