2

Whenever I try to to send rapid HTTP Post in succession, the server sometimes crashes (java.net.ConnectException: Connection refused: connect), sometimes freezes (no error, but cannot use it anymore), and sometimes works....

If I send HTTP Post to the server slowly, there seems to be no problems at all.

I have the simple code for an http server in node.js - My guess is that sometimes the NodeJS server will receive a request, then receive another request before it sends out the respond, thus causing all sorts of problem. How do I make my server able to accept multiple requests all at once?

var server = http.createServer(function (req, res) { 

     if (req.method != 'POST') {
          res.end();
     }
     else {
          req.on('data', function(chunk) {
               //Do some stuff here
               file1=JSON.parse(chunk.toString());
               console.log("Hello World") ;
          }

          req.on('end', function() {
               res.writeHead(200, "OK", {'Content-Type': 'text/html'});
               res.end();
          });
     }
} server.listen(9000);

EDIT Here is the java program sending the HTTP POSTS

 public static String httpUrlRequest(String requestURL, String json) {
       URL url;
       String response = "";
       HttpURLConnection connection = null;
       InputStream is = null;
       try {
           url = new URL(requestURL);

           connection = (HttpURLConnection) url.openConnection();
           connection.setDoOutput(true);
           connection.setRequestMethod("POST");
           connection.getOutputStream().write(json.getBytes());
           connection.getOutputStream().flush();
           connection.getOutputStream().close();
           int code = connection.getResponseCode();
           System.out.println("code" + code);

       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           connection.disconnect();
       }
       return response;
    }

    public static void main(String[] args) {
        Date date = new Date();
       Gson gson = new Gson();
       Map<String, Object> tempMap = gson.fromJson(json, new TypeToken<Map<String, Object>>(){}.getType());

       for(int i = 0; i < 10; i++){
           date = new Date();
           tempMap.put("GetOn", getDateString(date));
           httpUrlRequest("http://111.111.11.111:9000" ,gson.toJson(tempMap));
       }

    }

UPDATE : If I parse the JSON in the nodejs server, then sometimes I get the error where the connection is refused.

So when I parse the request data, for some reason, nodejs fails to receive the entire json file that was sent (5KB). Instead, it only receives half of it, and my Java console says connect error. And this problem occurs AFTER nodejs correctly parses about 3 to 5 requests. Then on the next request, everything goes wrong. How can I tell if the java breaks connection causing only half of the JSON to be sent, or if only half of the JSON is sent causing nodejs to crash, ultimately resulting in connect error.

If I comment out all the parsing, then I never get the errors anymore. I don't even understand why JSON.Parse in nodejs would induce a java connect error....

9
  • Only this code you are using or any other? blocks of code in between? Commented Dec 5, 2013 at 6:58
  • @AmGates I do some json data parsing where my comment is, but nothing else. Commented Dec 5, 2013 at 6:59
  • Are you sure your server isn't running out of resources? I easily get 7000 reqs/s for the code you post. Also, 'some json data parsing'? How big is the data you're uploading? Commented Dec 5, 2013 at 7:07
  • Might be some problem with the JSON data parsing Commented Dec 5, 2013 at 7:09
  • The error I get is java.net.ConnectException: Connection refused: connect . The file being parsed is about 5 KB Commented Dec 5, 2013 at 7:10

2 Answers 2

3

Your problem lies here:

 req.on('data', function(chunk) {
   //Do some stuff here
   file1=JSON.parse(chunk.toString());
   console.log("Hello World") ;
 }

Since the body can be sent in multiple packets, you might receive more than 1 'data' event, thus attempting to parse an incomplete JSON. Try this instead:

var chunks = [];

req.on('data', function(chunk) {
  chunks.push(chunk);
}

req.on('end', function () {
  // assemble all chunks
  var body = Buffer.concat(chunks).toString(),
      file1 = JSON.parse(body);

  res.writeHead(200, "OK", {'Content-Type': 'text/html'});
  res.end();
});
Sign up to request clarification or add additional context in comments.

2 Comments

I am facepalming myself now for not coming to the realization that the request data is sent in chunks. The thing that threw me off the most is that sometimes one chunk contained the entire 5KB file and other times it contained only half of it O_O
Now that I've been thinking about it more. 5KB travels in one chunk if I send 1 request per second. But if I send 10 requests in one second, it decides to split up the chunk. Why does this happen -_-
0

during this kind of tests, it would be easy to hit max throughput via the connection port.

  1. http://www.slideshare.net/sh1mmer/a-million-connections-and-beyond-nodejs-at-scale
  2. https://groups.google.com/forum/#!topic/nodejs/cRRS7ZJkyzc

The simply solution is ulimit -n 100000 before your run your node.js. But never do this in the production, better consider cluster for really large connection if you need to handle it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.