I have got a resources release problem.
- PostgreSQL runs on a server 1.
- Go lang service runs on a server 2 in a Docker container.
- There is an ssh tunnel Docker container for a connection between the database and the service. It does not know anything except ssh.
Docker is in swarm mode.
The service (2) connects to the database via golang database/sql library. I call sql.Open()
, driverName = "postgres"
. Then everything is ok. In some time, may be in 30 minutes, (*DB) Query()
returns an error read: connection reset by peer
. If I call (*DB) Ping()
previously, Ping()
does not return an error, but the next call Query()
does.
If I call Query()
again in some time, a new connection is created. I can see it in a select * from pg_stat_activity;
query in the database (state = idle
). But the previous connection has not been removed.
So I call (*DB) Close()
, create a new DB object and call sql.Open()
.
Close closes the database, releasing any open resources.
(https://golang.org/pkg/database/sql/#Conn.Close)
But after the Close
call I can still see the connection in a select * from pg_stat_activity;
query in the database (state = idle
).
I see the bad connection and the new one.
As the result there is a resources leak.
What is the correct way to handle a read: connection reset by peer
error? Why have I got this error?