@@ -205,6 +205,7 @@ func (hs *hooksMixin) processTxPipelineHook(ctx context.Context, cmds []Cmder) e
205205type baseClient struct {
206206 opt * Options
207207 connPool pool.Pooler
208+ hooksMixin
208209
209210 onClose func () error // hook called when client is closed
210211}
@@ -352,20 +353,8 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
352353 var err error
353354 cn .Inited = true
354355 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- }
367356
368- conn := newConn (c .opt , connPool , parentHooks )
357+ conn := newConn (c .opt , connPool , c . hooksMixin )
369358
370359 protocol := c .opt .Protocol
371360 // By default, use RESP3 in current version.
@@ -743,7 +732,6 @@ func txPipelineReadQueued(rd *proto.Reader, statusCmd *StatusCmd, cmds []Cmder)
743732type Client struct {
744733 * baseClient
745734 cmdable
746- hooksMixin
747735}
748736
749737// NewClient returns a client to the Redis Server specified by Options.
@@ -779,7 +767,7 @@ func (c *Client) WithTimeout(timeout time.Duration) *Client {
779767}
780768
781769func (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 )
783771}
784772
785773// 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 {
790778}
791779
792780func (c * Client ) Process (ctx context.Context , cmd Cmder ) error {
793- ctx = context .WithValue (ctx , internal.ParentHooksMixinKey {}, c .hooksMixin )
794781 err := c .processHook (ctx , cmd )
795782 cmd .SetErr (err )
796783 return err
@@ -913,20 +900,22 @@ type Conn struct {
913900 baseClient
914901 cmdable
915902 statefulCmdable
916- hooksMixin
917903}
918904
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.
919908func newConn (opt * Options , connPool pool.Pooler , parentHooks hooksMixin ) * Conn {
920909 c := Conn {
921910 baseClient : baseClient {
922- opt : opt ,
923- connPool : connPool ,
911+ opt : opt ,
912+ connPool : connPool ,
913+ hooksMixin : parentHooks .clone (),
924914 },
925915 }
926916
927917 c .cmdable = c .Process
928918 c .statefulCmdable = c .Process
929- c .hooksMixin = parentHooks
930919 c .initHooks (hooks {
931920 dial : c .baseClient .dial ,
932921 process : c .baseClient .process ,
0 commit comments