javascript - xmlHttpRequest does not close connections - Stack Overflow

I make an AJAX request and depending on the answer I will refresh the current page, code is like this:v

I make an AJAX request and depending on the answer I will refresh the current page, code is like this:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
  var res = xhttp.responseText;
  if (res.length > 0) {
      window.location = res;
  }
}
}
xhttp.open("POST", "getlink.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send('id='+id);

However I noticed that connections are not closed on the server. When running this mand:

netstat -anl | grep 80 | awk '/^tcp/ {t[$NF]++}END{for(state in t){print state, t[state]} }'

I see a lot of connections in TIME_WAIT state:

LAST_ACK 4
LISTEN 2
SYN_RECV 2
ESTABLISHED 1421
FIN_WAIT1 9
FIN_WAIT2 17
TIME_WAIT 2250

It would seem that xmlHttpRequest does not close connections creating a lot of problems on the server.

I make an AJAX request and depending on the answer I will refresh the current page, code is like this:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
  var res = xhttp.responseText;
  if (res.length > 0) {
      window.location = res;
  }
}
}
xhttp.open("POST", "getlink.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send('id='+id);

However I noticed that connections are not closed on the server. When running this mand:

netstat -anl | grep 80 | awk '/^tcp/ {t[$NF]++}END{for(state in t){print state, t[state]} }'

I see a lot of connections in TIME_WAIT state:

LAST_ACK 4
LISTEN 2
SYN_RECV 2
ESTABLISHED 1421
FIN_WAIT1 9
FIN_WAIT2 17
TIME_WAIT 2250

It would seem that xmlHttpRequest does not close connections creating a lot of problems on the server.

Share Improve this question asked Nov 11, 2015 at 21:10 Aditya ZaqiAditya Zaqi 3033 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

It's not a bug, it's a feature!

It is called Connection keep-alive and is a feature of the HTTP protocol and applies to all HTTP connections, not just ones created by XMLHttpRequest.

So basically the idea is that opening a TCP connection is a plicated thing, because you need to do the costly three-way-handshake to establish a connection and allocate system resources. However most web pages load many resources at a time. It is one request for each image and one for each CSS file and so on and so on. To speed up loading and reduce server load HTTP allows that the connection to the server is kept open and is reused for subsequent requests.

The behaviour is controlled using headers from the server that specify how many connections the client should open at max and after how many seconds of idle time the connection should be closed. If you find that you have problems on the server with too many connections you should adjust these headers which can usually be configured in your web servers config files.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744192191a4562483.html

相关推荐

  • javascript - xmlHttpRequest does not close connections - Stack Overflow

    I make an AJAX request and depending on the answer I will refresh the current page, code is like this:v

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信