@@ -205,6 +205,7 @@ func (hs *hooksMixin) processTxPipelineHook(ctx context.Context, cmds []Cmder) e
205
205
type baseClient struct {
206
206
opt * Options
207
207
connPool pool.Pooler
208
+ hooksMixin
208
209
209
210
onClose func () error // hook called when client is closed
210
211
}
@@ -352,20 +353,8 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
352
353
var err error
353
354
cn .Inited = true
354
355
connPool := pool .NewSingleConnPool (c .connPool , cn )
355
- var parentHooks hooksMixin
356
- pH := ctx .Value (internal.ParentHooksMixinKey {})
357
- switch pH := pH .(type ) {
358
- case nil :
359
- parentHooks = hooksMixin {}
360
- case hooksMixin :
361
- parentHooks = pH .clone ()
362
- case * hooksMixin :
363
- parentHooks = (* pH ).clone ()
364
- default :
365
- parentHooks = hooksMixin {}
366
- }
367
356
368
- conn := newConn (c .opt , connPool , parentHooks )
357
+ conn := newConn (c .opt , connPool , c . hooksMixin )
369
358
370
359
protocol := c .opt .Protocol
371
360
// By default, use RESP3 in current version.
@@ -743,7 +732,6 @@ func txPipelineReadQueued(rd *proto.Reader, statusCmd *StatusCmd, cmds []Cmder)
743
732
type Client struct {
744
733
* baseClient
745
734
cmdable
746
- hooksMixin
747
735
}
748
736
749
737
// NewClient returns a client to the Redis Server specified by Options.
@@ -779,7 +767,7 @@ func (c *Client) WithTimeout(timeout time.Duration) *Client {
779
767
}
780
768
781
769
func (c * Client ) Conn () * Conn {
782
- return newConn (c .opt , pool .NewStickyConnPool (c .connPool ), c .hooksMixin . clone () )
770
+ return newConn (c .opt , pool .NewStickyConnPool (c .connPool ), c .hooksMixin )
783
771
}
784
772
785
773
// Do create a Cmd from the args and processes the cmd.
@@ -790,7 +778,6 @@ func (c *Client) Do(ctx context.Context, args ...interface{}) *Cmd {
790
778
}
791
779
792
780
func (c * Client ) Process (ctx context.Context , cmd Cmder ) error {
793
- ctx = context .WithValue (ctx , internal.ParentHooksMixinKey {}, c .hooksMixin )
794
781
err := c .processHook (ctx , cmd )
795
782
cmd .SetErr (err )
796
783
return err
@@ -913,20 +900,22 @@ type Conn struct {
913
900
baseClient
914
901
cmdable
915
902
statefulCmdable
916
- hooksMixin
917
903
}
918
904
905
+ // newConn is a helper func to create a new Conn instance.
906
+ // the Conn instance is not thread-safe and should not be shared between goroutines.
907
+ // the parentHooks will be cloned, no need to clone before passing it.
919
908
func newConn (opt * Options , connPool pool.Pooler , parentHooks hooksMixin ) * Conn {
920
909
c := Conn {
921
910
baseClient : baseClient {
922
- opt : opt ,
923
- connPool : connPool ,
911
+ opt : opt ,
912
+ connPool : connPool ,
913
+ hooksMixin : parentHooks .clone (),
924
914
},
925
915
}
926
916
927
917
c .cmdable = c .Process
928
918
c .statefulCmdable = c .Process
929
- c .hooksMixin = parentHooks
930
919
c .initHooks (hooks {
931
920
dial : c .baseClient .dial ,
932
921
process : c .baseClient .process ,
0 commit comments