Skip to content

Commit 767109c

Browse files
committed
chore: cleanup names
1 parent a5aeb16 commit 767109c

File tree

6 files changed

+83
-94
lines changed

6 files changed

+83
-94
lines changed

cluster.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ type ClusterClient struct {
838838
state *clusterStateHolder
839839
cmdsInfoCache *cmdsInfoCache
840840
cmdable
841-
hooks
841+
hooksMixin
842842
}
843843

844844
// NewClusterClient returns a Redis Cluster client as described in
@@ -855,7 +855,7 @@ func NewClusterClient(opt *ClusterOptions) *ClusterClient {
855855
c.cmdsInfoCache = newCmdsInfoCache(c.cmdsInfo)
856856
c.cmdable = c.Process
857857

858-
c.hooks.setDefaultHook(defaultHook{
858+
c.initHooks(hooks{
859859
dial: nil,
860860
process: c.process,
861861
pipeline: c.processPipeline,
@@ -892,7 +892,7 @@ func (c *ClusterClient) Do(ctx context.Context, args ...interface{}) *Cmd {
892892
}
893893

894894
func (c *ClusterClient) Process(ctx context.Context, cmd Cmder) error {
895-
err := c.hooks.process(ctx, cmd)
895+
err := c.processHook(ctx, cmd)
896896
cmd.SetErr(err)
897897
return err
898898
}
@@ -1190,7 +1190,7 @@ func (c *ClusterClient) loadState(ctx context.Context) (*clusterState, error) {
11901190

11911191
func (c *ClusterClient) Pipeline() Pipeliner {
11921192
pipe := Pipeline{
1193-
exec: pipelineExecer(c.hooks.processPipeline),
1193+
exec: pipelineExecer(c.processPipelineHook),
11941194
}
11951195
pipe.init()
11961196
return &pipe
@@ -1279,7 +1279,7 @@ func (c *ClusterClient) cmdsAreReadOnly(ctx context.Context, cmds []Cmder) bool
12791279
func (c *ClusterClient) processPipelineNode(
12801280
ctx context.Context, node *clusterNode, cmds []Cmder, failedCmds *cmdsMap,
12811281
) {
1282-
_ = node.Client.hooks.withProcessPipelineHook(ctx, cmds, func(ctx context.Context, cmds []Cmder) error {
1282+
_ = node.Client.withProcessPipelineHook(ctx, cmds, func(ctx context.Context, cmds []Cmder) error {
12831283
cn, err := node.Client.getConn(ctx)
12841284
if err != nil {
12851285
_ = c.mapCmdsByNode(ctx, failedCmds, cmds)
@@ -1383,7 +1383,7 @@ func (c *ClusterClient) TxPipeline() Pipeliner {
13831383
pipe := Pipeline{
13841384
exec: func(ctx context.Context, cmds []Cmder) error {
13851385
cmds = wrapMultiExec(ctx, cmds)
1386-
return c.hooks.processTxPipeline(ctx, cmds)
1386+
return c.processTxPipelineHook(ctx, cmds)
13871387
},
13881388
}
13891389
pipe.init()
@@ -1456,7 +1456,7 @@ func (c *ClusterClient) processTxPipelineNode(
14561456
ctx context.Context, node *clusterNode, cmds []Cmder, failedCmds *cmdsMap,
14571457
) {
14581458
cmds = wrapMultiExec(ctx, cmds)
1459-
_ = node.Client.hooks.withProcessPipelineHook(ctx, cmds, func(ctx context.Context, cmds []Cmder) error {
1459+
_ = node.Client.withProcessPipelineHook(ctx, cmds, func(ctx context.Context, cmds []Cmder) error {
14601460
cn, err := node.Client.getConn(ctx)
14611461
if err != nil {
14621462
_ = c.mapCmdsByNode(ctx, failedCmds, cmds)

cluster_commands.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func (c *ClusterClient) DBSize(ctx context.Context) *IntCmd {
1010
cmd := NewIntCmd(ctx, "dbsize")
11-
_ = c.hooks.withProcessHook(ctx, cmd, func(ctx context.Context, _ Cmder) error {
11+
_ = c.withProcessHook(ctx, cmd, func(ctx context.Context, _ Cmder) error {
1212
var size int64
1313
err := c.ForEachMaster(ctx, func(ctx context.Context, master *Client) error {
1414
n, err := master.DBSize(ctx).Result()
@@ -30,8 +30,8 @@ func (c *ClusterClient) DBSize(ctx context.Context) *IntCmd {
3030

3131
func (c *ClusterClient) ScriptLoad(ctx context.Context, script string) *StringCmd {
3232
cmd := NewStringCmd(ctx, "script", "load", script)
33-
_ = c.hooks.withProcessHook(ctx, cmd, func(ctx context.Context, _ Cmder) error {
34-
mu := &sync.Mutex{}
33+
_ = c.withProcessHook(ctx, cmd, func(ctx context.Context, _ Cmder) error {
34+
var mu sync.Mutex
3535
err := c.ForEachShard(ctx, func(ctx context.Context, shard *Client) error {
3636
val, err := shard.ScriptLoad(ctx, script).Result()
3737
if err != nil {
@@ -56,7 +56,7 @@ func (c *ClusterClient) ScriptLoad(ctx context.Context, script string) *StringCm
5656

5757
func (c *ClusterClient) ScriptFlush(ctx context.Context) *StatusCmd {
5858
cmd := NewStatusCmd(ctx, "script", "flush")
59-
_ = c.hooks.withProcessHook(ctx, cmd, func(ctx context.Context, _ Cmder) error {
59+
_ = c.withProcessHook(ctx, cmd, func(ctx context.Context, _ Cmder) error {
6060
err := c.ForEachShard(ctx, func(ctx context.Context, shard *Client) error {
6161
return shard.ScriptFlush(ctx).Err()
6262
})
@@ -82,7 +82,7 @@ func (c *ClusterClient) ScriptExists(ctx context.Context, hashes ...string) *Boo
8282
result[i] = true
8383
}
8484

85-
_ = c.hooks.withProcessHook(ctx, cmd, func(ctx context.Context, _ Cmder) error {
85+
_ = c.withProcessHook(ctx, cmd, func(ctx context.Context, _ Cmder) error {
8686
var mu sync.Mutex
8787
err := c.ForEachShard(ctx, func(ctx context.Context, shard *Client) error {
8888
val, err := shard.ScriptExists(ctx, hashes...).Result()

redis.go

Lines changed: 53 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -40,45 +40,39 @@ type (
4040
ProcessPipelineHook func(ctx context.Context, cmds []Cmder) error
4141
)
4242

43-
var (
44-
nonDialHook = func(ctx context.Context, network, addr string) (net.Conn, error) { return nil, nil }
45-
nonProcessHook = func(ctx context.Context, cmd Cmder) error { return nil }
46-
nonProcessPipelineHook = func(ctx context.Context, cmds []Cmder) error { return nil }
47-
nonTxProcessPipelineHook = func(ctx context.Context, cmds []Cmder) error { return nil }
48-
)
43+
type hooksMixin struct {
44+
slice []Hook
45+
initial hooks
46+
current hooks
47+
}
4948

50-
type defaultHook struct {
49+
func (hs *hooksMixin) initHooks(hooks hooks) {
50+
hs.initial = hooks
51+
hs.chain()
52+
}
53+
54+
type hooks struct {
5155
dial DialHook
5256
process ProcessHook
5357
pipeline ProcessPipelineHook
5458
txPipeline ProcessPipelineHook
5559
}
5660

57-
func (h *defaultHook) init() {
61+
func (h *hooks) setDefaults() {
5862
if h.dial == nil {
59-
h.dial = nonDialHook
63+
h.dial = func(ctx context.Context, network, addr string) (net.Conn, error) { return nil, nil }
6064
}
6165
if h.process == nil {
62-
h.process = nonProcessHook
66+
h.process = func(ctx context.Context, cmd Cmder) error { return nil }
6367
}
6468
if h.pipeline == nil {
65-
h.pipeline = nonProcessPipelineHook
69+
h.pipeline = func(ctx context.Context, cmds []Cmder) error { return nil }
6670
}
6771
if h.txPipeline == nil {
68-
h.txPipeline = nonTxProcessPipelineHook
72+
h.txPipeline = func(ctx context.Context, cmds []Cmder) error { return nil }
6973
}
7074
}
7175

72-
type hooks struct {
73-
slice []Hook
74-
defaultHook defaultHook
75-
76-
dialHook DialHook
77-
processHook ProcessHook
78-
processPipelineHook ProcessPipelineHook
79-
processTxPipelineHook ProcessPipelineHook
80-
}
81-
8276
// AddHook is to add a hook to the queue.
8377
// Hook is a function executed during network connection, command execution, and pipeline,
8478
// it is a first-in-first-out stack queue (FIFO).
@@ -115,48 +109,43 @@ type hooks struct {
115109
//
116110
// Please note: "next(ctx, cmd)" is very important, it will call the next hook,
117111
// if "next(ctx, cmd)" is not executed, the redis command will not be executed.
118-
func (hs *hooks) AddHook(hook Hook) {
112+
func (hs *hooksMixin) AddHook(hook Hook) {
119113
hs.slice = append(hs.slice, hook)
120114
hs.chain()
121115
}
122116

123-
func (hs *hooks) chain() {
124-
hs.defaultHook.init()
117+
func (hs *hooksMixin) chain() {
118+
hs.initial.setDefaults()
125119

126-
hs.dialHook = hs.defaultHook.dial
127-
hs.processHook = hs.defaultHook.process
128-
hs.processPipelineHook = hs.defaultHook.pipeline
129-
hs.processTxPipelineHook = hs.defaultHook.txPipeline
120+
hs.current.dial = hs.initial.dial
121+
hs.current.process = hs.initial.process
122+
hs.current.pipeline = hs.initial.pipeline
123+
hs.current.txPipeline = hs.initial.txPipeline
130124

131125
for i := len(hs.slice) - 1; i >= 0; i-- {
132-
if wrapped := hs.slice[i].DialHook(hs.dialHook); wrapped != nil {
133-
hs.dialHook = wrapped
126+
if wrapped := hs.slice[i].DialHook(hs.current.dial); wrapped != nil {
127+
hs.current.dial = wrapped
134128
}
135-
if wrapped := hs.slice[i].ProcessHook(hs.processHook); wrapped != nil {
136-
hs.processHook = wrapped
129+
if wrapped := hs.slice[i].ProcessHook(hs.current.process); wrapped != nil {
130+
hs.current.process = wrapped
137131
}
138-
if wrapped := hs.slice[i].ProcessPipelineHook(hs.processPipelineHook); wrapped != nil {
139-
hs.processPipelineHook = wrapped
132+
if wrapped := hs.slice[i].ProcessPipelineHook(hs.current.pipeline); wrapped != nil {
133+
hs.current.pipeline = wrapped
140134
}
141-
if wrapped := hs.slice[i].ProcessPipelineHook(hs.processTxPipelineHook); wrapped != nil {
142-
hs.processTxPipelineHook = wrapped
135+
if wrapped := hs.slice[i].ProcessPipelineHook(hs.current.txPipeline); wrapped != nil {
136+
hs.current.txPipeline = wrapped
143137
}
144138
}
145139
}
146140

147-
func (hs *hooks) clone() hooks {
141+
func (hs *hooksMixin) clone() hooksMixin {
148142
clone := *hs
149143
l := len(clone.slice)
150144
clone.slice = clone.slice[:l:l]
151145
return clone
152146
}
153147

154-
func (hs *hooks) setDefaultHook(d defaultHook) {
155-
hs.defaultHook = d
156-
hs.chain()
157-
}
158-
159-
func (hs *hooks) withProcessHook(ctx context.Context, cmd Cmder, hook ProcessHook) error {
148+
func (hs *hooksMixin) withProcessHook(ctx context.Context, cmd Cmder, hook ProcessHook) error {
160149
for i := len(hs.slice) - 1; i >= 0; i-- {
161150
if wrapped := hs.slice[i].ProcessHook(hook); wrapped != nil {
162151
hook = wrapped
@@ -165,7 +154,7 @@ func (hs *hooks) withProcessHook(ctx context.Context, cmd Cmder, hook ProcessHoo
165154
return hook(ctx, cmd)
166155
}
167156

168-
func (hs *hooks) withProcessPipelineHook(
157+
func (hs *hooksMixin) withProcessPipelineHook(
169158
ctx context.Context, cmds []Cmder, hook ProcessPipelineHook,
170159
) error {
171160
for i := len(hs.slice) - 1; i >= 0; i-- {
@@ -176,20 +165,20 @@ func (hs *hooks) withProcessPipelineHook(
176165
return hook(ctx, cmds)
177166
}
178167

179-
func (hs *hooks) dial(ctx context.Context, network, addr string) (net.Conn, error) {
180-
return hs.dialHook(ctx, network, addr)
168+
func (hs *hooksMixin) dialHook(ctx context.Context, network, addr string) (net.Conn, error) {
169+
return hs.current.dial(ctx, network, addr)
181170
}
182171

183-
func (hs *hooks) process(ctx context.Context, cmd Cmder) error {
184-
return hs.processHook(ctx, cmd)
172+
func (hs *hooksMixin) processHook(ctx context.Context, cmd Cmder) error {
173+
return hs.current.process(ctx, cmd)
185174
}
186175

187-
func (hs *hooks) processPipeline(ctx context.Context, cmds []Cmder) error {
188-
return hs.processPipelineHook(ctx, cmds)
176+
func (hs *hooksMixin) processPipelineHook(ctx context.Context, cmds []Cmder) error {
177+
return hs.current.pipeline(ctx, cmds)
189178
}
190179

191-
func (hs *hooks) processTxPipeline(ctx context.Context, cmds []Cmder) error {
192-
return hs.processTxPipelineHook(ctx, cmds)
180+
func (hs *hooksMixin) processTxPipelineHook(ctx context.Context, cmds []Cmder) error {
181+
return hs.current.txPipeline(ctx, cmds)
193182
}
194183

195184
//------------------------------------------------------------------------------
@@ -595,7 +584,7 @@ func (c *baseClient) context(ctx context.Context) context.Context {
595584
type Client struct {
596585
*baseClient
597586
cmdable
598-
hooks
587+
hooksMixin
599588
}
600589

601590
// NewClient returns a client to the Redis Server specified by Options.
@@ -608,14 +597,14 @@ func NewClient(opt *Options) *Client {
608597
},
609598
}
610599
c.init()
611-
c.connPool = newConnPool(opt, c.hooks.dial)
600+
c.connPool = newConnPool(opt, c.dialHook)
612601

613602
return &c
614603
}
615604

616605
func (c *Client) init() {
617606
c.cmdable = c.Process
618-
c.hooks.setDefaultHook(defaultHook{
607+
c.initHooks(hooks{
619608
dial: c.baseClient.dial,
620609
process: c.baseClient.process,
621610
pipeline: c.baseClient.processPipeline,
@@ -642,7 +631,7 @@ func (c *Client) Do(ctx context.Context, args ...interface{}) *Cmd {
642631
}
643632

644633
func (c *Client) Process(ctx context.Context, cmd Cmder) error {
645-
err := c.hooks.process(ctx, cmd)
634+
err := c.processHook(ctx, cmd)
646635
cmd.SetErr(err)
647636
return err
648637
}
@@ -666,7 +655,7 @@ func (c *Client) Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmd
666655

667656
func (c *Client) Pipeline() Pipeliner {
668657
pipe := Pipeline{
669-
exec: pipelineExecer(c.hooks.processPipeline),
658+
exec: pipelineExecer(c.processPipelineHook),
670659
}
671660
pipe.init()
672661
return &pipe
@@ -681,7 +670,7 @@ func (c *Client) TxPipeline() Pipeliner {
681670
pipe := Pipeline{
682671
exec: func(ctx context.Context, cmds []Cmder) error {
683672
cmds = wrapMultiExec(ctx, cmds)
684-
return c.hooks.processTxPipeline(ctx, cmds)
673+
return c.processTxPipelineHook(ctx, cmds)
685674
},
686675
}
687676
pipe.init()
@@ -764,7 +753,7 @@ type Conn struct {
764753
baseClient
765754
cmdable
766755
statefulCmdable
767-
hooks
756+
hooksMixin
768757
}
769758

770759
func newConn(opt *Options, connPool pool.Pooler) *Conn {
@@ -777,7 +766,7 @@ func newConn(opt *Options, connPool pool.Pooler) *Conn {
777766

778767
c.cmdable = c.Process
779768
c.statefulCmdable = c.Process
780-
c.hooks.setDefaultHook(defaultHook{
769+
c.initHooks(hooks{
781770
dial: c.baseClient.dial,
782771
process: c.baseClient.process,
783772
pipeline: c.baseClient.processPipeline,
@@ -788,7 +777,7 @@ func newConn(opt *Options, connPool pool.Pooler) *Conn {
788777
}
789778

790779
func (c *Conn) Process(ctx context.Context, cmd Cmder) error {
791-
err := c.hooks.process(ctx, cmd)
780+
err := c.processHook(ctx, cmd)
792781
cmd.SetErr(err)
793782
return err
794783
}
@@ -799,7 +788,7 @@ func (c *Conn) Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder
799788

800789
func (c *Conn) Pipeline() Pipeliner {
801790
pipe := Pipeline{
802-
exec: c.hooks.processPipeline,
791+
exec: c.processPipelineHook,
803792
}
804793
pipe.init()
805794
return &pipe
@@ -814,7 +803,7 @@ func (c *Conn) TxPipeline() Pipeliner {
814803
pipe := Pipeline{
815804
exec: func(ctx context.Context, cmds []Cmder) error {
816805
cmds = wrapMultiExec(ctx, cmds)
817-
return c.hooks.processTxPipeline(ctx, cmds)
806+
return c.processTxPipelineHook(ctx, cmds)
818807
},
819808
}
820809
pipe.init()

0 commit comments

Comments
 (0)