@@ -40,45 +40,39 @@ type (
40
40
ProcessPipelineHook func (ctx context.Context , cmds []Cmder ) error
41
41
)
42
42
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
+ }
49
48
50
- type defaultHook struct {
49
+ func (hs * hooksMixin ) initHooks (hooks hooks ) {
50
+ hs .initial = hooks
51
+ hs .chain ()
52
+ }
53
+
54
+ type hooks struct {
51
55
dial DialHook
52
56
process ProcessHook
53
57
pipeline ProcessPipelineHook
54
58
txPipeline ProcessPipelineHook
55
59
}
56
60
57
- func (h * defaultHook ) init () {
61
+ func (h * hooks ) setDefaults () {
58
62
if h .dial == nil {
59
- h .dial = nonDialHook
63
+ h .dial = func ( ctx context. Context , network , addr string ) (net. Conn , error ) { return nil , nil }
60
64
}
61
65
if h .process == nil {
62
- h .process = nonProcessHook
66
+ h .process = func ( ctx context. Context , cmd Cmder ) error { return nil }
63
67
}
64
68
if h .pipeline == nil {
65
- h .pipeline = nonProcessPipelineHook
69
+ h .pipeline = func ( ctx context. Context , cmds [] Cmder ) error { return nil }
66
70
}
67
71
if h .txPipeline == nil {
68
- h .txPipeline = nonTxProcessPipelineHook
72
+ h .txPipeline = func ( ctx context. Context , cmds [] Cmder ) error { return nil }
69
73
}
70
74
}
71
75
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
-
82
76
// AddHook is to add a hook to the queue.
83
77
// Hook is a function executed during network connection, command execution, and pipeline,
84
78
// it is a first-in-first-out stack queue (FIFO).
@@ -115,48 +109,43 @@ type hooks struct {
115
109
//
116
110
// Please note: "next(ctx, cmd)" is very important, it will call the next hook,
117
111
// 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 ) {
119
113
hs .slice = append (hs .slice , hook )
120
114
hs .chain ()
121
115
}
122
116
123
- func (hs * hooks ) chain () {
124
- hs .defaultHook . init ()
117
+ func (hs * hooksMixin ) chain () {
118
+ hs .initial . setDefaults ()
125
119
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
130
124
131
125
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
134
128
}
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
137
131
}
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
140
134
}
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
143
137
}
144
138
}
145
139
}
146
140
147
- func (hs * hooks ) clone () hooks {
141
+ func (hs * hooksMixin ) clone () hooksMixin {
148
142
clone := * hs
149
143
l := len (clone .slice )
150
144
clone .slice = clone .slice [:l :l ]
151
145
return clone
152
146
}
153
147
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 {
160
149
for i := len (hs .slice ) - 1 ; i >= 0 ; i -- {
161
150
if wrapped := hs .slice [i ].ProcessHook (hook ); wrapped != nil {
162
151
hook = wrapped
@@ -165,7 +154,7 @@ func (hs *hooks) withProcessHook(ctx context.Context, cmd Cmder, hook ProcessHoo
165
154
return hook (ctx , cmd )
166
155
}
167
156
168
- func (hs * hooks ) withProcessPipelineHook (
157
+ func (hs * hooksMixin ) withProcessPipelineHook (
169
158
ctx context.Context , cmds []Cmder , hook ProcessPipelineHook ,
170
159
) error {
171
160
for i := len (hs .slice ) - 1 ; i >= 0 ; i -- {
@@ -176,20 +165,20 @@ func (hs *hooks) withProcessPipelineHook(
176
165
return hook (ctx , cmds )
177
166
}
178
167
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 )
181
170
}
182
171
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 )
185
174
}
186
175
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 )
189
178
}
190
179
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 )
193
182
}
194
183
195
184
//------------------------------------------------------------------------------
@@ -595,7 +584,7 @@ func (c *baseClient) context(ctx context.Context) context.Context {
595
584
type Client struct {
596
585
* baseClient
597
586
cmdable
598
- hooks
587
+ hooksMixin
599
588
}
600
589
601
590
// NewClient returns a client to the Redis Server specified by Options.
@@ -608,14 +597,14 @@ func NewClient(opt *Options) *Client {
608
597
},
609
598
}
610
599
c .init ()
611
- c .connPool = newConnPool (opt , c .hooks . dial )
600
+ c .connPool = newConnPool (opt , c .dialHook )
612
601
613
602
return & c
614
603
}
615
604
616
605
func (c * Client ) init () {
617
606
c .cmdable = c .Process
618
- c .hooks . setDefaultHook ( defaultHook {
607
+ c .initHooks ( hooks {
619
608
dial : c .baseClient .dial ,
620
609
process : c .baseClient .process ,
621
610
pipeline : c .baseClient .processPipeline ,
@@ -642,7 +631,7 @@ func (c *Client) Do(ctx context.Context, args ...interface{}) *Cmd {
642
631
}
643
632
644
633
func (c * Client ) Process (ctx context.Context , cmd Cmder ) error {
645
- err := c .hooks . process (ctx , cmd )
634
+ err := c .processHook (ctx , cmd )
646
635
cmd .SetErr (err )
647
636
return err
648
637
}
@@ -666,7 +655,7 @@ func (c *Client) Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmd
666
655
667
656
func (c * Client ) Pipeline () Pipeliner {
668
657
pipe := Pipeline {
669
- exec : pipelineExecer (c .hooks . processPipeline ),
658
+ exec : pipelineExecer (c .processPipelineHook ),
670
659
}
671
660
pipe .init ()
672
661
return & pipe
@@ -681,7 +670,7 @@ func (c *Client) TxPipeline() Pipeliner {
681
670
pipe := Pipeline {
682
671
exec : func (ctx context.Context , cmds []Cmder ) error {
683
672
cmds = wrapMultiExec (ctx , cmds )
684
- return c .hooks . processTxPipeline (ctx , cmds )
673
+ return c .processTxPipelineHook (ctx , cmds )
685
674
},
686
675
}
687
676
pipe .init ()
@@ -764,7 +753,7 @@ type Conn struct {
764
753
baseClient
765
754
cmdable
766
755
statefulCmdable
767
- hooks
756
+ hooksMixin
768
757
}
769
758
770
759
func newConn (opt * Options , connPool pool.Pooler ) * Conn {
@@ -777,7 +766,7 @@ func newConn(opt *Options, connPool pool.Pooler) *Conn {
777
766
778
767
c .cmdable = c .Process
779
768
c .statefulCmdable = c .Process
780
- c .hooks . setDefaultHook ( defaultHook {
769
+ c .initHooks ( hooks {
781
770
dial : c .baseClient .dial ,
782
771
process : c .baseClient .process ,
783
772
pipeline : c .baseClient .processPipeline ,
@@ -788,7 +777,7 @@ func newConn(opt *Options, connPool pool.Pooler) *Conn {
788
777
}
789
778
790
779
func (c * Conn ) Process (ctx context.Context , cmd Cmder ) error {
791
- err := c .hooks . process (ctx , cmd )
780
+ err := c .processHook (ctx , cmd )
792
781
cmd .SetErr (err )
793
782
return err
794
783
}
@@ -799,7 +788,7 @@ func (c *Conn) Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder
799
788
800
789
func (c * Conn ) Pipeline () Pipeliner {
801
790
pipe := Pipeline {
802
- exec : c .hooks . processPipeline ,
791
+ exec : c .processPipelineHook ,
803
792
}
804
793
pipe .init ()
805
794
return & pipe
@@ -814,7 +803,7 @@ func (c *Conn) TxPipeline() Pipeliner {
814
803
pipe := Pipeline {
815
804
exec : func (ctx context.Context , cmds []Cmder ) error {
816
805
cmds = wrapMultiExec (ctx , cmds )
817
- return c .hooks . processTxPipeline (ctx , cmds )
806
+ return c .processTxPipelineHook (ctx , cmds )
818
807
},
819
808
}
820
809
pipe .init ()
0 commit comments