fasthttp: Error: "too many open files" for
I have one very unpleasant issue with the fasthttp :
There is a “highly loaded” server that stops processing requests after 5-10 minutes (about 5000 requests per minute) with error:
Temporary error when accepting new connections: accept tcp4 IP:PORT: accept: too many open files
Tried 2 ways to start the (with standard ListenAndServe and with custom Server options (disabled Keepalive, custom timeouts and closing connection on handers) but isn’t works.
func HTTPServer() {
listenAddress := strings.Join([]string{os.Getenv("SERVER_IP"), os.Getenv("SERVER_PORT")}, ":")
log.Printf("Starting server on %q", listenAddress)
go func() {
if err := server.ListenAndServe(listenAddress, CustomModelHandler); err != nil {
log.Fatalf("error in ListenAndServe: %s", err)
}
}()
}
func CustomModelHandler(ctx *server.RequestCtx) {
var customModel models.CustomModel
err := json.Unmarshal(ctx.PostBody(), &customModel)
if err != nil {
ctx.Error(err.Error(), server.StatusBadRequest)
return
}
// ....
// some actions with customModel
// ....
if updated {
ctx.SetStatusCode(server.StatusOK)
} else {
ctx.SetStatusCode(server.StatusCreated)
}
defer ctx.Request.Reset()
defer ctx.Request.SetConnectionClose()
}
`
Any ideas how to fix it?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 15 (4 by maintainers)
@savsgio Thanks, I will use it.
@savsgio thanks for answer, but he not explained how did… sad!
@sjke to be precise, you should check that your listener is correctly closing connections (if you use custom listener), decrease TTL for your keep alive connections (or disable keep-alive) and set those limits to a max number of opened connections at once.
basically, you just found a common issue, when you got 4096 opened connections and tried to open 4097th, but it can’t work due to ulimits. each socket (including client-server connections) in linux are files, so to serve more connections, you’ll need higher limits and always correctly handle connection close.