Skip to content

Commit 5fac913

Browse files
committed
wip, hooks refactor
1 parent 420c4fb commit 5fac913

File tree

4 files changed

+12
-27
lines changed

4 files changed

+12
-27
lines changed

internal/internal.go

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"github.com/redis/go-redis/v9/internal/rand"
77
)
88

9-
type ParentHooksMixinKey struct{}
10-
119
func RetryBackoff(retry int, minBackoff, maxBackoff time.Duration) time.Duration {
1210
if retry < 0 {
1311
panic("not reached")

redis.go

+9-20
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ func (hs *hooksMixin) processTxPipelineHook(ctx context.Context, cmds []Cmder) e
205205
type 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)
743732
type 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

781769
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)
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

792780
func (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.
919908
func 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,

sentinel.go

-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ func masterReplicaDialer(
309309
// SentinelClient is a client for a Redis Sentinel.
310310
type SentinelClient struct {
311311
*baseClient
312-
hooksMixin
313312
}
314313

315314
func NewSentinelClient(opt *Options) *SentinelClient {

tx.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ type Tx struct {
1919
baseClient
2020
cmdable
2121
statefulCmdable
22-
hooksMixin
2322
}
2423

2524
func (c *Client) newTx() *Tx {
2625
tx := Tx{
2726
baseClient: baseClient{
28-
opt: c.opt,
29-
connPool: pool.NewStickyConnPool(c.connPool),
27+
opt: c.opt,
28+
connPool: pool.NewStickyConnPool(c.connPool),
29+
hooksMixin: c.hooksMixin.clone(),
3030
},
31-
hooksMixin: c.hooksMixin.clone(),
3231
}
3332
tx.init()
3433
return &tx

0 commit comments

Comments
 (0)