Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit dfa8487

Browse files
author
Tsimafei Bredau
committed
add connOpenTimeRandom strategy for choosing host to connect to
1 parent d8e3533 commit dfa8487

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

bootstrap.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ func open(dsn string) (*clickhouse, error) {
152152
connOpenStrategy = connOpenRandom
153153
case "in_order":
154154
connOpenStrategy = connOpenInOrder
155+
case "time_random":
156+
connOpenStrategy = connOpenTimeRandom
155157
}
156158

157159
settings, err := makeQuerySettings(query)

connect.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ func (s openStrategy) String() string {
1717
switch s {
1818
case connOpenInOrder:
1919
return "in_order"
20+
case connOpenTimeRandom:
21+
return "time_random"
2022
}
2123
return "random"
2224
}
2325

2426
const (
2527
connOpenRandom openStrategy = iota + 1
2628
connOpenInOrder
29+
connOpenTimeRandom
2730
)
2831

2932
type connOptions struct {
@@ -55,13 +58,21 @@ func dial(options connOptions) (*connect, error) {
5558
}
5659
tlsConfig.InsecureSkipVerify = options.skipVerify
5760
}
61+
checkedHosts := make(map[int]struct{}, len(options.hosts))
5862
for i := range options.hosts {
5963
var num int
6064
switch options.openStrategy {
6165
case connOpenInOrder:
6266
num = i
6367
case connOpenRandom:
6468
num = (ident + i) % len(options.hosts)
69+
case connOpenTimeRandom:
70+
// select host based on milliseconds
71+
num = int((time.Now().UnixNano()/1000)%1000) % len(options.hosts)
72+
for _, ok := checkedHosts[num]; ok; _, ok = checkedHosts[num] {
73+
num = int(time.Now().UnixNano()) % len(options.hosts)
74+
}
75+
checkedHosts[num] = struct{}{}
6576
}
6677
switch {
6778
case options.secure:

0 commit comments

Comments
 (0)