Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When should I call Close on connection? #1347

Closed
pankajsoni19 opened this issue Jul 2, 2024 · 4 comments
Closed

When should I call Close on connection? #1347

pankajsoni19 opened this issue Jul 2, 2024 · 4 comments
Labels

Comments

@pankajsoni19
Copy link

I am setting up the connection as

conn, _ := clickhouse.Open(
		&clickhouse.Options{
			Addr: []string{
				fmt.Sprintf("%s:9000", conf.Hostname),
			},
			MaxOpenConns:    5,
			MaxIdleConns:    1,
			ConnMaxLifetime: 10 * time.Minute,
			Auth: clickhouse.Auth{
				Database: conf.Database,
				Username: conf.Username,
				Password: conf.Password,
			}})

If I do not call close and keep using the conn object. I get this error after sometime

read: read tcp 172.31.36.2:38716->172.31.6.70:9000: read: connection reset by peer

Do I need to call conn.Close() after conn.Exec ?
Do I need to call conn.Close() after conn.Select ?
Do I need to call conn.Close() after conn.QueryRow().ScanStruct() ?
Do I need to call conn.Close() after batch.Send ?

Or how do I return the connection to the pool?

@pankajsoni19
Copy link
Author

Error is coming for me in conn.PrepareBatch

@jkaflik
Copy link
Contributor

jkaflik commented Jul 2, 2024

Do I need to call conn.Close() after conn.Exec ?
Do I need to call conn.Close() after conn.Select ?
Do I need to call conn.Close() after conn.QueryRow().ScanStruct() ?
Do I need to call conn.Close() after batch.Send ?

The answer is no to all of these. Until you really want to close the connection pool.

Or how do I return the connection to the pool?

Connections are automatically released to the pool after use.


connection reset by peer

Can be caused by any network glitch. Depending on your use case, your application should be able to retry.

If you observe it intensifies for long living connections, you can decrease your ConnMaxLifetime. You can also set MaxIdleConns to zero, so no idle connections will be kept in a pool.

@pankajsoni19
Copy link
Author

pankajsoni19 commented Jul 3, 2024

its running on AWS fargate, so I am network isn't an issue. After calling close after each operation it works. I also added a sleep timeout of 100 milliseconds between each clickhouse query call.
error always happens in conn.PrepareBatch if I don't call close.

There are 10k read query ops to table A. post that I filter the result and call conn.PrepareBatch to push data into table B.

any way to get more error details out of the library?

@jkaflik
Copy link
Contributor

jkaflik commented Jul 4, 2024

General rule for distributed systems is network is unreliable.

any way to get more error details out of the library?

connection reset by peer is the lowest level error and is strictly related to connection being dropped either by ClickHouse server or anything that is in front of it, like network router or load balancer.

Would be great if you share a code snippet. I am not sure about how Fargate implements network, but maybe there are some settings that might cause a socket drop. What is the time delay between connection reuse?

@jkaflik jkaflik closed this as completed Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants