Skip to content

Instantly share code, notes, and snippets.

@kavirajk
Created January 7, 2026 17:26
Show Gist options
  • Select an option

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

Select an option

Save kavirajk/e22a159686449a14c4f1b24124fb8974 to your computer and use it in GitHub Desktop.
ClickHouse go client side load balancing
package code
import (
"testing"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
)
func TestClientLB(t *testing.T) {
conn, err := clickhouse.Open(&clickhouse.Options{
Addr: []string{
"127.0.0.1:9001",
"127.0.0.2:9002",
},
ConnOpenStrategy: clickhouse.ConnOpenRoundRobin,
Debug: true,
Auth: clickhouse.Auth{
Database: "default",
},
MaxOpenConns: 10,
MaxIdleConns: 10,
})
if err != nil {
t.Log(err)
}
t.Log("=====================")
for range 3 {
v, err := conn.ServerVersion()
if err != nil {
t.Log(err)
}
t.Log(v.String())
}
t.Log("=====================")
for range 3 {
v, err := conn.ServerVersion()
if err != nil {
t.Log(err)
}
t.Log(v.String())
t.Log("sleep 5 seconds")
time.Sleep(5 * time.Second)
}
}
@kavirajk
Copy link
Author

kavirajk commented Jan 7, 2026

$ go test . -run TestClientLB -v
=== RUN TestClientLB
client_lb_test.go:27: =====================
[clickhouse][127.0.0.2:9002][id=1][handshake] -> 0.0.0
[clickhouse][127.0.0.2:9002][id=1][handshake] <- ClickHouse (clickhouse1) server version 25.11.5 revision 54482 (timezone UTC)
[clickhouse][127.0.0.2:9002][id=1][acquired new]
[clickhouse][127.0.0.2:9002][id=1][released]
client_lb_test.go:33: ClickHouse (clickhouse1) server version 25.11.5 revision 54482 (timezone UTC)
[clickhouse][127.0.0.1:9001][id=2][handshake] -> 0.0.0
[clickhouse][127.0.0.1:9001][id=2][handshake] <- ClickHouse (clickhouse2) server version 25.11.5 revision 54482 (timezone UTC)
[clickhouse][127.0.0.1:9001][id=2][acquired new]
[clickhouse][127.0.0.1:9001][id=2][released]
client_lb_test.go:33: ClickHouse (clickhouse2) server version 25.11.5 revision 54482 (timezone UTC)
[clickhouse][127.0.0.2:9002][id=3][handshake] -> 0.0.0
[clickhouse][127.0.0.2:9002][id=3][handshake] <- ClickHouse (clickhouse1) server version 25.11.5 revision 54482 (timezone UTC)
[clickhouse][127.0.0.2:9002][id=3][acquired new]
[clickhouse][127.0.0.2:9002][id=3][released]
client_lb_test.go:33: ClickHouse (clickhouse1) server version 25.11.5 revision 54482 (timezone UTC)
client_lb_test.go:35: =====================
[clickhouse][127.0.0.1:9001][id=4][handshake] -> 0.0.0
[clickhouse][127.0.0.1:9001][id=4][handshake] <- ClickHouse (clickhouse2) server version 25.11.5 revision 54482 (timezone UTC)
[clickhouse][127.0.0.1:9001][id=4][acquired new]
[clickhouse][127.0.0.1:9001][id=4][released]
client_lb_test.go:41: ClickHouse (clickhouse2) server version 25.11.5 revision 54482 (timezone UTC)
client_lb_test.go:42: sleep 5 seconds
[clickhouse][127.0.0.2:9002][id=5][handshake] -> 0.0.0
[clickhouse][127.0.0.2:9002][id=5][handshake] <- ClickHouse (clickhouse1) server version 25.11.5 revision 54482 (timezone UTC)
[clickhouse][127.0.0.2:9002][id=5][acquired new]
[clickhouse][127.0.0.2:9002][id=5][released]
client_lb_test.go:41: ClickHouse (clickhouse1) server version 25.11.5 revision 54482 (timezone UTC)
client_lb_test.go:42: sleep 5 seconds
[clickhouse][127.0.0.1:9001][id=6][handshake] -> 0.0.0
[clickhouse][127.0.0.1:9001][id=6][handshake] <- ClickHouse (clickhouse2) server version 25.11.5 revision 54482 (timezone UTC)
[clickhouse][127.0.0.1:9001][id=6][acquired new]
[clickhouse][127.0.0.1:9001][id=6][released]
client_lb_test.go:41: ClickHouse (clickhouse2) server version 25.11.5 revision 54482 (timezone UTC)
client_lb_test.go:42: sleep 5 seconds
--- PASS: TestClientLB (15.01s)

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