The go server appears too many open files error

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed. The last two days the server will always appear too many open files error. It's really a headache. In the local development environment, stress tests were also used to find that the problem did not exist. But why the server will have this error, some do not understand. I wanted to change the descriptor of the open file with Ulimit. But this does not seem to solve the problem. Well, let's find out more about the reason.


The server is Amazon, development language: Golang, Database: Mongo,mysql. Cache: Redis.


Test script: wrote a golang concurrent request script. Probably at the same time request the 100.ulimit limit to 50. Find errors immediately present. found that a large number of socket connections were opened under the program's process. So it was considered a MONGO connection error. Determines the MONGO connection using singleton mode. But later found that these sockets will fall down. In other words, when the connection is exhausted, it is closed. However, the server's socket connection has significantly exceeded the ulimit default limit. The project has just come online, and there is no such a large user base. So the problem is certainly not in the MONGO connection error. That's redis,mysql. After testing, it is not the error of these connections.


The whole thing is really dizzy. Finally remember, you can look at the server's socket connection is what the request. After viewing, it was found to be port 80. A website of the. Have a day, the direction is wrong ... Suddenly remembered, recently added a small feature that requires the use of an extranet API. After go opens the connection, it does not seem to close the request. The problem is resolved after the request is closed. Socket up, and then it will come down automatically.


Summary: You should close these request connections in a timely manner. Go is not the same as PHP. After the execution of PHP, even if you do not close the request, after the completion of the resolution, so the variable will be automatically released. Go is a compiled line language, so after the connection is opened, it will always be occupied if it is not manually closed.
Related Article

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.