http:接受错误:接受tcp [::]:8080:接受4:打开的文件过多;

I have written REST API in Golang and I am doing performance test of my API using Jmeter. When I run the test with 300 or more users, each user sending 20 requests with a gap of 500ms between each request I get the below error:

http: Accept error: accept tcp [::]:8080: accept4: too many open files;

I am running this Go application in AWS EC2 server. I am running this app on a 8GB RAM machine. Below is what I have tried already:

  1. I have increased the ulimit to a sufficiently good number. When I run ulimit -n command the output is: 1048576
  2. In my code I made sure that the response body is closed.

But, none of these solved the issue. Any help is appreciated. Thanks in advance.

One problem could be not closing the opened files or releasing the resources.
For example: the body object in http request is of type io.ReadCloser This read closer has a close method which you must call after your process has been finished to release the resources.

func UserHandler(w http.ResponseWriter, r *http.request) {
    var user User

    if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
      //handle error
    }
    defer r.Body.Close()

  // More Code
}

Here calling a defer on the r.Body.Close() will lead to releasing the associated resources after the method has returned its value.

Similar To this, there are alot of methods which implement this interface, like: * os.File, sql.DB, mgo.Session* etc. So you can just check if you're properly closing the resources.