Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intermittent "connection reset by peer" sql postgres

After a period of inactivity, my go web service is getting a net.OpError with message read tcp x.x.x.x:52086->x.x.x.x:24414: read: connection reset by peer when executing the first postgres sql query. After the error, the subsequent requests will work fine.

The postgres database is hosted with compose.com which has haproxy in front of the postgres db. My go web app is using standard sql and sqlx.

I've tried running a ticker invoking db.Ping() every 15 minutes, but this hasn't fixed the issue.

Why is the go standard sql lib not handling these connection drops?

like image 635
Nick Avatar asked Nov 08 '22 07:11

Nick


1 Answers

Since no one wrote that explicity. The solution to this problem is setting db.SetConnMaxLifetime(time.Minute). I tried it and it works. Connection reset occurs often on AWS where there is inactivity limit set to 350 seconds, after that TCP RST is returned.

like image 102
pixel Avatar answered Nov 14 '22 21:11

pixel