1
0
mirror of https://github.com/redis/go-redis.git synced 2025-04-17 20:17:02 +03:00

Reinstate read-only lock on hooks access in dialHook (#3225)

This commit is contained in:
LINKIWI 2025-02-11 07:50:31 -08:00 committed by GitHub
parent 40e049e67a
commit 9db1286414
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -41,7 +41,7 @@ type (
) )
type hooksMixin struct { type hooksMixin struct {
hooksMu *sync.Mutex hooksMu *sync.RWMutex
slice []Hook slice []Hook
initial hooks initial hooks
@ -49,7 +49,7 @@ type hooksMixin struct {
} }
func (hs *hooksMixin) initHooks(hooks hooks) { func (hs *hooksMixin) initHooks(hooks hooks) {
hs.hooksMu = new(sync.Mutex) hs.hooksMu = new(sync.RWMutex)
hs.initial = hooks hs.initial = hooks
hs.chain() hs.chain()
} }
@ -151,7 +151,7 @@ func (hs *hooksMixin) clone() hooksMixin {
clone := *hs clone := *hs
l := len(clone.slice) l := len(clone.slice)
clone.slice = clone.slice[:l:l] clone.slice = clone.slice[:l:l]
clone.hooksMu = new(sync.Mutex) clone.hooksMu = new(sync.RWMutex)
return clone return clone
} }
@ -176,7 +176,14 @@ func (hs *hooksMixin) withProcessPipelineHook(
} }
func (hs *hooksMixin) dialHook(ctx context.Context, network, addr string) (net.Conn, error) { func (hs *hooksMixin) dialHook(ctx context.Context, network, addr string) (net.Conn, error) {
return hs.current.dial(ctx, network, addr) // Access to hs.current is guarded by a read-only lock since it may be mutated by AddHook(...)
// while this dialer is concurrently accessed by the background connection pool population
// routine when MinIdleConns > 0.
hs.hooksMu.RLock()
current := hs.current
hs.hooksMu.RUnlock()
return current.dial(ctx, network, addr)
} }
func (hs *hooksMixin) processHook(ctx context.Context, cmd Cmder) error { func (hs *hooksMixin) processHook(ctx context.Context, cmd Cmder) error {