Skip to content

Commit

Permalink
Reinstate read-only lock on hooks access in dialHook
Browse files Browse the repository at this point in the history
  • Loading branch information
LINKIWI committed Jan 11, 2025
1 parent 91dddc2 commit 88de941
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ type (
)

type hooksMixin struct {
hooksMu *sync.Mutex
hooksMu *sync.RWMutex

slice []Hook
initial hooks
current hooks
}

func (hs *hooksMixin) initHooks(hooks hooks) {
hs.hooksMu = new(sync.Mutex)
hs.hooksMu = new(sync.RWMutex)
hs.initial = hooks
hs.chain()
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func (hs *hooksMixin) clone() hooksMixin {
clone := *hs
l := len(clone.slice)
clone.slice = clone.slice[:l:l]
clone.hooksMu = new(sync.Mutex)
clone.hooksMu = new(sync.RWMutex)
return clone
}

Expand All @@ -176,7 +176,14 @@ func (hs *hooksMixin) withProcessPipelineHook(
}

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 {
Expand Down

0 comments on commit 88de941

Please # to comment.