Last active
February 16, 2026 10:56
-
-
Save kavirajk/7ce39ac07acec7364a3137a622c6c4d4 to your computer and use it in GitHub Desktop.
custom_dialer.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Observation during time (UTC) 13th Feb 15:46 - 16:36