@@ -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 {
595584type 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
616605func (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
644633func (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
667656func (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
770759func 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
790779func (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
800789func (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