Created
January 7, 2026 17:26
-
-
Save kavirajk/e22a159686449a14c4f1b24124fb8974 to your computer and use it in GitHub Desktop.
ClickHouse go client side load balancing
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 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) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ 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)