The Web server has a large number of close_wait connections

Source: Internet
Author: User

The company's website has been very stable, some time before the occasional web site can not open, prompted 504 error, operation is suspected to be a program update caused, but carefully read the code and did not get too much data volume, and the database performance has been very smooth. So has not been able to start, do not know where the problem, every time this problem restarts IIS can be resolved, or sometimes it is time to solve their own, so this problem has been unresolved.

It was not until last night that the site was updated again, and this issue frequently occurred during functional verification. Later testing is an administrative identity of the user login will cause this error, and operations in the view of the server situation found a large number of HTTP requests, and the status of Close_wait. Looked under the IP address of the request, exactly the IP of our image server. On the Internet to check this problem, found that most of the problem is the socket program appears, how our web program also appeared such a problem?

Do not struggle with this, or from the code to start it. So looked at the management of the user login code, important to focus on the image of the server-related places. Finally found that this account landing will be a lot of images used in the home page display, the key problem is to display these images will be executed first to determine whether the image path exists, if not present, return a default local image address.

  

  Public Static stringGettrueimgpath (stringURL) {HttpWebRequest Myreq=(HttpWebRequest) webrequest.create (URL); Try{HttpWebResponse Myres=(HttpWebResponse) myreq.getresponse (); if(Myres.contentlength >0)                {                    returnUrl//exist                }                Else                {                    return "/contents/images/img_fload.gif"; }            }            Catch            {                return "/contents/images/img_fload.gif"; }                   }

See this code estimate everyone immediately know where the problem is, HttpWebResponse's request was not closed! That's why there are so many close_wait HTTP connections, and it is these HTTP connections that do not shut down in a timely manner that consume normal HTTP requests from the server, resulting in non-normal access. According to the information I found, the connection of Close_wait state will be closed after 2 hours, so the website will be back to normal after a period of time. At this point, the source of the problem has been found, the code is modified and then validated, the code is modified as follows:

  

  Public Static stringGettrueimgpath (stringURL) {            varMyreq =(HttpWebRequest) webrequest.create (URL); Myreq.keepalive=false;            HttpWebResponse Myres; Try{myres=(HttpWebResponse) myreq.getresponse (); if(Myres.contentlength <=0) {URL="/contents/images/img_fload.gif";            } myres.close (); }            Catch{URL="/contents/images/img_fload.gif"; }            returnURL; }

After the update to the server to verify also passed, no more close_wait connection, this problem resolved.

The Web server has a large number of close_wait connections

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.