Skip to content

Commit 63873d9

Browse files
committed
Fixing bugs for HAConnFactory
1 parent 1f5fa14 commit 63873d9

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## v3.1.1 (2023-06-24)
2+
3+
- Fixed connection selection error for HAConnFactory in MGet/MGetWithGD
4+
- Fixed a bug with the handling of the weight value for the slave
5+
16
## v3.1.0 (2023-06-13)
27

38
- Edit optional Hooks to HAConfig

ha_conn_factory.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pool
22

33
import (
44
"errors"
5+
"fmt"
56
"math/rand"
67
"strconv"
78
"strings"
@@ -126,13 +127,14 @@ func (cfg *HAConfig) init() error {
126127
}
127128
cfg.weights = make([]int64, len(cfg.Slaves))
128129
for i, slave := range cfg.Slaves {
129-
elems := strings.Split(slave, ":")
130130
cfg.weights[i] = 100
131+
elems := strings.Split(slave, ":")
131132
if len(elems) == 3 {
132133
cfg.weights[i], err = strconv.ParseInt(elems[2], 10, 64)
133134
if err != nil {
134135
return errors.New("the weight should be integer")
135136
}
137+
cfg.Slaves[i] = fmt.Sprintf("%s:%s", elems[0], elems[1])
136138
}
137139
}
138140
if cfg.ServerRetryTimeout <= 0 {

pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func (p *Pool) MGetWithGD(ctx context.Context, keys ...string) ([]interface{}, m
353353
keyErrors := make(map[string]error, 0)
354354

355355
if _, ok := p.connFactory.(*HAConnFactory); ok {
356-
conn, _ := p.connFactory.getMasterConn()
356+
conn, _ := p.connFactory.getSlaveConn()
357357
vals, err := conn.MGet(ctx, keys...).Result()
358358
if err != nil {
359359
for _, key := range keys {

pool_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@ var _ = Describe("Pool", func() {
2323
haConfig := &HAConfig{
2424
Master: "127.0.0.1:8379",
2525
Slaves: []string{
26-
"127.0.0.1:8380",
27-
"127.0.0.1:8381",
26+
"127.0.0.1:8380:100",
27+
"127.0.0.1:8381:200",
2828
},
29+
PollType: PollByWeight,
2930
}
3031
haConfig1 := &HAConfig{
3132
Master: "127.0.0.1:8382",
3233
Slaves: []string{
3334
"127.0.0.1:8383",
3435
},
36+
PollType: PollByWeight,
3537
}
3638

3739
haPool, err = NewHA(haConfig)
@@ -1824,7 +1826,6 @@ var _ = Describe("Pool_GD", func() {
18241826
for _, pool := range pools {
18251827
_, err := pool.Ping(ctx).Result()
18261828
Expect(err).To(HaveOccurred())
1827-
Expect(err.Error()).To(Equal("dial tcp 127.0.0.1:8384: connect: connection refused"))
18281829
}
18291830
})
18301831

0 commit comments

Comments
 (0)