Skip to content

Instantly share code, notes, and snippets.

@kavirajk
Last active February 16, 2026 10:56
Show Gist options
  • Select an option

  • Save kavirajk/7ce39ac07acec7364a3137a622c6c4d4 to your computer and use it in GitHub Desktop.

Select an option

Save kavirajk/7ce39ac07acec7364a3137a622c6c4d4 to your computer and use it in GitHub Desktop.
custom_dialer.go
package main
import (
"context"
"crypto/tls"
"fmt"
"os"
"sync"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
)
func main() {
ctx := context.Background()
options := &clickhouse.Options{
Addr: []string{"hada5zrfx4.us-east1.gcp.clickhouse.cloud:9440"},
Protocol: clickhouse.Native, // just to be explicit. default is Native
Auth: clickhouse.Auth{
Username: "default",
Password: "xxxxx",
},
TLS: &tls.Config{},
// Disables TCP keep alive
DialContext: func(ctx context.Context, addr string) (net.Conn, error) {
dialer := &net.Dialer{
KeepAliveConfig: net.KeepAliveConfig{Enable: false},
}
return tls.DialWithDialer(dialer, "tcp", addr, &tls.Config{})
},
// ConnMaxLifeTime is less than idle-threshold (5 mins) on my cloud instance setup
ConnMaxLifetime: 4 * time.Minute,
MaxIdleConns: 5,
MaxOpenConns: 10,
}
conn, err := clickhouse.Open(options)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to connect: %v\n", err)
os.Exit(1)
}
defer conn.Close()
doPing(ctx, conn)
fmt.Println("ping success: ", time.Now().UTC())
stats := conn.Stats()
fmt.Println("connection stats: ", "maxOpen", stats.MaxOpenConns, "open", stats.Open, "maxIdle", stats.MaxIdleConns, "idle", stats.Idle)
fmt.Println("starting the ticket. Next ping in 7 mins")
tk := time.NewTicker(7 * time.Minute)
// tk := time.NewTicker(5 * time.Second)
for _ = range tk.C {
doPing(ctx, conn)
fmt.Println("ping success: ", time.Now().UTC())
stats := conn.Stats()
fmt.Println("connection stats: ", "maxOpen", stats.MaxOpenConns, "open", stats.Open, "maxIdle", stats.MaxIdleConns, "idle", stats.Idle)
}
}
func doPing(ctx context.Context, conn driver.Conn) error {
// do 10 pings, to make sure we have 10 open conns and 5 always in idle
var wg sync.WaitGroup
for _ = range 10 {
wg.Add(1)
go func() {
defer wg.Done()
if err := conn.Ping(ctx); err != nil {
fmt.Println("ping failed", err)
}
if _, err := conn.Query(ctx, "SELECT 1"); err != nil {
fmt.Println("query select failed", err)
}
}()
}
wg.Wait()
return nil
}
@kavirajk
Copy link
Author

Observation during time (UTC) 13th Feb 15:46 - 16:36

$ go run custom_dialer.go
ping success:  2026-02-13 15:46:34.514408754 +0000 UTC
connection stats:  maxOpen 10 open 1 maxIdle 5 idle 5
starting the ticket. Next ping in 7 mins
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=24, auth_db=): read: read tcp 192.168.2.2:36086->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=30, auth_db=): read: read tcp 192.168.2.2:36074->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=21, auth_db=): read: read tcp 192.168.2.2:36068->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=22, auth_db=): read: read tcp 192.168.2.2:36098->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=28, auth_db=): read: read tcp 192.168.2.2:36034->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=26, auth_db=): read: read tcp 192.168.2.2:36022->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=23, auth_db=): read: read tcp 192.168.2.2:36072->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=25, auth_db=): read: read tcp 192.168.2.2:36044->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=29, auth_db=): read: read tcp 192.168.2.2:36060->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=27, auth_db=): read: read tcp 192.168.2.2:36016->34.36.105.62:9440: i/o timeout
query select failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=38, auth_db=): read: read tcp 192.168.2.2:37174->34.36.105.62:9440: i/o timeout
query select failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=39, auth_db=): read: read tcp 192.168.2.2:37224->34.36.105.62:9440: i/o timeout
query select failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=31, auth_db=): read: read tcp 192.168.2.2:37180->34.36.105.62:9440: i/o timeout
query select failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=40, auth_db=): read: read tcp 192.168.2.2:37196->34.36.105.62:9440: i/o timeout
query select failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=34, auth_db=): read: read tcp 192.168.2.2:37232->34.36.105.62:9440: i/o timeout
query select failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=33, auth_db=): read: read tcp 192.168.2.2:37230->34.36.105.62:9440: i/o timeout
query select failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=35, auth_db=): read: read tcp 192.168.2.2:37178->34.36.105.62:9440: i/o timeout
query select failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=32, auth_db=): read: read tcp 192.168.2.2:37244->34.36.105.62:9440: i/o timeout
query select failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=37, auth_db=): read: read tcp 192.168.2.2:37182->34.36.105.62:9440: i/o timeout
query select failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=36, auth_db=): read: read tcp 192.168.2.2:37184->34.36.105.62:9440: i/o timeout
ping success:  2026-02-13 15:54:34.867453702 +0000 UTC
connection stats:  maxOpen 10 open 0 maxIdle 5 idle 0
ping success:  2026-02-13 16:00:34.991200954 +0000 UTC
connection stats:  maxOpen 10 open 1 maxIdle 5 idle 5
ping success:  2026-02-13 16:07:35.010346784 +0000 UTC
connection stats:  maxOpen 10 open 1 maxIdle 5 idle 5
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=66, auth_db=): read: read tcp 192.168.2.2:32804->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=63, auth_db=): read: read tcp 192.168.2.2:32808->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=68, auth_db=): read: read tcp 192.168.2.2:32846->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=61, auth_db=): read: read tcp 192.168.2.2:32784->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=64, auth_db=): read: read tcp 192.168.2.2:32806->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=70, auth_db=): read: read tcp 192.168.2.2:32782->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=67, auth_db=): read: read tcp 192.168.2.2:32834->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=69, auth_db=): read: read tcp 192.168.2.2:32848->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=62, auth_db=): read: read tcp 192.168.2.2:32800->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=65, auth_db=): read: read tcp 192.168.2.2:32860->34.36.105.62:9440: i/o timeout
ping success:  2026-02-13 16:15:06.898405928 +0000 UTC
connection stats:  maxOpen 10 open 1 maxIdle 5 idle 5
ping success:  2026-02-13 16:21:34.998524854 +0000 UTC
connection stats:  maxOpen 10 open 1 maxIdle 5 idle 5
ping success:  2026-02-13 16:28:35.003697281 +0000 UTC
connection stats:  maxOpen 10 open 1 maxIdle 5 idle 5
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=109, auth_db=): read: read tcp 192.168.2.2:51226->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=101, auth_db=): read: read tcp 192.168.2.2:51186->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=106, auth_db=): read: read tcp 192.168.2.2:51210->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=103, auth_db=): read: read tcp 192.168.2.2:51172->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=104, auth_db=): read: read tcp 192.168.2.2:51160->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=107, auth_db=): read: read tcp 192.168.2.2:51234->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=110, auth_db=): read: read tcp 192.168.2.2:51170->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=108, auth_db=): read: read tcp 192.168.2.2:51216->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=102, auth_db=): read: read tcp 192.168.2.2:51176->34.36.105.62:9440: i/o timeout
ping failed handshake: failed to read packet from 34.36.105.62:9440 (conn_id=105, auth_db=): read: read tcp 192.168.2.2:51202->34.36.105.62:9440: i/o timeout
ping success:  2026-02-13 16:36:05.904279717 +0000 UTC
connection stats:  maxOpen 10 open 1 maxIdle 5 idle 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment