Wiznet - gracefully handle out of sockets?

Posted on
  • Is there any way to detect and handle the "out of sockets" error?

    If you have a number of events that can trigger HTTP requests, it's easy to end up with enough happening in succession (if a few events happen at about the same time) that you run out of sockets (or that seems to be the case? It just happened to me when I made a few requests in rapid succession, and I don't think I was doing anything that would break it...). Is there a way to catch and handle this (ie, by waiting a few minutes and retrying)? Or is there a way to check how many free sockets there are?

    Otherwise, it will be very hard to implement my current project, because it can generate http requests in response to a fairly wide variety of events, and it is very likely that these will sometimes happen in rapid succession (for example, when I open my door, I normally close it within a few seconds, since I have the A/C on...), and I don't want to have it missing events....

  • At the moment I'm afraid there isn't anything built-in. I should probably implement the error event on the HTTP request though, which would make this kind of thing a lot easier.

    Amazingly it doesn't seem to be listed as an event in the node.js docs despite being there, which is probably one reason I never implemented it. I'll add it to the bug list :)

    For now, you could actually look inside the CientRequest structure to see if it's managed to get a socket - the test will work even for 'unable to locate host' errors too. I think you should be able to do:

    var http = require("http");
    var req = http.get("http://www.espruino.com", function(res) {
      res.on('data', function(data) {
        console.log(data);
      });
    });
    if (!(req.sckt>0)) console.log("oops.");
    

    Other option is to make your own http get function that'd look a bit like:

    function get(url, callback) {
      var timeout = setTimeout(function() {
        timeout = undefined;
        console.log("Retrying");
        get(url, callback);
      }, 10000);
      http.get(url, function(res) {
       if (timeout===undefined) return; // too late, already retried
       clearTimeout(timeout);
       callback(res);
      });
    }
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Wiznet - gracefully handle out of sockets?

Posted by Avatar for DrAzzy @DrAzzy

Actions