Skip to content

Commit 026d036

Browse files
authored
Merge pull request #17 from el-yurchito/master
Optional Hooks list
2 parents 3f0ae3a + 7640b2e commit 026d036

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v3.1.0 (2023-06-13)
2+
3+
- Edit optional Hooks to HAConfig
4+
15
## v3.0.0 (2023-04-17)
26

37
- Update go-redis library to v9 (v9.0.3)
@@ -34,4 +38,4 @@
3438

3539
## v1.0.0 (2020-02-26)
3640

37-
- Initial public version
41+
- Initial public version

ha_conn_factory.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type HAConfig struct {
2626
Password string // the password of the master
2727
ReadonlyPassword string // the passsword of slaves
2828
Options *redis.Options // redis options
29+
Hooks []redis.Hook // optional list of redis hooks
2930
PollType int // the slave polling type
3031

3132
AutoEjectHost bool // eject the failure host or not
@@ -85,7 +86,7 @@ func NewHAConnFactory(cfg *HAConfig) (*HAConnFactory, error) {
8586
options.Addr = cfg.Master
8687
options.Password = cfg.Password
8788

88-
factory.master = newClient(redis.NewClient(&options), 0)
89+
factory.master = newClient(redis.NewClient(&options), 0, cfg.Hooks)
8990
factory.slaves = newClientPool(cfg)
9091
return factory, nil
9192
}
@@ -150,14 +151,17 @@ func (cfg *HAConfig) init() error {
150151
return nil
151152
}
152153

153-
func newClient(redisCli *redis.Client, weight int64) *client {
154+
func newClient(redisCli *redis.Client, weight int64, hooks []redis.Hook) *client {
154155
c := &client{
155156
redisCli: redisCli,
156157
weight: weight,
157158

158159
failureCount: 0,
159160
lastEjectTime: 0,
160161
}
162+
for _, hook := range hooks {
163+
c.redisCli.AddHook(hook)
164+
}
161165
return c
162166
}
163167

@@ -193,7 +197,7 @@ func newClientPool(cfg *HAConfig) *clientPool {
193197
slaveOptions.Addr = slave
194198
slaveOptions.Password = slavePassword
195199
redisCli := redis.NewClient(&slaveOptions)
196-
cli := newClient(redisCli, cfg.weights[i])
200+
cli := newClient(redisCli, cfg.weights[i], cfg.Hooks)
197201
if cfg.AutoEjectHost && len(cfg.Slaves) > 1 {
198202
redisCli.AddHook(newFailureHook(cli))
199203
}

0 commit comments

Comments
 (0)