Note that there are some explanatory texts on larger screens.

plurals
  1. PO“Proxying” a lot of HTTP requests with Node.js + Express 2
    text
    copied!<p>I'm writing proxy in Node.js + Express 2. Proxy should:</p> <ol> <li>decrypt POST payload and issue HTTP request to server based on result;</li> <li>encrypt reply from server and send it back to client.</li> </ol> <p>Encryption-related part works fine. The problem I'm facing is timeouts. Proxy should process requests in less than 15 secs. And most of them are under 500ms, actually.</p> <p>Problem appears when I increase number of parallel requests. Most requests are completed ok, but some are failed after 15 secs + couple of millis. <code>ab -n5000 -c300</code> works fine, but with concurrency of 500 it fails for some requests with timeout.</p> <p>I could only speculate, but it seems thant problem is an order of callbacks exectuion. Is it possible that requests that comes first are hanging until <code>ETIMEDOUT</code> because of node's focus in latest ones which are still being processed in time under 500ms.</p> <p>P.S.: There is no problem with remote server. I'm using <a href="https://github.com/mikeal/request" rel="noreferrer">request</a> for interactions with it.</p> <p><strong>upd</strong></p> <p>The way things works with some code:</p> <pre class="lang-js prettyprint-override"><code>function queryRemote(req, res) { var options = {}; // built based on req object (URI, body, authorization, etc.) request(options, function(err, httpResponse, body) { return err ? send500(req, res) : res.end(encrypt(body)); }); } app.use(myBodyParser); // reads hex string in payload // and calls next() on 'end' event app.post('/', [checkHeaders, // check Content-Type and Authorization headers authUser, // query DB and call next() parseRequest], // decrypt payload, parse JSON, call next() function(req, res) { req.socket.setTimeout(TIMEOUT); queryRemote(req, res); }); </code></pre> <p>My problem is following: when <code>ab</code> issuing, let's say, 20 POSTs to <code>/</code>, express route handler gets called like thousands of times. That's not always happening, sometimes 20 and only 20 requests are processed in timely fashion.</p> <p>Of course, <code>ab</code> is not a problem. I'm 100% sure that only 20 requests sent by <code>ab</code>. But route handler gets called multiple times.</p> <p>I can't find reasons for such behaviour, any advice?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload