title: connect EADDRNOTAVAIL in nodejs under high load - how to faster free or reuse TCP ports?
body: <p>I have a small wiki-like web application based on the express-framework which uses elastic search as it's back-end. For each request it basically only goes to the elastic search DB, retrieves the object and returns it rendered with by the handlebars template engine. The communication with elastic search is over HTTP</p>
  
  <p>This works great as long as I have only one node-js instance running. After I updated my code to use the cluster (as described in the <a href="http://nodejs.org/docs/latest/api/cluster.html#cluster_cluster" rel="nofollow noreferrer">nodejs-documentation</a> I started to encounter the following error: <strong>connect EADDRNOTAVAIL</strong></p>
  
  <p>This error shows up when I have 3 and more python scripts running which constantly retrieve some URL from my server. With 3 scripts I can retrieve ~45,000 pages with 4 and more scripts running it is between 30,000 and 37,000 pages Running only 2 or 1 scripts, I stopped them after half an hour when they retrieved 310,000 pages and 160,000 pages respectively. </p>
  
  <p>I've found this <a href="https://stackoverflow.com/questions/20991551/eaddrnotavail-after-many-http-get-requests-to-localhost/20993398#20993398">similar question</a> and tried changing <code>http.globalAgent.maxSockets</code> but that didn't have any effect.</p>
  
  <p>This is the part of the code which listens for the URLs and retrieves the data from elastic search.</p>
  
  <pre><code>app.get('/wiki/:contentId', (req, res) -&gt;
      http.get(elasticSearchUrl(req.params.contentId), (innerRes) -&gt;
          if (innerRes.statusCode != 200)
              res.send(innerRes.statusCode)
              innerRes.resume()
          else
              body = ''
              innerRes.on('data', (bodyChunk) -&gt;
                  body += bodyChunk
              )
              innerRes.on('end', () -&gt;
                  res.render('page', {'title': req.params.contentId, 'content': JSON.parse(body)._source.html})
              )
      ).on('error', (e) -&gt;
          console.log('Got error: ' + e.message)  # the error is reported here
      )
  )
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>After looking more into it, I understand now the root of the problem. I ran the command <code>netstat -an | grep -e tcp -e udp | wc -l</code> several times during my test runs, to see how many ports are used, as described in the post <a href="http://www.toptip.ca/2010/02/linux-eaddrnotavail-address-not.html" rel="nofollow noreferrer">Linux: EADDRNOTAVAIL (Address not available) error</a>. I could observe that at the time I received the EADDRNOTAVAIL-error, 56677 ports were used (instead of ~180 normally)</p>
  
  <p>Also when using only 2 simultaneous scripts, the number of used ports is saturated at around 40,000 (+/- 2,000), that means ~20,000 ports are used per script (that is the time when node-js cleans up old ports before new ones are created) and for 3 scripts running it breaches over the 56677 ports (~60,000). This explains why it fails with 3 scripts requesting data, but not with 2.</p>
  
  <p>So now my question changes to - how can I force node-js to free up the ports quicker or to reuse the same port all the time (would be the preferable solution)</p>
  
  <p>Thanks</p>