Skip to content

Commit 9db1286

Browse files
authored
Reinstate read-only lock on hooks access in dialHook (#3225)
1 parent 40e049e commit 9db1286

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

redis.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ type (
4141
)
4242

4343
type hooksMixin struct {
44-
hooksMu *sync.Mutex
44+
hooksMu *sync.RWMutex
4545

4646
slice []Hook
4747
initial hooks
4848
current hooks
4949
}
5050

5151
func (hs *hooksMixin) initHooks(hooks hooks) {
52-
hs.hooksMu = new(sync.Mutex)
52+
hs.hooksMu = new(sync.RWMutex)
5353
hs.initial = hooks
5454
hs.chain()
5555
}
@@ -151,7 +151,7 @@ func (hs *hooksMixin) clone() hooksMixin {
151151
clone := *hs
152152
l := len(clone.slice)
153153
clone.slice = clone.slice[:l:l]
154-
clone.hooksMu = new(sync.Mutex)
154+
clone.hooksMu = new(sync.RWMutex)
155155
return clone
156156
}
157157

@@ -176,7 +176,14 @@ func (hs *hooksMixin) withProcessPipelineHook(
176176
}
177177

178178
func (hs *hooksMixin) dialHook(ctx context.Context, network, addr string) (net.Conn, error) {
179-
return hs.current.dial(ctx, network, addr)
179+
// Access to hs.current is guarded by a read-only lock since it may be mutated by AddHook(...)
180+
// while this dialer is concurrently accessed by the background connection pool population
181+
// routine when MinIdleConns > 0.
182+
hs.hooksMu.RLock()
183+
current := hs.current
184+
hs.hooksMu.RUnlock()
185+
186+
return current.dial(ctx, network, addr)
180187
}
181188

182189
func (hs *hooksMixin) processHook(ctx context.Context, cmd Cmder) error {

0 commit comments

Comments
 (0)